vendredi 6 novembre 2015

Caching with different expiration values

Say I have following query method with caching:

public function getData($params, $expires)
{
    $key = createKey($params);
    if($cache->get($key)) {
        return $cache->get($key);
    } else {
        $data = getData($params);
        $cache->set($key, $params, $expires);
        return $data;
    }
}

$expires parameter is not included in cache-key generation. So I can have following logic:

 // accepts 1 day lag
 getData($params, '1 day');
// accepts 1 hour lag
 getData($params, '1 hour');

First method invocation will cache data for 1 day and that will break logic of second method.

The simpliest solution is to include $expires param to cache-key, but that will generate a lot of cached keys probably with same data.

Is there any better solution of that problem?

Aucun commentaire:

Enregistrer un commentaire