本文实例讲述了CI框架中redis缓存相关操作文件。分享给大家供大家参考,具体如下:
redis缓存类文件位置:
'cisystemlibrariesCachedriversCache_redis.php'
_redis->keys($key);
}
public function get($key)
{
return $this->_redis->get($key);
}
public function mget($keys)
{
return $this->_redis->mget($keys);
}
public function save($id, $data, $ttl = 60, $raw = FALSE)
{
return ($ttl)
? $this->_redis->setex($id, $ttl, $data)
: $this->_redis->set($id, $data);
}
public function delete($key)
{
return ($this->_redis->delete($key) === 1);
}
public function hincrby($key, $field, $value = 1)
{
return $this->_redis->hIncrBy($key, $field, $value);
}
public function hincrbyfloat($key, $field, $value = 1)
{
return $this->_redis->hIncrByFloat($key, $field, $value);
}
public function lpush($key, $value)
{
return $this->_redis->lPush($key, $value);
}
public function rpush($key, $value)
{
return $this->_redis->rPush($key, $value);
}
public function rpop($key)
{
return $this->_redis->rPop($key);
}
public function brpop($key,$ontime=0)
{
return $this->_redis->brPop($key,$ontime);
}
public function llen($key)
{
return $this->_redis->lLen($key);
}
public function increment($id, $offset = 1)
{
return $this->_redis->exists($id)
? $this->_redis->incr($id, $offset)
: FALSE;
}
public function incrby($key, $value = 1)
{
return $this->_redis->incrby($key, $value);
}
public function expire($key, $seconds)
{
return $this->_redis->expire($key, $seconds);
}
public function hset($alias,$key, $value)
{
return $this->_redis->hset($alias,$key, $value);
}
public function hget($alias,$key)
{
return $this->_redis->hget($alias,$key);
}
public function hkeys($alias)
{
return $this->_redis->hkeys($alias);
}
public function hgetall($alias)
{
return $this->_redis->hgetall($alias);
}
public function hmget($alias,$key)
{
return $this->_redis->hmget($alias,$key);
}
public function hdel($alias,$key)
{
return $this->_redis->hdel($alias,$key);
}
public function hvals($alias)
{
return $this->_redis->hvals($alias);
}
public function hmset($alias,$array)
{
return $this->_redis->hmset($alias,$array);
}
public function decrement($id, $offset = 1)
{
return $this->_redis->exists($id)
? $this->_redis->decr($id, $offset)
: FALSE;
}
public function clean()
{
return $this->_redis->flushDB();
}
public function cache_info($type = NULL)
{
return $this->_redis->info();
}
public function get_metadata($key)
{
$value = $this->get($key);
if ($value)
{
return array(
'expire' => time() + $this->_redis->ttl($key),
'data' => $value
);
}
return FALSE;
}
public function is_supported()
{
if (extension_loaded('redis'))
{
return $this->_setup_redis();
}
else
{
log_message('debug', 'The Redis extension must be loaded to use Redis cache.');
return FALSE;
}
}
protected function _setup_redis()
{
$config = array();
$CI =& get_instance();
if ($CI->config->load('redis', TRUE, TRUE))
{
$config += $CI->config->item('redis');
}
$config = array_merge(self::$_default_config, $config);
$config = !empty($config['redis'])?$config['redis']:$config;
$this->_redis = new Redis();
try
{
if ($config['socket_type'] === 'unix')
{
$success = $this->_redis->connect($config['socket']);
}
else // tcp socket
{
$success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']);
}
if ( ! $success)
{
log_message('debug', 'Cache: Redis connection refused. Check the config.');
return FALSE;
}
}
catch (RedisException $e)
{
log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')');
return FALSE;
}
if (isset($config['password']))
{
$this->_redis->auth($config['password']);
}
return TRUE;
}
public function __destruct()
{
if ($this->_redis)
{
$this->_redis->close();
}
}
}
更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《php缓存技术总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。



