The Memcache cache adapter is meant to be used through the Cache interface, which abstracts away key generation, adapter instantiation and filter implementation. A simple configuration of this adapter can be accomplished in app/config/bootstrap.php as follows: {{{ Cache::config(array( 'cache-config-name' => array( 'adapter' => 'Memcached', 'servers' => array( array('127.0.0.1', 11211, 100) ) ) )); }}} The 'servers' key accepts entries as arrays, where the format is array(server, port, [weight]), with the weight being optional. This Memcache adapter provides basic support for write, read, delete and clear cache functionality, as well as allowing the first four methods to be filtered as per the Lithium filtering system. Additionally, This adapter defines several methods that are _not_ implemented in other adapters, and are thus non-portable - see the documentation for Cache as to how these methods should be accessed. This adapter stores two keys for each written value - one which consists of the data to be cached, and the other being a cache of the expiration time.
See also: lithium\storage\Cache::key()
See also: lithium\storage\Cache::adapter()
Inheritance: extends lithium\core\Object
 public function testWriteDefaultCacheExpiry()
 {
     $memcache = new Memcache(array('expiry' => '+5 seconds'));
     $key = 'default_key';
     $data = 'value';
     $closure = $memcache->write($key, $data);
     $this->assertTrue(is_callable($closure));
     $params = compact('key', 'data');
     $result = $closure($memcache, $params);
     $expected = $data;
     $this->assertEqual($expected, $result);
     $result = $this->_conn->get($key);
     $this->assertEqual($expected, $result);
     $result = $this->_conn->delete($key);
     $this->assertTrue($result);
 }
示例#2
0
 public function testIncrementWithScope()
 {
     $adapter = new Memcache(array('scope' => 'primary'));
     $this->_conn->set('primary:key1', 1, 60);
     $this->_conn->set('key1', 1, 60);
     $adapter->increment('key1');
     $expected = 1;
     $result = $this->_conn->get('key1');
     $this->assertEqual($expected, $result);
     $expected = 2;
     $result = $this->_conn->get('primary:key1');
     $this->assertEqual($expected, $result);
 }
示例#3
0
文件: cache.php 项目: EHER/chegamos
/**
 * This file creates a default cache configuration using the most optimized adapter available, and
 * uses it to provide default caching for high-overhead operations.
 */
use lithium\storage\Cache;
use lithium\core\Libraries;
use lithium\action\Dispatcher;
use lithium\storage\cache\adapter\Memcache;
use lithium\storage\cache\adapter\File;
if (PHP_SAPI === 'cli') {
    return;
}
/**
 * If APC is not available and the cache directory is not writeable, bail out.
 */
if (!($memcacheEnabled = Memcache::enabled()) && !is_writable(LITHIUM_APP_PATH . '/resources/tmp/cache')) {
    return;
}
if ($memcacheEnabled && USE_MEMCACHED === true) {
    Cache::config(array('default' => array('adapter' => 'Memcache', 'host' => MEMCACHED_SERVER)));
} else {
    Cache::config(array('default' => array('adapter' => 'File')));
}
Dispatcher::applyFilter('run', function ($self, $params, $chain) {
    if ($cache = Cache::read('default', 'core.libraries')) {
        $cache = (array) unserialize($cache) + Libraries::cache();
        Libraries::cache($cache);
    }
    $result = $chain->next($self, $params, $chain);
    if ($cache != Libraries::cache()) {
        Cache::write('default', 'core.libraries', serialize(Libraries::cache()), '+1 day');