aloframework / cache
双向 Redis 缓存管理
2.0
2016-11-07 22:22 UTC
Requires
- php: >=5.5.9
- ext-redis: *
- aloframework/common: ^2.0
- aloframework/config: ^2.0
- aloframework/log: ^3.0
Requires (Dev)
- aloframework/handlers: ^3.0
- symfony/var-dumper: ^2.7 || ^3.0
This package is not auto-updated.
Last update: 2024-09-14 18:42:04 UTC
README
Redis 缓存管理器
最新发布 API 文档:https://aloframework.github.io/cache/
安装
可以通过 Composer 进行安装
composer require aloframework/cache
用法
目前仅支持 Redis。
<?php use AloFramework\Cache\Clients\RedisClient; $redis = new RedisClient(); $redis->connect('localhost'); $redis->setKey('foo', 'bar', 300); // expire the key in 5 minutes $redis->setKey('foo', 'bar', new DateTime('2015-01-01 05:05:05')); //Expire the key on 2015-01-01 05:05:05 $redis->setKey('foo', 'bar'); //Use default expiration time $redis['foo'] = 'bar'; //Use default expiration time //Echo the keys echo $redis->getKey('foo'); echo $redis['foo']; //Echo all the keys print_r($redis->getAll()); //Loop through all the keys foreach ($redis as $k => $v) { echo 'Key: ' . $k . ', Value: ' . $v . '<br/>'; } //Count the number of items in the database echo count($redis); //Or do anything you would with the standard Redis class - RedisClient extends it.
替代用法
<?php use AloFramework\Cache\Clients\RedisClient; use AloFramework\Cache\CacheItem; //Save an item $server = new RedisClient(); $server->connect(); $cacheItem = new CacheItem('key', 'value', $server); $cacheItem->lifetime = 600; $cacheItem->saveToServer(); // Get or load an item $cacheItem = new CacheItem('key'); $cacheItem->getFromServer($server); echo $cacheItem->value;
配置
配置通过 配置类 完成。
Cfg::CFG_IP
- connect() 方法中使用的默认 IP(默认为 127.0.0.1)Cfg::CFG_PORT
- connect() 方法中使用的默认端口(默认为 6379)Cfg::CFG_TIMEOUT
- 默认缓存超时时间(默认为 300 秒)