C
C
Cat Anton2014-11-04 20:34:31
Zend Framework
Cat Anton, 2014-11-04 20:34:31

How to make Zend\Cache\Storage\Adapter\Filesystem work with multiple keys?

You need to implement the following behavior:

$cache->setItem(array('key1', 'key2', 'key3'), 'value');
$cache->getItem('key1');
$cache->getItem('key2');
$cache->getItem('key3');

That is, the same cache file can be accessed using different keys.
Are there any ways to implement something similar using the usual Zend\Cache\Storage\Adapter\Filesystem, or is the only way out is to write your own adapter/plugin?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Narek, 2014-11-09
@27cm

Keep the list of keys separately and cache by the main key.

$allKeys = array(
            'Page' => array('page1', 'page2', ...),
);
function getKey($searchKey){
    foreach($allKeys as $mainKey => $keys){
        if (array_key_exists($searchKey, $keys[$mainKey])){
            return $keys[$maintKey][$searchKey];
        }
        return null;
    }
}
$key = getKey('page1');// or getKey('page2') // return Page
$cache->getItem($key);

A
Alexander Wolf, 2014-11-04
@mannaro

I think this is an unusual task. So write a function:

function setItems($arr, $value) {
  for($key in $arr) {
    $cache->setItem($key, $value);
  }
}

setItem(['key1', 'key2'], 'value');
$cache->getItem('key1') == $cache->getItem('key2'); # true
And, in general, reconsider your architecture.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question