When working with web based applications, usually query results (or the results of complex calculations that require substantial computing power) that don't change frequently would be cached in memory, or in a file based data store.
When these results are likely to change for every request, but are consumed by a number of classes in your application, what would be the best way to go about caching this information for the duration of your request while keeping things SOLID?
The most straight forward solution I see, and what I'm implementing right now, is for every such instance checking if the result has already been calculated/retrieved, and if not, calculating it and storing it in a variable.
public function getSomething()
if( ! isset($this->something) )
$this->something = $this->calculateSomething();
I seem to have found the answer to my own question.
The technique (or pattern, if you want to call it that) is called memoization.
This article by Edd Mann provides a good practical example on how one could implement it:
The code in this example is procedural but can easily be adapted for use in an object oriented environment.
All in all this is a pretty simple technique that could boost performance quite a bit in certain scenarios. I'm certainly glad I now have a word I can use when trying to explain this to someone else! :-)