校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > CI框架(CodeIgniter)操作redis的方法詳解

CI框架(CodeIgniter)操作redis的方法詳解

熱門標簽:400電話辦理福州市 離石地圖標注 南寧高頻外呼回撥系統哪家好 江蘇外呼電銷機器人報價 深圳外呼系統收費 長沙crm外呼系統業務 電話機器人危險嗎 專業電話機器人批發商 400電話申請方法收費

本文實例講述了CI框架(CodeIgniter)操作redis的方法。分享給大家供大家參考,具體如下:

1. 在autoload.php 中加入 如下配置行

$autoload['libraries'] = array('redis');

2. 在/application/config 中加入文件 redis.php

文件內容如下:

?php
// Default connection group
$config['redis_default']['host'] = 'localhost';   // IP address or host
$config['redis_default']['port'] = '6379';     // Default Redis port is 6379
$config['redis_default']['password'] = '';     // Can be left empty when the server does not require AUTH
$config['redis_slave']['host'] = '';
$config['redis_slave']['port'] = '6379';
$config['redis_slave']['password'] = '';
?>

3. 在 /application/libraries 中加入文件 Redis.php

文件來源:redis庫文件包

文件內容:

?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * CodeIgniter Redis
 *
 * A CodeIgniter library to interact with Redis
 *
 * @package     CodeIgniter
 * @category    Libraries
 * @author     Joël Cox
 * @version     v0.4
 * @link      https://github.com/joelcox/codeigniter-redis
 * @link      http://joelcox.nl
 * @license     http://www.opensource.org/licenses/mit-license.html
 */
class CI_Redis {
  /**
   * CI
   *
   * CodeIgniter instance
   * @var   object
   */
  private $_ci;
  /**
   * Connection
   *
   * Socket handle to the Redis server
   * @var   handle
   */
  private $_connection;
  /**
   * Debug
   *
   * Whether we're in debug mode
   * @var   bool
   */
  public $debug = FALSE;
  /**
   * CRLF
   *
   * User to delimiter arguments in the Redis unified request protocol
   * @var   string
   */
  const CRLF = "\r\n";
  /**
   * Constructor
   */
  public function __construct($params = array())
  {
    log_message('debug', 'Redis Class Initialized');
    $this->_ci = get_instance();
    $this->_ci->load->config('redis');
    // Check for the different styles of configs
    if (isset($params['connection_group']))
    {
      // Specific connection group
      $config = $this->_ci->config->item('redis_' . $params['connection_group']);
    }
    elseif (is_array($this->_ci->config->item('redis_default')))
    {
      // Default connection group
      $config = $this->_ci->config->item('redis_default');
    }
    else
    {
      // Original config style
      $config = array(
        'host' => $this->_ci->config->item('redis_host'),
        'port' => $this->_ci->config->item('redis_port'),
        'password' => $this->_ci->config->item('redis_password'),
      );
    }
    // Connect to Redis
    $this->_connection = @fsockopen($config['host'], $config['port'], $errno, $errstr, 3);
    // Display an error message if connection failed
    if ( ! $this->_connection)
    {
      show_error('Could not connect to Redis at ' . $config['host'] . ':' . $config['port']);
    }
    // Authenticate when needed
    $this->_auth($config['password']);
  }
  /**
   * Call
   *
   * Catches all undefined methods
   * @param  string method that was called
   * @param  mixed  arguments that were passed
   * @return mixed
   */
  public function __call($method, $arguments)
  {
    $request = $this->_encode_request($method, $arguments);
    return $this->_write_request($request);
  }
  /**
   * Command
   *
   * Generic command function, just like redis-cli
   * @param  string full command as a string
   * @return mixed
   */
  public function command($string)
  {
    $slices = explode(' ', $string);
    $request = $this->_encode_request($slices[0], array_slice($slices, 1));
    return $this->_write_request($request);
  }
  /**
   * Auth
   *
   * Runs the AUTH command when password is set
   * @param  string password for the Redis server
   * @return void
   */
  private function _auth($password = NULL)
  {
    // Authenticate when password is set
    if ( ! empty($password))
    {
      // See if we authenticated successfully
      if ($this->command('AUTH ' . $password) !== 'OK')
      {
        show_error('Could not connect to Redis, invalid password');
      }
    }
  }
  /**
   * Clear Socket
   *
   * Empty the socket buffer of theconnection so data does not bleed over
   * to the next message.
   * @return NULL
   */
  public function _clear_socket()
  {
    // Read one character at a time
    fflush($this->_connection);
    return NULL;
  }
  /**
   * Write request
   *
   * Write the formatted request to the socket
   * @param  string request to be written
   * @return mixed
   */
  private function _write_request($request)
  {
    if ($this->debug === TRUE)
    {
      log_message('debug', 'Redis unified request: ' . $request);
    }
    // How long is the data we are sending?
    $value_length = strlen($request);
    // If there isn't any data, just return
    if ($value_length = 0) return NULL;
    // Handle reply if data is less than or equal to 8192 bytes, just send it over
    if ($value_length = 8192)
    {
      fwrite($this->_connection, $request);
    }
    else
    {
      while ($value_length > 0)
      {
        // If we have more than 8192, only take what we can handle
        if ($value_length > 8192) {
          $send_size = 8192;
        }
        // Send our chunk
        fwrite($this->_connection, $request, $send_size);
        // How much is left to send?
        $value_length = $value_length - $send_size;
        // Remove data sent from outgoing data
        $request = substr($request, $send_size, $value_length);
      }
    }
    // Read our request into a variable
    $return = $this->_read_request();
    // Clear the socket so no data remains in the buffer
    $this->_clear_socket();
    return $return;
  }
  /**
   * Read request
   *
   * Route each response to the appropriate interpreter
   * @return mixed
   */
  private function _read_request()
  {
    $type = fgetc($this->_connection);
    // Times we will attempt to trash bad data in search of a
    // valid type indicator
    $response_types = array('+', '-', ':', '$', '*');
    $type_error_limit = 50;
    $try = 0;
    while ( ! in_array($type, $response_types)  $try  $type_error_limit)
    {
      $type = fgetc($this->_connection);
      $try++;
    }
    if ($this->debug === TRUE)
    {
      log_message('debug', 'Redis response type: ' . $type);
    }
    switch ($type)
    {
      case '+':
        return $this->_single_line_reply();
        break;
      case '-':
        return $this->_error_reply();
        break;
      case ':':
        return $this->_integer_reply();
        break;
      case '$':
        return $this->_bulk_reply();
        break;
      case '*':
        return $this->_multi_bulk_reply();
        break;
      default:
        return FALSE;
    }
  }
  /**
   * Single line reply
   *
   * Reads the reply before the EOF
   * @return mixed
   */
  private function _single_line_reply()
  {
    $value = rtrim(fgets($this->_connection));
    $this->_clear_socket();
    return $value;
  }
  /**
   * Error reply
   *
   * Write error to log and return false
   * @return bool
   */
  private function _error_reply()
  {
    // Extract the error message
    $error = substr(rtrim(fgets($this->_connection)), 4);
    log_message('error', 'Redis server returned an error: ' . $error);
    $this->_clear_socket();
    return FALSE;
  }
  /**
   * Integer reply
   *
   * Returns an integer reply
   * @return int
   */
  private function _integer_reply()
  {
    return (int) rtrim(fgets($this->_connection));
  }
  /**
   * Bulk reply
   *
   * Reads to amount of bits to be read and returns value within
   * the pointer and the ending delimiter
   * @return string
   */
  private function _bulk_reply()
  {
    // How long is the data we are reading? Support waiting for data to
    // fully return from redis and enter into socket.
    $value_length = (int) fgets($this->_connection);
    if ($value_length = 0) return NULL;
    $response = '';
    // Handle reply if data is less than or equal to 8192 bytes, just read it
    if ($value_length = 8192)
    {
      $response = fread($this->_connection, $value_length);
    }
    else
    {
      $data_left = $value_length;
        // If the data left is greater than 0, keep reading
        while ($data_left > 0 ) {
        // If we have more than 8192, only take what we can handle
        if ($data_left > 8192)
        {
          $read_size = 8192;
        }
        else
        {
          $read_size = $data_left;
        }
        // Read our chunk
        $chunk = fread($this->_connection, $read_size);
        // Support reading very long responses that don't come through
        // in one fread
        $chunk_length = strlen($chunk);
        while ($chunk_length  $read_size)
        {
          $keep_reading = $read_size - $chunk_length;
          $chunk .= fread($this->_connection, $keep_reading);
          $chunk_length = strlen($chunk);
        }
        $response .= $chunk;
        // Re-calculate how much data is left to read
        $data_left = $data_left - $read_size;
      }
    }
    // Clear the socket in case anything remains in there
    $this->_clear_socket();
  return isset($response) ? $response : FALSE;
  }
  /**
   * Multi bulk reply
   *
   * Reads n bulk replies and return them as an array
   * @return array
   */
  private function _multi_bulk_reply()
  {
    // Get the amount of values in the response
    $response = array();
    $total_values = (int) fgets($this->_connection);
    // Loop all values and add them to the response array
    for ($i = 0; $i  $total_values; $i++)
    {
      // Remove the new line and carriage return before reading
      // another bulk reply
      fgets($this->_connection, 2);
      // If this is a second or later pass, we also need to get rid
      // of the $ indicating a new bulk reply and its length.
      if ($i > 0)
      {
        fgets($this->_connection);
        fgets($this->_connection, 2);
      }
      $response[] = $this->_bulk_reply();
    }
    // Clear the socket
    $this->_clear_socket();
    return isset($response) ? $response : FALSE;
  }
  /**
   * Encode request
   *
   * Encode plain-text request to Redis protocol format
   * @link  http://redis.io/topics/protocol
   * @param  string request in plain-text
   * @param  string additional data (string or array, depending on the request)
   * @return string encoded according to Redis protocol
   */
  private function _encode_request($method, $arguments = array())
  {
    $request = '$' . strlen($method) . self::CRLF . $method . self::CRLF;
    $_args = 1;
    // Append all the arguments in the request string
    foreach ($arguments as $argument)
    {
      if (is_array($argument))
      {
        foreach ($argument as $key => $value)
        {
          // Prepend the key if we're dealing with a hash
          if (!is_int($key))
          {
            $request .= '$' . strlen($key) . self::CRLF . $key . self::CRLF;
            $_args++;
          }
          $request .= '$' . strlen($value) . self::CRLF . $value . self::CRLF;
          $_args++;
        }
      }
      else
      {
        $request .= '$' . strlen($argument) . self::CRLF . $argument . self::CRLF;
        $_args++;
      }
    }
    $request = '*' . $_args . self::CRLF . $request;
    return $request;
  }
  /**
   * Info
   *
   * Overrides the default Redis response, so we can return a nice array
   * of the server info instead of a nasty string.
   * @return array
   */
  public function info($section = FALSE)
  {
    if ($section !== FALSE)
    {
      $response = $this->command('INFO '. $section);
    }
    else
    {
      $response = $this->command('INFO');
    }
    $data = array();
    $lines = explode(self::CRLF, $response);
    // Extract the key and value
    foreach ($lines as $line)
    {
      $parts = explode(':', $line);
      if (isset($parts[1])) $data[$parts[0]] = $parts[1];
    }
    return $data;
  }
  /**
   * Debug
   *
   * Set debug mode
   * @param  bool  set the debug mode on or off
   * @return void
   */
  public function debug($bool)
  {
    $this->debug = (bool) $bool;
  }
  /**
   * Destructor
   *
   * Kill the connection
   * @return void
   */
  function __destruct()
  {
    if ($this->_connection) fclose($this->_connection);
  }
}
?>

4. 然后你就可以 在文件中這樣使用了

?php
  if($this->redis->get('mark_'.$gid) === null){ //如果未設置
    $this->redis->set('mark_'.$gid, $giftnum); //設置
    $this->redis->EXPIRE('mark_'.$gid, 30*60); //設置過期時間 (30 min)
  }else{
    $giftnum = $this->redis->get('mark_'.$gid); //從緩存中直接讀取對應的值
  }
?>

5. 重點是你所需要的 東東在這里很詳細的講解了

所有要用的函數只需要更改 $redis  ==> $this->redis

php中操作redis庫函數功能與用法可參考本站https://www.jb51.net/article/33887.htm

需要注意的是:

(1)你的本地需要安裝 redis服務(windows安裝)
(2)并開啟redis 服務
(3)不管是windows 還是linux 都需要裝 php對應版本的 redis擴展

更多關于CodeIgniter相關內容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php優秀開發框架總結》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結》、《Zend FrameWork框架入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家基于CodeIgniter框架的PHP程序設計有所幫助。

您可能感興趣的文章:
  • Thinkphp 3.2框架使用Redis的方法詳解
  • thinkPHP框架通過Redis實現增刪改查操作的方法詳解
  • thinkphp5框架擴展redis類方法示例
  • Spring Boot單元測試中使用mockito框架mock掉整個RedisTemplate的示例
  • Laravel框架使用Redis的方法詳解
  • Python的Flask框架使用Redis做數據緩存的配置方法
  • PHP的Laravel框架結合MySQL與Redis數據庫的使用部署
  • Redis框架Jedis及Redisson對比解析

標簽:南昌 白酒營銷 濱州 太原 南京 興安盟 曲靖 株洲

巨人網絡通訊聲明:本文標題《CI框架(CodeIgniter)操作redis的方法詳解》,本文關鍵詞  框架,CodeIgniter,操作,redis,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《CI框架(CodeIgniter)操作redis的方法詳解》相關的同類信息!
  • 本頁收集關于CI框架(CodeIgniter)操作redis的方法詳解的相關信息資訊供網民參考!
  • 推薦文章
    校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃
    欧美午夜片在线观看| 成人教育av在线| 国产精品成人在线观看| 国产女人aaa级久久久级 | 色一情一乱一乱一91av| 成人免费精品视频| 成人在线视频一区二区| 国产99久久精品| 成人黄色网址在线观看| 99精品一区二区| 在线观看日韩精品| 欧美酷刑日本凌虐凌虐| 欧美一区二区人人喊爽| 精品少妇一区二区三区免费观看 | 欧美日韩国产综合草草| 国产.精品.日韩.另类.中文.在线.播放| 国产成人小视频| 日本一区二区成人在线| 天天色综合天天| 欧美久久久久久久久中文字幕| 精品少妇一区二区三区在线视频| 亚洲国产va精品久久久不卡综合 | 欧美日韩国产大片| 亚洲欧美自拍偷拍色图| 国产一区二区三区在线观看免费视频 | 99精品黄色片免费大全| 国产精品乱子久久久久| 成人激情电影免费在线观看| 欧美色大人视频| 视频一区二区三区中文字幕| 欧美精品在线一区二区| 亚洲成a人v欧美综合天堂| 91国产视频在线观看| 午夜精品久久久久久久久久| 色94色欧美sute亚洲13| 亚洲五码中文字幕| 日韩一卡二卡三卡| 久久国产精品色婷婷| 精品毛片乱码1区2区3区| 久久精品国产网站| 国产精品无遮挡| 欧美日韩不卡视频| 狠狠色丁香婷婷综合| 亚洲天堂精品视频| 日韩三级在线观看| 成人午夜激情视频| 国产精品久线在线观看| 成人精品免费网站| 亚洲日韩欧美一区二区在线| 欧美最猛黑人xxxxx猛交| 最好看的中文字幕久久| 99精品久久免费看蜜臀剧情介绍 | 亚洲女爱视频在线| 在线观看不卡一区| 日本在线不卡一区| 久久嫩草精品久久久精品| 国产一区二区三区香蕉| 国产日韩精品久久久| 成人午夜在线播放| 亚洲国产aⅴ成人精品无吗| 久久久三级国产网站| 欧美三级日韩三级| 在线视频欧美精品| 97se亚洲国产综合在线| 亚洲大片一区二区三区| 成人欧美一区二区三区在线播放| 久久久亚洲精品石原莉奈| 91麻豆精品国产91久久久久久久久| 国产白丝精品91爽爽久久| 日韩在线一区二区三区| 亚洲国产cao| 夜夜嗨av一区二区三区| 国产精品乱码妇女bbbb| 精品88久久久久88久久久| 欧美精品aⅴ在线视频| 欧美专区在线观看一区| 91电影在线观看| 日本道免费精品一区二区三区| voyeur盗摄精品| 91浏览器入口在线观看| 一本色道综合亚洲| 欧美性大战久久久久久久| 欧美伊人精品成人久久综合97| 色婷婷av一区二区三区之一色屋| www.欧美精品一二区| 日本精品裸体写真集在线观看| 欧美亚州韩日在线看免费版国语版| 欧美性大战xxxxx久久久| 日韩一区二区在线观看视频 | 美女精品一区二区| 色香蕉久久蜜桃| 精品成人一区二区三区四区| 国产精品久久久久婷婷| 日韩精品每日更新| www.欧美色图| 久久精品男人天堂av| 亚洲第一精品在线| 国产在线看一区| 欧美电影免费观看高清完整版在线观看 | 日韩视频123| 亚洲欧洲日产国码二区| 久久精品噜噜噜成人88aⅴ| 97国产精品videossex| 欧美大尺度电影在线| 日韩伦理av电影| 麻豆免费看一区二区三区| 国产精品久久久久四虎| 亚洲免费伊人电影| 91丝袜美女网| 中文字幕中文乱码欧美一区二区| 欧美视频一区二| 亚洲精品视频免费看| 欧美日韩性生活| 日韩国产精品久久久| 欧美另类久久久品| 天天影视色香欲综合网老头| 欧美一区二区国产| 国产精品综合视频| 亚洲视频狠狠干| 91精品国产综合久久久久久久久久| 国内精品久久久久影院色| 国产一区二区电影| 亚洲精品成人在线| 国产偷国产偷亚洲高清人白洁 | 欧日韩精品视频| 337p亚洲精品色噜噜| 欧美日韩亚洲高清一区二区| 3d动漫精品啪啪一区二区竹菊| 欧美精品久久99久久在免费线| 日韩欧美中文字幕一区| 国产精品免费看片| 亚洲一区二区精品久久av| 久久婷婷久久一区二区三区| 国产精品色哟哟| 日韩精品每日更新| 一本一本大道香蕉久在线精品 | 国产午夜精品一区二区三区嫩草| 2023国产精品自拍| 一区二区三区四区激情| 欧美网站一区二区| 亚洲精品一区二区三区精华液| 国产一区二区看久久| 欧美经典三级视频一区二区三区| 亚洲午夜精品网| 成人看片黄a免费看在线| 国产精品亲子伦对白| 92精品国产成人观看免费| 中文字幕一区二区三区av| 成人精品国产免费网站| 久久久青草青青国产亚洲免观| 国产精品视频一二三| 狠狠色狠狠色综合| 国产精品系列在线观看| 在线亚洲人成电影网站色www| 欧美自拍偷拍一区| 一区二区三区欧美久久| 欧美一级二级三级蜜桃| 欧美一级黄色大片| 欧美私人免费视频| 久久99国产精品尤物| 欧美欧美欧美欧美| 亚洲欧美日韩人成在线播放| 国产精品77777竹菊影视小说| 日本丶国产丶欧美色综合| 亚洲一区二区三区三| 成人亚洲精品久久久久软件| 精品国产一区二区三区av性色| 五月婷婷欧美视频| 一本高清dvd不卡在线观看| 国产精品电影一区二区| 国产99精品国产| 久久伊人蜜桃av一区二区| 日韩不卡一区二区| 欧美日韩大陆一区二区| 亚洲国产精品久久人人爱蜜臀| 色婷婷亚洲婷婷| 亚洲色图.com| 色老汉一区二区三区| 国产精品久久久久久久久免费相片| 美女视频一区二区三区| 欧美一区二区三区四区在线观看 | 国产精品自拍一区| 欧美www视频| 成人精品视频一区二区三区| 国产色爱av资源综合区| 国产aⅴ综合色| 日本一区二区三区久久久久久久久不 | 在线日韩av片| 香蕉成人伊视频在线观看| 在线成人午夜影院| 日韩成人精品视频| 精品国产sm最大网站| 99久久精品国产一区二区三区| 亚洲人成小说网站色在线| 色偷偷久久人人79超碰人人澡| 一区二区三区日韩欧美精品| 欧美日韩一区高清| 日韩成人dvd| 精品美女一区二区三区| 国产91精品精华液一区二区三区|