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);

你可能感兴趣的:(redis,C++,c,PHP,cakephp)