多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### **1.String 字符串** * 最常见的数据类型 * 可以为任何类型的字符串,比如二进制,json对象 * 最大容量512M ### **2.常见操作** 1. 赋值 `$redis->set('key','value');` 2. 赋值并添加过期时间 `$redis->setex('key','expire','value');` 3. 取值 `$redis->get('key');` 4. 递增数字 `$redis->incr('int_key');` 5. 增加指定的数字 `$redis->incrBy('int_key','number');` 6. 递减数字 `$redis->decr('key1');` 7. 减少指定的数字 `$redis->descBy('key1','number');` 8. 增加指定浮点数 `$redis->incrByFloat('key1',1.5);` 9. 向尾部追加值 `$redis->append('key','value2');` 10. 获取字符串长度 `$redis->strlen('key');` 11. 同时设置多个key的值 `$redis->mSet(array('key0' => 'value0','key1' => 'value1'));` 12. 同时获取多个key的值 `$redis->mGet(array('key0','key1'));` ### **3.封装自己的Redis类** #### 1.编写 Redis 类 ``` class Redis { private static $write_handle = null; private static $read_handle = null; /**初始化 * @return Redis|null */ public static function getWrite() { $option = array( 'host' => Config::get('cache.REDIS_W_HOST', '127.0.0.1'), 'port' => Config::get('cache.REDIS_W_PORT', '6379'), 'auth' => Config::get('cache.REDIS_W_PASSWORD', '') ); if (!self::$write_handle) { self::$write_handle = new \Redis(); self::$write_handle->connect($option['host'], $option['port']); self::$write_handle->auth($option['auth']); } return self::$write_handle; } public static function getRead() { $option = array( 'host' => Config::get('cache.REDIS_R_HOST', '127.0.0.1'), 'port' => Config::get('cache.REDIS_R_PORT', '6379'), 'auth' => Config::get('cache.REDIS_R_PASSWORD', '') ); if (!self::$read_handle) { self::$read_handle = new \Redis(); self::$read_handle->connect($option['host'], $option['port']); self::$read_handle->auth($option['auth']); } return self::$read_handle; } /** * 存储 * @param $key * @param $value * @param int $expire */ public static function set($key, $value, $expire = 0) { if (!self::getWrite()) { return false; } if ($expire == 0) { return self::getWrite()->set($key, $value); } else { return self::getWrite()->setex($key, $expire, $value); } } /** * 获取 * @param $key * @return mixed */ public static function get($key) { $func = is_array($key) ? 'mget' : 'get'; return self::getRead()->{$func}($key); } /**自增 * @param $key * @param int $number * @return false|int */ public static function incr($key, $number = 1) { if (!self::getWrite()) { return false; } if ($number == 1) { return self::getWrite()->incr($key); } else { return self::getWrite()->incrBy($key, $number); } } /**获取长度 * @param $key * @return int */ public static function strlen($key) { return self::getRead()->strlen(); } } ``` #### 2.调用 ``` \Driver\Cache\Redis::set('user','李四',600); echo \Driver\Cache\Redis::get('user'); ``` ### **5.Zset 可排序集合**