当前位置:首页 > CMS教程 > 其它CMS > 列表

CI框架中redis缓存相关操作文件示例代码

发布:smiling 来源: PHP粉丝网  添加日期:2021-08-06 10:47:04 浏览: 评论:0 

这篇文章主要介绍了CI框架中redis缓存相关操作文件,结合完整示例演示了CI框架redis缓存相关操作技巧,需要的朋友可以参考下。

本文实例讲述了CI框架中redis缓存相关操作文件,分享给大家供大家参考,具体如下:

redis缓存类文件位置:

'ci\system\libraries\Cache\drivers\Cache_redis.php'

  1. <?php 
  2. /** 
  3.  * CodeIgniter 
  4.  * 
  5.  * An open source application development framework for PHP 5.2.4 or newer 
  6.  * 
  7.  * NOTICE OF LICENSE 
  8.  * 
  9.  * Licensed under the Open Software License version 3.0 
  10.  * 
  11.  * This source file is subject to the Open Software License (OSL 3.0) that is 
  12.  * bundled with this package in the files license.txt / license.rst. It is 
  13.  * also available through the world wide web at this URL: 
  14.  * http://opensource.org/licenses/OSL-3.0 
  15.  * If you did not receive a copy of the license and are unable to obtain it 
  16.  * through the world wide web, please send an email to 
  17.  * licensing@ellislab.com so we can send you a copy immediately. 
  18.  * 
  19.  * @package   CodeIgniter 
  20.  * @author   EllisLab Dev Team 
  21.  * @copyright  Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) 
  22.  * @license   http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) 
  23.  * @link    http://codeigniter.com 
  24.  * @since    Version 3.0 
  25.  * @filesource 
  26.  */ 
  27. defined('BASEPATH') OR exit('No direct script access allowed'); 
  28. /** 
  29.  * CodeIgniter Redis Caching Class 
  30.  * 
  31.  * @package  CodeIgniter 
  32.  * @subpackage Libraries 
  33.  * @category  Core 
  34.  * @author   Anton Lindqvist <anton@qvister.se> 
  35.  * @link 
  36.  */ 
  37. class CI_Cache_redis extends CI_Driver 
  38. { 
  39.   /** 
  40.    * Default config 
  41.    * 
  42.    * @static 
  43.    * @var array 
  44.    */ 
  45.   protected static $_default_config = array( 
  46.     /* 
  47.     'socket_type' => 'tcp', 
  48.     'host' => '127.0.0.1', 
  49.     'password' => NULL, 
  50.     'port' => 6379, 
  51.     'timeout' => 0 
  52.     */ 
  53.   ); 
  54.   /** 
  55.    * Redis connection 
  56.    * 
  57.    * @var Redis 
  58.    */ 
  59.   protected $_redis; 
  60.   /** 
  61.    * Get cache 
  62.    * 
  63.    * @param  string like *$key* 
  64.    * @return array(hash) 
  65.    */ 
  66.   public function keys($key) 
  67.   { 
  68.     return $this->_redis->keys($key); 
  69.   } 
  70.   /** 
  71.    * Get cache 
  72.    * 
  73.    * @param  string Cache ID 
  74.    * @return mixed 
  75.    */ 
  76.   public function get($key) 
  77.   { 
  78.     return $this->_redis->get($key); 
  79.   } 
  80.   /** 
  81.    * mGet cache 
  82.    * 
  83.    * @param  array  Cache ID Array 
  84.    * @return mixed 
  85.    */ 
  86.   public function mget($keys) 
  87.   { 
  88.     return $this->_redis->mget($keys); 
  89.   } 
  90.   /** 
  91.    * Save cache 
  92.    * 
  93.    * @param  string $id Cache ID 
  94.    * @param  mixed  $data  Data to save 
  95.    * @param  int $ttl  Time to live in seconds 
  96.    * @param  bool  $raw  Whether to store the raw value (unused) 
  97.    * @return bool  TRUE on success, FALSE on failure 
  98.    */ 
  99.   public function save($id, $data, $ttl = 60, $raw = FALSE) 
  100.   { 
  101.     return ($ttl) 
  102.       ? $this->_redis->setex($id, $ttl, $data) 
  103.       : $this->_redis->set($id, $data); 
  104.   } 
  105.   /** 
  106.    * Delete from cache 
  107.    * 
  108.    * @param  string Cache key 
  109.    * @return bool 
  110.    */ 
  111.   public function delete($key) 
  112.   { 
  113.     return ($this->_redis->delete($key) === 1); 
  114.   } 
  115.   /** 
  116.    * hIncrBy a raw value 
  117.    * 
  118.    * @param  string $id Cache ID 
  119.    * @param  string $field Cache ID 
  120.    * @param  int $offset Step/value to add 
  121.    * @return mixed  New value on success or FALSE on failure 
  122.    */ 
  123.   public function hincrby($key, $field, $value = 1) 
  124.   { 
  125.     return $this->_redis->hIncrBy($key, $field, $value); 
  126.   } 
  127.   /** 
  128.    * hIncrByFloat a raw value 
  129.    * 
  130.    * @param  string $id Cache ID 
  131.    * @param  string $field Cache ID 
  132.    * @param  int $offset Step/value to add 
  133.    * @return mixed  New value on success or FALSE on failure 
  134.    */ 
  135.   public function hincrbyfloat($key, $field, $value = 1) 
  136.   { 
  137.     return $this->_redis->hIncrByFloat($key, $field, $value); 
  138.   } 
  139.   /** 
  140.    * lpush a raw value 
  141.    * 
  142.    * @param  string $key  Cache ID 
  143.    * @param  string $value value 
  144.    * @return mixed  New value on success or FALSE on failure 
  145.    */ 
  146.   public function lpush($key, $value) 
  147.   { 
  148.     return $this->_redis->lPush($key, $value); 
  149.   } 
  150.    /** 
  151.    * rpush a raw value 
  152.    * 
  153.    * @param  string $key  Cache ID 
  154.    * @param  string $value value 
  155.    * @return mixed  New value on success or FALSE on failure 
  156.    */ 
  157.   public function rpush($key, $value) 
  158.   { 
  159.     return $this->_redis->rPush($key, $value); 
  160.   } 
  161.   /** 
  162.    * rpop a raw value 
  163.    * 
  164.    * @param  string $key  Cache ID 
  165.    * @param  string $value value 
  166.    * @return mixed  New value on success or FALSE on failure 
  167.    */ 
  168.   public function rpop($key) 
  169.   { 
  170.     return $this->_redis->rPop($key); 
  171.   } 
  172.    /** 
  173.    * brpop a raw value 
  174.    * 
  175.    * @param  string $key  Cache ID 
  176.    * @param  string $ontime 阻塞等待时间 
  177.    * @return mixed  New value on success or FALSE on failure 
  178.    */ 
  179.   public function brpop($key,$ontime=0) 
  180.   { 
  181.     return $this->_redis->brPop($key,$ontime); 
  182.   } 
  183.   /** 
  184.    * lLen a raw value 
  185.    * 
  186.    * @param  string $key  Cache ID 
  187.    * @return mixed  Value on success or FALSE on failure 
  188.    */ 
  189.   public function llen($key) 
  190.   { 
  191.     return $this->_redis->lLen($key); 
  192.   } 
  193.   /** 
  194.    * Increment a raw value 
  195.    * 
  196.    * @param  string $id Cache ID 
  197.    * @param  int $offset Step/value to add 
  198.    * @return mixed  New value on success or FALSE on failure 
  199.    */ 
  200.   public function increment($id, $offset = 1) 
  201.   { 
  202.     return $this->_redis->exists($id) 
  203.       ? $this->_redis->incr($id, $offset) 
  204.       : FALSE; 
  205.   } 
  206.   /** 
  207.    * incrby a raw value 
  208.    * 
  209.    * @param  string $key Cache ID 
  210.    * @param  int $offset Step/value to add 
  211.    * @return mixed  New value on success or FALSE on failure 
  212.    */ 
  213.   public function incrby($key, $value = 1) 
  214.   { 
  215.     return $this->_redis->incrby($key, $value); 
  216.   } 
  217.   /** 
  218.    * set a value expire time 
  219.    * 
  220.    * @param  string $key Cache ID 
  221.    * @param  int $seconds expire seconds 
  222.    * @return mixed  New value on success or FALSE on failure 
  223.    */ 
  224.   public function expire($key, $seconds) 
  225.   { 
  226.     return $this->_redis->expire($key, $seconds); 
  227.   } 
  228.   /** 
  229.    * Increment a raw value 
  230.    * 
  231.    * @param  string $id Cache ID 
  232.    * @param  int $offset Step/value to add 
  233.    * @return mixed  New value on success or FALSE on failure 
  234.    */ 
  235.   public function hset($alias,$key, $value) 
  236.   { 
  237.     return $this->_redis->hset($alias,$key, $value); 
  238.   } 
  239.   /** 
  240.    * Increment a raw value 
  241.    * 
  242.    * @param  string $id Cache ID 
  243.    * @param  int $offset Step/value to add 
  244.    * @return mixed  New value on success or FALSE on failure 
  245.    */ 
  246.   public function hget($alias,$key) 
  247.   { 
  248.     return $this->_redis->hget($alias,$key); 
  249.   } 
  250.   /** 
  251.    * Increment a raw value 
  252.    * 
  253.    * @param  string $id Cache ID 
  254.    * @return mixed  New value on success or FALSE on failure 
  255.    */ 
  256.   public function hkeys($alias) 
  257.   { 
  258.     return $this->_redis->hkeys($alias); 
  259.   } 
  260.   /** 
  261.    * Increment a raw value 
  262.    * 
  263.    * @param  string $id Cache ID 
  264.    * @param  int $offset Step/value to add 
  265.    * @return mixed  New value on success or FALSE on failure 
  266.    */ 
  267.   public function hgetall($alias) 
  268.   { 
  269.     return $this->_redis->hgetall($alias); 
  270.   } 
  271.   /** 
  272.    * Increment a raw value 
  273.    * 
  274.    * @param  string $id Cache ID 
  275.    * @param  int $offset Step/value to add 
  276.    * @return mixed  New value on success or FALSE on failure 
  277.    */ 
  278.   public function hmget($alias,$key) 
  279.   { 
  280.     return $this->_redis->hmget($alias,$key); 
  281.   } 
  282.   /** 
  283.    * del a key value 
  284.    * 
  285.    * @param  string $id Cache ID 
  286.    * @param  int $offset Step/value to add 
  287.    * @return mixed  New value on success or FALSE on failure 
  288.    */ 
  289.   public function hdel($alias,$key) 
  290.   { 
  291.     return $this->_redis->hdel($alias,$key); 
  292.   } 
  293.   /** 
  294.    * del a key value 
  295.    * 
  296.    * @param  string $id Cache ID 
  297.    * @return mixed  New value on success or FALSE on failure 
  298.    */ 
  299.   public function hvals($alias) 
  300.   { 
  301.     return $this->_redis->hvals($alias); 
  302.   } 
  303.   /** 
  304.    * Increment a raw value 
  305.    * 
  306.    * @param  string $id Cache ID 
  307.    * @param  int $offset Step/value to add 
  308.    * @return mixed  New value on success or FALSE on failure 
  309.    */ 
  310.   public function hmset($alias,$array) 
  311.   { 
  312.     return $this->_redis->hmset($alias,$array); 
  313.   } 
  314.   /** 
  315.    * Decrement a raw value 
  316.    * 
  317.    * @param  string $id Cache ID 
  318.    * @param  int $offset Step/value to reduce by 
  319.    * @return mixed  New value on success or FALSE on failure 
  320.    */ 
  321.   public function decrement($id, $offset = 1) 
  322.   { 
  323.     return $this->_redis->exists($id) 
  324.       ? $this->_redis->decr($id, $offset) 
  325.       : FALSE; 
  326.   } 
  327.   /** 
  328.    * Clean cache 
  329.    * 
  330.    * @return bool 
  331.    * @see   Redis::flushDB() 
  332.    */ 
  333.   public function clean() 
  334.   { 
  335.     return $this->_redis->flushDB(); 
  336.   } 
  337.   /** 
  338.    * Get cache driver info 
  339.    * 
  340.    * @param  string Not supported in Redis. 
  341.    *     Only included in order to offer a 
  342.    *     consistent cache API. 
  343.    * @return array 
  344.    * @see   Redis::info() 
  345.    */ 
  346.   public function cache_info($type = NULL) 
  347.   { 
  348.     return $this->_redis->info(); 
  349.   } 
  350.   /** 
  351.    * Get cache metadata 
  352.    * 
  353.    * @param  string Cache key 
  354.    * @return array 
  355.    */ 
  356.   public function get_metadata($key) 
  357.   { 
  358.     $value = $this->get($key); 
  359.     if ($value) 
  360.     { 
  361.       return array( 
  362.         'expire' => time() + $this->_redis->ttl($key), 
  363.         'data' => $value 
  364.       ); 
  365.     } 
  366.     return FALSE; 
  367.   } 
  368.   /** 
  369.    * Check if Redis driver is supported 
  370.    * 
  371.    * @return bool 
  372.    */ 
  373.   public function is_supported() 
  374.   { 
  375.     if (extension_loaded('redis')) 
  376.     { 
  377.       return $this->_setup_redis(); 
  378.     } 
  379.     else 
  380.     { 
  381.       log_message('debug', 'The Redis extension must be loaded to use Redis cache.'); 
  382.       return FALSE; 
  383.     } 
  384.   } 
  385.   /** 
  386.    * Setup Redis config and connection 
  387.    * 
  388.    * Loads Redis config file if present. Will halt execution 
  389.    * if a Redis connection can't be established. 
  390.    * 
  391.    * @return bool 
  392.    * @see   Redis::connect() 
  393.    */ 
  394.   protected function _setup_redis() 
  395.   { 
  396.     $config = array(); 
  397.     $CI =& get_instance(); 
  398.     if ($CI->config->load('redis', TRUE, TRUE)) 
  399.     { 
  400.       $config += $CI->config->item('redis'); 
  401.     } 
  402.     $config = array_merge(self::$_default_config, $config); 
  403.     $config = !emptyempty($config['redis'])?$config['redis']:$config; 
  404.     $this->_redis = new Redis(); 
  405.     try 
  406.     { 
  407.       if ($config['socket_type'] === 'unix') 
  408.       { 
  409.         $success = $this->_redis->connect($config['socket']); 
  410.       } 
  411.       else // tcp socket 
  412.       { 
  413.         $success = $this->_redis->connect($config['host'], $config['port'], $config['timeout']); 
  414.       } 
  415.       if ( ! $success) 
  416.       { 
  417.         log_message('debug', 'Cache: Redis connection refused. Check the config.'); 
  418.         return FALSE; 
  419.       } 
  420.     } 
  421.     catch (RedisException $e) 
  422.     { 
  423.       log_message('debug', 'Cache: Redis connection refused ('.$e->getMessage().')'); 
  424.       return FALSE; 
  425.     } 
  426.     if (isset($config['password'])) 
  427.     { 
  428.       $this->_redis->auth($config['password']); 
  429.     } 
  430.     return TRUE; 
  431.   } 
  432.   /** 
  433.    * Class destructor 
  434.    * 
  435.    * Closes the connection to Redis if present. 
  436.    * 
  437.    * @return void 
  438.    */ 
  439.   public function __destruct() 
  440.   { 
  441.     if ($this->_redis) 
  442.     { 
  443.       $this->_redis->close(); 
  444.     } 
  445.   } 
  446. } 
  447. /* End of file Cache_redis.php */ 
  448. /* Location: ./system/libraries/Cache/drivers/Cache_redis.php */

Tags: CI框架 redis

分享到: