Reputation: 4825
Hello,
consider class method, that each time do some code preparations, i.e. instance new object, create array or concatenate long strings like so:
class Example
{
public function methodWithPreparations($timestamp)
{
// preparations
$date = new DateTime();
$format = implode('-', array('Y', 'm', 'd', 'h', 'i', 's'));
$append = ' some text';
$append .= OtherClass::someText(); // persisten during runtime
// using function arguments
return $date->setTimestamp($timestamp)->format($format).$append;
}
}
Is it good to implement some cache mechanism to code preparations, or modern PHP is capable to handle it itself? I use PHP 5.6, but I've never deeply studied how it internally works.
What about using $cache
property like so:
class Example
{
protected $cache;
public function methodWithPreparations($timestamp)
{
if(empty($cache = &$this->cache('chacheKey'))) {
$cache['dateTime'] = new DateTime();
$cache['format'] = implode('-', array('Y', 'm', 'd', 'h', 'i', 's'));
$cache['append'] = ' some text'.OtherClass::someText();
}
// and then
return $cache['dateTime']->setTimestamp($timestamp)
->format($cache['format']).$cache['append'];
}
}
What do you think?
Upvotes: 0
Views: 894
Reputation: 48357
You use PHP - so you should have some familiarity with RFC 2616. You should be aware of the complexity of aching as implemented with HTTP/1.1, and the problems around cache management in operating a website. Caching the output of a function is no less complex a proposal, but PHP does not attempt to implement any mechanism to do so. As a programing language it doesn't need to. (interestingly the semantics of the procedural language implemented by MySQL does explicitly allow for caching, but it is very, very limited in its implementation).
PHP doesn't know how long a value could be cached for. It doesn't know the conditions which would invalidate that cache. It has no means to maintain an asociation of a data item with the code which generated it.
That's your job.
And for exactly the same reason, we can't say whether your approach is valid. Saving CPU cycles (or database queries, or network packets...) is usually a good idea. But, for example, your code above becomes increasingly broken the longer the object persists for. If it's handling a shortlived web request, that may not be a problem - but if you're writing a monitoring daemon for a nuclear reactor then you might not want to using caching for tie values (and time dependent values should have a TTL).
Upvotes: 1
Reputation: 4160
PHP doesn't cache those variables for you. Create them in the constructor as class variables and reuse them later when needed.
public function __construct() {
$this->dateTime = new DateTime();
$this->dateFormat = implode('-', array('Y', 'm', 'd', 'h', 'i', 's'));
$this->append = ' some text'.OtherClass::someText();
}
public function methodWithPreparations($timestamp) {
return $this->dateTime->setTimestamp($timestamp)->format($this->dateFormat).$this->append;
}
You don't need to re create the DateTime because you can reuse it. You can make a cache for different dateFormats and appends if not only the timestamp change.
Upvotes: 1