I am writing a class to handle a memcached object. The idea was to create abstract class Cachable, and all cached objects (such as User, Post, etc.) would be subclasses of the specified class.
The class offers some method, for example Load(), that calls an abstract function LoadFromDB(), if the object is not cached, the function of updating / invalidating the cache, etc.
The main problem is Load(); I wanted to do something like this:
protected function Load($id)
{
$this->memcacheId = $id;
$this->Connect();
$cached = $this->memcache->get(get_class($this) . ':' . $id);
if($cached === false)
{
$this->SetLoaded(LoadFromDB($id));
UpdateCache();
} else {
$this = $cached;
$this->SetLoaded(true);
}
}
Unfortunately, I need $thisto become $cached(cached object); is there any way to do this? Was “every cached object comes from a cached class” a bad design idea?