<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* redis 缓存操作
*/
class Cache_redis {
private $redis;
/**
* 初始化
* connect 脚本执行完,释放连接
* pconnect 脚本执行完,连接保持在php-fpm中
*/
public function __construct()
{
$this->redis = new Redis();
$this->redis->connect('127.0.0.1', 6379);
}
/**
* 设置值
* @param key 键
* @param val 值
* @return 成功true 失败false
*/
public function set($key,$val)
{
return $this->redis->set($key,$val);
}
/**
* 为指定的 key 设置值及其过期时间
* 如果 key 已经存在,将会替换旧的值
* @param key 键
* @param val 值
* @param time 有效期 缺省值 259200 (三天)
* @return 成功true 失败false
*/
public function setex($key,$val,$time=259200)
{
return $this->redis->setex($key,$time,$val);
}
/**
* 获取值
* @param key 键
* @return 成功返回值 失败返回false
*/
public function get($key)
{
return $this->redis->get($key);
}
/**
* 获取所有键
*/
public function keys()
{
return $this->redis->keys('*');
}
/**
* 删除指定键值
* @param keys key/array(key,key1,key2...)
*/
public function delete($keys)
{
return $this->redis->delete($keys);
}
/**
* 以秒为单位返回 key 的剩余过期时间
* 当 key 不存在时,返回 -2 。
* 当 key 存在但没有设置剩余生存时间时,返回 -1 。
* 否则,以秒为单位,返回 key 的剩余生存时间。
* 注意:在 Redis 2.8 以前,当 key 不存在,或者 key 没有设置剩余生存时间时,命令都返回 -1
*/
public function ttl($key)
{
return $this->redis->ttl($key);
}
}
PHP - redis类
最新推荐文章于 2024-01-12 08:21:42 发布