2

据我所知,这似乎是设置 Memcached 并设置 TTL 和命名空间的方法,但它们在缓存中没有效果。密钥没有以命名空间为前缀,并且过期时间是无限的。

    $MemcachedOptions = new \Zend\Cache\Storage\Adapter\MemcachedOptions();
    $MemcachedResourceManager = new \Zend\Cache\Storage\Adapter\MemcachedResourceManager(1, new \Zend\Cache\Storage\Adapter\Memcached());
    $MemcachedResourceManager->addServer(1, array('localhost', 11211));
    $MemcachedOptions->setResourceManager($MemcachedResourceManager);

    $MemcachedOptions->setNamespace('FooBar_');
    $MemcachedOptions->setTtl(10);

    $cache = $MemcachedOptions->getResourceManager()->getResource(1);
    $cache->set('foobar_key','I am in cache');

有没有人有任何提示,线索?任何帮助将非常感激。

4

1 回答 1

9

MemcachedResourceManager当您尝试使用它时,其工作方式会有所不同。

你应该像下面这样初始化它:

// init a memcached resource manager with one native memcached resource
// using resource id "1"
$MemcachedResourceManager = new \Zend\Cache\Storage\Adapter\MemcachedResourceManager();
$MemcachedResourceManager->addServer('1', array('localhost', 11211));

// init a memcached storage adapter
// using the native memcached resource of id "1"
// configure it with a ttl and a namespace
$cache = \Zend\Cache\StorageFactory::adapterFactory('memcached', array(
    'resource_manager' => $MemcachedResourceManager,
    'resource_id'      => '1',
    'namespace'        => 'FooBar_',
    'ttl'              => 10,
));

// or
$memcachedAdapterOptions = new \Zend\Cache\Storage\Adapter\MemcachedOptions(array(
    'resource_manager' => $MemcachedResourceManager,
    'resource_id'      => '1',
    'namespace'        => 'FooBar_',
    'ttl'              => 10,
));
$cache = new \Zend\Cache\Storage\Adapter\Memcached($memcachedAdapterOptions);

这些类如何协同工作:

最重要的类是Zend\Cache\Storage\Adapter\Memcached它是MemcachedZend\Cache\StorageInterface.

此存储适配器有许多定义为Zend\Cache\Storage\Adapter\MemcachedOptions.

因为 ZF2 中的缓存存储适配器旨在处理一种类型的项目来存储您需要不同Zend\Cache\Storage\Adapter\Memcached类型的项目的不同实例。但是您不会使用与 memcached(本地Memcached类的不同实例)服务器的不同连接 - 这是Zend\Cache\Storage\Adapter\MemcachedResourceManager可以发挥作用的。

将使用的Zend\Cache\Storage\Adapter\MemcachedResourceManager句柄本机实例。MemcachedZend\Cache\Storage\Adapter\Memcached

于 2013-08-29T12:30:16.887 回答