Cakephp + Predis使用文档

Cakephp + Predis使用文档
1. 环境准备
a.Php环境
b.Capephp 环境
c.Predis文件
2. 使用说明
a. predis的URL:http://github.com/nrk/predis/
b. 和cakephp集成
取Predis_Compatibility.php、Predis.php、SharedConfigurations.php文件放置于cakephp/app/vendors/Predis目录下

文件修改:
<?php
require_once 'Predis.php';
define('REDIS_HOST', '127.0.0.1');
define('REDIS_PORT', 6379);

$single_server = array(
'host' => REDIS_HOST,
'port' => REDIS_PORT//,
// 'database' => 15
);

$multiple_servers = array(
array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 15,
'alias' => 'first',
),
array(
'host' => '127.0.0.1',
'port' => 6380,
'database' => 15,
'alias' => 'second',
),
);
?>


c. 新建Cakephp的components文件redis_client.php
<?php
App::import('Vendor', 'Predis_Client', array('file' =>'Predis'.DS.'SharedConfigurations.php'));

class RedisClientComponent extends Object {

// Predis info
var $redis = null;

//called before Controller::beforeFilter()
function initialize(&$controller, $settings = array()) {
// saving the controller reference for later use
$this->controller =& $controller;

//修改redis
$this->redis = new Predis_Client(array(
'host' => REDIS_HOST,
'port' => REDIS_PORT,
));

//$this->redis = new Predis_Client($single_server);

}

/**
* client add value
*/
function setValue( $key, $val ){
//不存在就添加
if(!$this->redis->exists($key)){
$this->redis->set($key, $val);
}else{
$this->redis->del($key);
$this->redis->set($key, $val);
}
}

/**
* client get value
*/
function getValue($key){
return $this->redis->get($key);
}

/**
* client delete value
*/
function del($key){
if($this->redis->exists($key)){
$this->redis->del($key);
}
}

//called after Controller::render()
function shutdown(&$controller) {
}

}
?>

d. Controller中使用
引入components:
public $components = array('RedisClient');


设置值:
$this->RedisClient->setValue($key, $val);


获得值:
$this->RedisClient->getValue($key);


删除值:
$this->RedisClient->del($key);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值