Redis 的配置和使用
在这里我主要介绍的配置为redis在 php 框架 Codeigniter 中的配置
1、Redis缓存:Redis是一个在内存中以键值形式存储数据的缓存,使用LRU缓存模式,要使用他,你需要先安装Redis服务器和phpredis扩展。(Redis的安装以及在linux系统下的命令操作我会在之后相继会给大家展出,这块主要针对在框架里的配置)
连接Redis服务器(已经下载好并加入php扩展的redis)的配置信息必须保存到application/config/redis.php文件中,可用参数有:
<?PHP
$config['socket_type'] = tcp;
$config['socket'] = '/var/run/redis.sock';
$config['host'] = '127.0.0.1';
$config['password'] = NULL;
$config['port'] = 6379;
$config['timeout'] = 0;
配置好后的简单使用:
<?PHP
$this->load->driver->('cache');
$this->cache->redis->save('test', '一个简单的测试');
echo $this->cache->redis->get('test');
在config文件里配置我们的使用的缓存类型,default配置:
<?php
$config['sess_driver'] = 'redis';
$config['session_cookie_name'] = 'ci_session';
$config['session_expiration'] = 0;
$config['sess_save_path'] = 'tcp://127.0.0.1:.....';//省略号为端口号;
$config['sess_match_ip'] = FALSE;
$config['session_time_to_update'] = 600;//session更新时间;
$config['sess_regenerate_destroy'] = TRUE;
2、自定义redis缓存驱动
创建 application\libraries\Cache\Cache.php文件:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class CI_Cache extends CI_Driver_Library {
/**
* Valid cache drivers
*
* @var array
*/
protected $valid_drivers = array(
'apc',
'apcu',
'dummy',
'file',
'memcached',
'redis',
'wincache'
);
/**
* Path of cache files (if file-based cache)
*
* @var string
*/
protected $_cache_path = NULL;
/**
* Reference to the driver
*
* @var mixed
*/
protected $_adapter = 'dummy';
/**
* Fallback driver
*
* @var string
*/
protected $_backup_driver = 'dummy';
/**
* Cache key prefix
*
* @var string
*/
public $key_prefix = '';
/**
* Constructor
*
* Initialize class properties based on the configuration array.
*
* @param array $config = array()
* @return void
*/
public function __construct($config = array())
{
isset($config['adapter']) && $this->_adapter = $config['adapter'];
isset($config['backup']) && $this->_backup_driver = $config['backup'];
isset($config['key_prefix']) && $this->key_prefix =