redis缓存
//调用redis的客户端函数 $redis = RedisCache::get_instance(); $redis_key = "string:traning:mode:send:msg:".$user['id']; $ttl = 86400; $redis_val = $redis->set($redis_key,time(),$ttl);
基类: app\common\service\RedisCache.php
<?php namespace app\common\service; //1.创建一个单例类 use think\Config; class RedisCache { private static $redis; private function __construct() { echo '我被实例化了!'; } private function __clone() { trigger_error('Clone is not allow', E_USER_ERROR); } public static function get_instance() { if (!isset(self::$redis)) { self::$redis = new \Redis(); $config = Config::get('cache.redis'); self::$redis->connect($config['host'],$config['port'],$config['timeout']); if(!empty($config['password'])){ self::$redis->auth($config['password']); } if (!empty($config['prefix'])) { self::$redis->setOption(\Redis::OPT_PREFIX, $config['prefix']); } } return self::$redis; } //类中的方法只会有一个实例执行它 public function test() { echo("单例模式设计成功"); } }
删除指定前缀的所有redis的key
$redis = RedisCache::get_instance(); $key1 = $redis->keys('test_*');//查找所有以test_开头的redis的keys $key2 = $redis->keys('*login:id:*'); //查找所有包含‘login:id:’字符的keys $redis->del($key1); //删除所有以test_开头的redis的keys
命令行删除redis的key
redis -p 6379 -a 密码 keys ‘test_*’ | xargs redis -p 6379 -a 密码 del