I have a set of classes which have a habit of being called repeatedly with the same arguments. These methods generally run database requests and build arrays of objects and such, and so to cut out this duplication I've constructed a couple of caching methods to optimise. These are used like so:
Before caching applied:
public function method($arg1, $arg2) {
$result = doWork();
return $result;
}
After caching applied:
public function method($arg1, $arg2, $useCached=true) {
if ($useCached) {return $this->tryCache();}
$result = doWork();
return $this->cache($result);
}
Unfortunately I'm now left with the slightly laborious task of manually adding this to all of the methods- I believe this is a use case of the decorator pattern but I can't figure out how to implement it in a simpler way in PHP for this case.
What's the best way to do this, hopefully such that either all methods in any of these classes automatically do this, or I just have to add one line in the method etc?
I've had a look at ways to override the return statement and such but can't really see anything.
Thanks!
Use the
__call
magic method.This is used using internal storage but you can use the DB for this and create your own Transient storage. Just append
_Cached
orCached
to any method that exists. Obviously, you can change the lifespan and more.This is just proof of concept. There's room for much improvement :)
Here is an extract from an article around the subject of caching in php
Makes more sense to me as it delivers in a SOLID implementation way. I am not a huge fan of implementing the same with annotations, would prefer something simpler.
If you don't need Type Safety, you can use a generic Cache Decorator:
You would then wrap an instance that needs caching into this Decorator like this:
Obviously, the
$cachedInstance
does not have afoo()
method. The trick here is to utilize the magic__call
method to intercept all calls to inaccessible or non-existing methods and delegate them to the decorated instance. This way we are exposing the entire public API of the decorated instance through the Decorator.As you can see, the
__call
method also contains the code to check whether there is a caching defined for that method. If so, it will return the cached method call. If not, it will call the instance and cache the return.Alternatively, you pass in a dedicated CacheBackend to the Decorator instead of implementing the Caching in the decorator itself. The Decorator would then only work as a Mediator between the decorated instance and the backend.
The drawback of this generic approach is that your Cache Decorator will not have the type of the Decorated Instance. When your consuming code expects instances of type Instance, you will get errors.
If you need type-safe decorators, you need to use the "classic" approach:
In a nutshell
The drawback is, that it is more work. You need to do that for every class supposed to use caching. You'll also need to put the check to the cache backend into every function (code duplication) as well as delegating any calls that don't need caching to the decorated instance (tedious and error prone).