kalaspuff/redis-cache-zend-framework

Zend Framework 的 Redis 缓存后端

dev-master 2017-07-07 07:14 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:45:56 UTC


README

PHP 需要安装 phpredis 扩展,以便 PHP 与 Redis 键值存储进行通信。请从 https://github.com/nicolasff/phpredis 安装可用的 phpredis PHP 扩展开始。

通过在项目中的某个位置调用以下代码来设置 Redis 缓存后端。

$redisCache = Zend_Cache::factory(
    new Zend_Cache_Core(array(
        'lifetime' => 3600,
        'automatic_serialization' => true,
    )),
    new Extended_Cache_Backend_Redis(array(
        'servers' => array(
            array(
                'host' => '127.0.0.1',
                'port' => 6379,
                'dbindex' => 1,
                // 'persistent' => false, // not a persistent connection
                // 'auth' => true, // enable authentication
                // 'password' => 'mypwd000000', // password to authenticate on redis server
            ),
        ),
        // 'key_prefix' => 'my_app', // if desire to add a prefix to all cache keys
    ))
);

从缓存中写入和读取的方式与所有其他 Zend Framework 缓存后端相同。

$cacheKey = 'my_key';
$data = 'e48e13207341b6bffb7fb1622282247b';

/* Save data to cache */
$redisCache->save($data, $cacheKey, array('tag1', 'tag2'));

/* Load data from cache */
$data = $redisCache->load($cacheKey);

/* Clear all keys with tag 'tag1' */
$redisCache->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG, array('tag1'));

/* Clear all cache (flush cache) */
$redisCache->clean(Zend_Cache::CLEANING_MODE_ALL);