PHP-redis的配置及使用

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 = 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值