peloncano / cakephp-plugin-redis-cache
此包最新版本(1.1.0)没有可用的许可信息。
CakePHP 2.0 插件,支持使用 Redis(https://redis.ac.cn/)作为会话和缓存的后端。使用 Predis(https://github.com/nrk/predis)库与 Redis 进行通信。
1.1.0
2019-08-23 13:40 UTC
Requires
- php: >=5.3.0
- composer/installers: ~1.0
- predis/predis: ~1.1
This package is auto-updated.
Last update: 2024-09-26 14:51:50 UTC
README
A CakePHP 2.x 插件,支持使用 Redis 作为会话和缓存的后端。使用 Predis 库与 Redis 进行通信。
需要复活此插件以连接到需要通过 TLS/SSL 连接到 Redis 的旧项目。
安装
运行 composer 命令
composer require peloncano/cakephp-plugin-redis-cache
需要初始设置
插件包含自己的 bootstrap.php
文件,必须在应用的 app/Config/bootstrap.php
文件中加载,如下所示
CakePlugin::load(array('RedisCache' => array('bootstrap' => true)));
默认情况下,插件将通过 localhost
的端口 6379
与无密码的 Redis 服务器进行通信。但是,如果需要,您也可以使用远程 Redis 服务器。您还可以为缓存和会话任务配置不同的服务器,因为这些配置可能因性能最佳而略有不同。
用于缓存
连接配置
如果加载配置文件(文档)
$config['RedisCache'] = array(
'cache' => array(
'password' => '',
'port' => 6379,
'database' => 0
)
);
如果需要 TLS/SSL
$config['RedisCache'] = array(
'cache' => array(
'scheme' => 'tls',
'ssl' => ['verify_peer' => false], // any other predis SSL related options here
'host' => 'localhost',
'password' => '',
'port' => 6379, // could be different for tls connections
'database' => 0
)
);
在 app/Config/bootstrap.php
中,您现在可以配置您的缓存
注意:请确保在加载插件之后完成此操作
Cache::config('default', array('engine' => 'RedisCache.Redis'));
// with prefix and duration
Cache::config('other_cache_with_prefix', array(
'engine' => 'RedisCache.Redis',
'prefix' => 'other_cache_with_prefix',
'duration'=> '+1 week'
));
// `throwExceptions` set to TRUE will throw exceptions on redis connection issues
// by default this is FALSE which will allow the app to continue working if caching fails
// due to redis/predis connection issues
Cache::config('other_cache', array(
'engine' => 'RedisCache.Redis',
'throwExceptions' => true
));
用于会话处理(待定)
在 app/Config/core.php
中的会话配置部分
Configure::write('Session', array(
'cookie' => {{YOUR COOKIE NAME HERE}}
, 'timeout' => 60
, 'handler' => array(
'engine' => 'RedisCache.RedisSession'
)
));
全局前缀
您可以在插件加载后立即添加全局前缀,以将所有键命名空间化。请添加以下行。
对于 Cache
RedisConfig::$globalCachePrefix = Inflector::slug('my app cache prefix') . ':'; // use ':' to group keys in redis
对于 Session
RedisConfig::$globalSessionPrefix= Inflector::slug('my app session prefix') . ':'; // use ':' to group keys in redis
注意:这将在任何特定缓存前缀之前应用