本文介绍了在Zend Framework 2中使用Memcached设置ttl和名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,这似乎是设置Memcached以及设置TTL和命名空间的方法,但它们在缓存中没有任何作用.密钥没有前缀命名空间,并且到期时间是无限的.

As far as I can figure this seems to be the way to set up Memcached and set the TTL and Namespace but they have no effect in the cache. The key is not prefixed with a namespace and the expire is infinite.

    $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');

有人有任何提示,线索吗?任何帮助将不胜感激.

Does anyone have any tips, clues? Any help would be much appreciated.

推荐答案

MemcachedResourceManager在尝试使用它时会有所不同.

The MemcachedResourceManager works different as you trying to use it.

您应按以下方式对其进行初始化:

You should initialize it like the following:

// 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,它是在Zend\Cache\StorageInterface上下文中使用的Memcached本地实例的包装.

The most important class is Zend\Cache\Storage\Adapter\Memcached it is a wrapper for a native instance of Memcached used in a context of Zend\Cache\StorageInterface.

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

This storage adapter has a number of options defined as Zend\Cache\Storage\Adapter\MemcachedOptions.

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

Because cache storage adapters in ZF2 are designed to handle one type of items to store you need different instances of Zend\Cache\Storage\Adapter\Memcached for different type of items. But you don't wont to use different connections to a memcached (different instance of the native Memcached class) server - this is were Zend\Cache\Storage\Adapter\MemcachedResourceManager comes to play.

Zend\Cache\Storage\Adapter\MemcachedResourceManager处理Memcached的本机实例,这些实例将由Zend\Cache\Storage\Adapter\Memcached使用.

The Zend\Cache\Storage\Adapter\MemcachedResourceManager handles native instances of Memcached which will be used by Zend\Cache\Storage\Adapter\Memcached.

这篇关于在Zend Framework 2中使用Memcached设置ttl和名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 00:32