Any ideas for a cache manager?

Hi all. I'm sure that many of you have already managed a structured cache system in your projects. What I think it's very important is the possibility to use or disable cache without modifying the code, managing indexes and deleting cache when inserting or updating something in the backend. I immagine to manage cache refreshing with events to fire on model saving in the backend, so if the cache is disabled you don't receive any error. What about using the cache inside the frontend? I think there should be the possibility to disable it by config file (cache enabled or disabled) and in debug mode, but putting an if everytime i call the cache is not a good way to perform it. How have you managed a structured caching system?

Hi @Cosimo you can overwrite the get method in your own cache adapter for example

class myCacheAdapter implement Phalcon\Cache\BackendInterface {
    public function get($id) {
        if ($cacheServiceIsAvailable === true) { // cache service is active?
            return parent::get($id);
        }

        return false;
    }

    // the rest of adapter
}

In this way you can enable the service whenever you want without having to modify all the code of your application

Good luck



1.8k

Very nice, thank you @emiliodeg. In terms of performance, returning false is neutral or a little bit slower than not using cache at all?

returning false you can continue using the adapter without changes

$element = $this->persistent->get('myElement'); // posible element cached

if (!$element) { // no cached
    $element = $this->elementManager->get('myElement'); // get fresh element

    $this->persistent->set('myElement', $element); // here you have to overwrite set method to cache is the service is active, similar on get
}

echo $element;

I hope it's help you

Good luck