本文实例讲述了ci框架中redis缓存相关操作文件。分享给大家供大家参考,具体如下:
redis缓存类文件位置:
'cisystemlibrariescachedriverscache_redis.php'
<?php
/**
* codeigniter
*
* an open source application development framework for php 5.2.4 or newer
*
* notice of license
*
* licensed under the open software license version 3.0
*
* this source file is subject to the open software license (osl 3.0) that is
* bundled with this package in the files license.txt / license.rst. it is
* also available through the world wide web at this url:
* http://opensource.org/licenses/osl-3.0
* if you did not receive a copy of the license and are unable to obtain it
* through the world wide web, please send an email to
* licensing@ellislab.com so we can send you a copy immediately.
*
* @package codeigniter
* @author ellislab dev team
* @copyright copyright (c) 2008 - 2014, ellislab, inc. (http://ellislab.com/)
* @license http://opensource.org/licenses/osl-3.0 open software license (osl 3.0)
* @link http://codeigniter.com
* @since version 3.0
* @filesource
*/
defined('basepath') or exit('no direct script access allowed');
/**
* codeigniter redis caching class
*
* @package codeigniter
* @subpackage libraries
* @category core
* @author anton lindqvist <anton@qvister.se>
* @link
*/
class ci_cache_redis extends ci_driver
{
/**
* default config
*
* @static
* @var array
*/
protected static $_default_config = array(
/*
'socket_type' => 'tcp',
'host' => '127.0.0.1',
'password' => null,
'port' => 6379,
'timeout' => 0
*/
);
/**
* redis connection
*
* @var redis
*/
protected $_redis;
/**
* get cache
*
* @param string like *$key*
* @return array(hash)
*/
public function keys($key)
{
return $this->_redis->keys($key);
}
/**
* get cache
*
* @param string cache id
* @return mixed
*/
public function get($key)
{
return $this->_redis->get($key);
}
/**
* mget cache
*
* @param array cache id array
* @return mixed
*/
public function mget($keys)
{
return $this->_redis->mget($keys);
}
/**
* save cache
*
* @param string $id cache id
* @param mixed $data data to save
* @param int $ttl time to live in seconds
* @param bool $raw whether to store the raw value (unused)
* @return bool true on success, false on failure
*/
public function save($id, $data, $ttl = 60, $raw = false)
{
return ($ttl)
? $this->_redis->setex($id, $ttl, $data)
: $this->_redis->set($id, $data);
}
/**
* delete from cache
*
* @param string cache key
* @return bool
*/
public function delete($key)
{
return ($this->_redis->delete($key) === 1);
}
/**
* hincrby a raw value
*
* @param string $id cache id
* @param string $field cache id
* @param int $offset step/value to add
* @return mixed new value on success or false on failure
*/
public function hincrby($key, $field, $value = 1)
{
return $this->_redis->hincrby($key, $field, $value);
}
/**
* hincrbyfloat a raw value
*
* @param string $id cache id
* @param string $field cache id
* @param int $offset step/value to add
* @return mixed new value on success or false on failure
*/
public function hincrbyfloat($key, $field, $value = 1)
{
return $this->_redis->hincrbyfloat($key, $field, $value);
}
/**
* lpush a raw value
*
* @param string $key cache id
* @param string $value value
* @return mixed new value on success or false on failure
*/
public function lpush($key, $value)
{
return $this->_redis->lpush($key, $value);
}
/**
* rpush a raw value
*
* @param string $key cache id
* @param string $value value
* @return mixed new value on success or false on failure
*/
public function rpush($key, $value)
{
return $this->_redis->rpush($key, $value);
}
/**
* rpop a raw value
*
* @param string $key cache id
* @param string $value value
* @return mixed new value on success or false on failure
*/
public function rpop($key)
{
return $this->_redis->rpop($key);
}
/**
* brpop a raw value
*
* @param string $key cache id
* @param string $ontime 阻塞等待时间
* @return mixed new value on success or false on failure
*/
public function brpop($key,$ontime=0)
{
return $this->_redis->brpop($key,$ontime);
}
/**
* llen a raw value
*
* @param string $key cache id
* @return mixed value on success or false on failure
*/
public function llen($key)
{
return $this->_redis->llen($key);
}
/**
* increment a raw value
*
* @param string $id cache id
* @param int $offset step/value to add
* @return mixed new value on success or false on failure
*/
public function increment($id, $offset = 1)
{
return $this->_redis->exists($id)
? $this->_redis->incr($id, $offset)
: false;
}
/**
* incrby a raw value
*
* @param string $key cache id
* @param int $offset step/value to add
* @return mixed new value on success or false on failure
*/
public function incrby($key, $value = 1)
{
return $this->_redis->incrby($key, $value);
}
/**
* set a value expire time
*
* @param string $key cache id
* @param int $seconds expire seconds
* @return mixed new value on success or false on failure
*/
public function expire($key, $seconds)
{
return $this->_redis->expire($key, $seconds);
}
/**
* increment a raw value
*
* @param string $id cache id
* @param int $offset step/value to add
* @return mixed new value on success or false on failure
*/
public function hset($alias,$key, $value)
{
return $this->_redis->hset($alias,$key, $value);
}
/**
* increment a raw value
*
* @param string $id cache id
* @param int $offset step/value to add
* @return mixed new value on success or false on failure
*/
public function hget($alias,$key)
{
return $this->_redis->hget($alias,$key);
}
/**
* increment a raw value
*
* @param string $id cache id
* @return mixed new value on success or false on failure
*/
public function hkeys($alias)
{
return $this->_redis->hkeys($alias);
}
/**
* increment a raw value
*
* @param string $id cache id
* @param int $offset step/value to add
* @return mixed new value on success or false on failure
*/
public function hgetall($alias)
{
return $this->_redis->hgetall($alias);
}
/**
* increment a raw value
*
* @param string $id cache id
* @param int $offset step/value to add
* @return mixed new value on success or false on failure
*/
public function hmget($alias,$key)
{
return $this->_redis->hmget($alias,$key);
}
/**
* del a key value
*
* @param string $id cache id
* @param int $offset step/value to add
* @return mixed new value on success or false on failure
*/
public function hdel($alias,$key)
{
return $this->_redis->hdel($alias,$key);
}
/**
* del a key value
*
* @param string $id cache id
* @return mixed new value on success or false on failure
*/
public function hvals($alias)
{
return $this->_redis->hvals($alias);
}
/**
* increment a raw value
*
* @param string $id cache id
* @param int $offset step/value to add
* @return mixed new value on success or false on failure
*/
public function hmset($alias,$array)
{
return $this->_redis->hmset($alias,$array);
}
/**
* decrement a raw value
*
* @param string $id cache id
* @param int $offset step/value to reduce by
* @return mixed new value on success or false on failure
*/
public function decrement($id, $offset = 1)
{
return $this->_redis->exists($id)
? $this->_redis->decr($id, $offset)
: false;
}
/**
* clean cache
*
* @return bool
* @see redis::flushdb()
*/
public function clean()
{
return $this->_redis->flushdb();
}
/**
* get cache driver info
*
* @param string not supported in redis.
* only included in order to offer a
* consistent cache api.
* @return array
* @see redis::info()
*/
public function cache_info($type = null)
{
return $this->_redis->info();
}
/**
* get cache metadata
*
* @param string cache key
* @return array
*/
public function get_metadata($key)
{
$value = $this->get($key);
if ($value)
{
return array(
'expire' => time() + $this->_redis->ttl($key),
'data' => $value
);
}
return false;
}
/**
* check if redis driver is supported
*
* @return bool
*/
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;
}
}
/**
* setup redis config and connection
*
* loads redis config file if present. will halt execution
* if a redis connection can't be established.
*
* @return bool
* @see redis::connect()
*/
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;
}
/**
* class destructor
*
* closes the connection to redis if present.
*
* @return void
*/
public function __destruct()
{
if ($this->_redis)
{
$this->_redis->close();
}
}
}
/* end of file cache_redis.php */
/* location: ./system/libraries/cache/drivers/cache_redis.php */
更多关于codeigniter相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《php优秀开发框架总结》、《yii框架入门及常用技巧总结》、《php缓存技术总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于codeigniter框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!