【问题标题】:Obtaining All Cache Keys in Zend-Cache获取 Zend-Cache 中的所有缓存键
【发布时间】:2017-07-24 02:29:20
【问题描述】:

这是我的缓存初始化代码:

use Zend\Cache\StorageFactory;
$cache   = StorageFactory::factory(array(
                    'adapter' => array(
                            'name'    => 'filesystem',
                            // With a namespace we can indicate the same type of items
                            // -> So we can simple use the db id as cache key
                            'options' => array(
                                    'namespace' => 'dbtable',
                                    'cache_dir' => Pluto::path('cache')
                            ),
                    ),
                    'plugins' => array(
                            // Don't throw exceptions on cache errors
                            'exception_handler' => array(
                                    'throw_exceptions' => false
                            ),
                            // We store database rows on filesystem so we need to serialize them
                            'Serializer'
                    )
            ));

我想知道的是如何获取我们在这个缓存对象中的所有缓存键

例如,现在执行这段代码:

$cache->setItem('key1','foo');

$cache->setItem('key2','bar');

$cache->setItem('key3','baz');

并在不同的区域/点执行此代码:

$cache->setItem('key4','foo2');

$cache->setItem('key5','bar2');

$cache->setItem('key6','baz2');

我想要一个包含['key1','key2','key3','key4','key5','key6'] 的数组,它可能来自缓存对象内所有键的内部数组(包括在此特定请求期间未受影响的那些)

【问题讨论】:

    标签: php zend-framework zend-framework2 zend-framework3 zend-cache


    【解决方案1】:

    AFAIK,zend-cache 没有用于检索缓存对象内的所有键的方法。但是,如果您想检索所有键,则可以迭代该对象。不是array,但如果你愿意,可以改成array

    $caches = $cache->getIterator();
    $cacheKeys = []
    foreach ($caches as $key) {
        // $key is the cache key
        $cacheKeys[] = $key;
    }
    
    print_r($cacheKeys);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 2018-10-26
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      相关资源
      最近更新 更多