亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 數據庫 > Redis > 正文

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

2020-03-22 20:15:30
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了CI框架(CodeIgniter)操作redis的方法,結合實例形式詳細分析了CodeIgniter框架針對redis數據庫操作的相關配置與使用技巧,需要的朋友可以參考下

本文實例講述了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.htmlclass 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庫函數功能與用法可參考本站//www.jb51.net/article/33887.htm

需要注意的是:

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

您可能感興趣的文章:

php使用imagecopymerge()函數創建半透明水印的詳解

一個中高級PHP工程師所應該具備的能力

php實現mysql連接池效果實現代碼的詳解

以上就是CI框架(CodeIgniter)操作redis的方法的詳解的詳細內容,PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲天堂av高清| 91高清免费在线观看| 欧美在线性视频| 自拍偷拍亚洲在线| 亚洲女成人图区| 国产日韩av高清| 亚洲一区二区在线| 亚洲午夜精品久久久久久性色| 免费av一区二区| 92看片淫黄大片看国产片| 亚洲黄色免费三级| 日韩中文字幕不卡视频| 992tv在线成人免费观看| 久久久精品国产一区二区| 91国在线精品国内播放| 国产v综合v亚洲欧美久久| 隔壁老王国产在线精品| 亚洲欧美成人网| 欧美日韩国产中文字幕| 97超碰蝌蚪网人人做人人爽| 国产日产亚洲精品| 亚洲午夜未删减在线观看| 久久综合九色九九| 亚洲欧美国产精品专区久久| 日韩中文av在线| 免费成人高清视频| 久久电影一区二区| 操91在线视频| 中文在线资源观看视频网站免费不卡| …久久精品99久久香蕉国产| 久久精品国产欧美激情| 久久精品视频导航| 6080yy精品一区二区三区| 日日狠狠久久偷偷四色综合免费| 欧美极品少妇与黑人| 亚洲精品成人久久| 亚洲精品国产精品国自产在线| 亚洲欧美日韩一区二区在线| 欧美日韩激情小视频| 久久精品中文字幕| 国产美女搞久久| 久久91精品国产91久久跳| 伊人成人开心激情综合网| 亚洲第一精品久久忘忧草社区| 亚洲大胆人体在线| 国产一区二区三区在线观看视频| 中文字幕最新精品| 日韩av综合中文字幕| 欧美猛男性生活免费| 欧美有码在线视频| 国产精品第二页| 欧美黑人巨大精品一区二区| 国产精品白丝av嫩草影院| 亚洲a区在线视频| 亚洲男女性事视频| 中文字幕在线亚洲| 精品久久久久久国产| 91高清免费在线观看| 欧美电影免费观看大全| 日韩电影在线观看永久视频免费网站| 精品呦交小u女在线| 国产主播欧美精品| 国产欧美日韩精品丝袜高跟鞋| 亚洲加勒比久久88色综合| 久久在线视频在线| 国产精品九九久久久久久久| 欧美一区亚洲一区| 国产精品极品美女粉嫩高清在线| 亚洲色图欧美制服丝袜另类第一页| 中文字幕在线看视频国产欧美在线看完整| 国产婷婷97碰碰久久人人蜜臀| 美女福利精品视频| 久久久久九九九九| 97婷婷涩涩精品一区| 国产精品久久久久久影视| 亚洲高清久久网| 亚洲在线观看视频网站| 久久免费高清视频| 久久国产精彩视频| 日韩中文有码在线视频| 欧洲亚洲免费在线| 亚洲精品免费在线视频| 成人在线视频网站| 国产精品九九久久久久久久| 成人日韩在线电影| 欧美精品videofree1080p| 亚洲男人的天堂在线播放| 日韩在线播放av| 亚洲激情免费观看| 亚洲国产欧美一区二区三区同亚洲| 欧美成年人视频网站欧美| 国产91免费看片| 欧美激情一区二区三区成人| 亚洲欧美一区二区激情| 欧美久久精品一级黑人c片| 国产精品91在线| 九九热这里只有在线精品视| 在线观看日韩www视频免费| 国产男女猛烈无遮挡91| 国内精品小视频| 成人精品视频久久久久| 欧美人与性动交| 亚洲女成人图区| 欧美一级大片在线观看| 91精品国产91久久久久久吃药| 亚洲欧美综合精品久久成人| 午夜精品久久久久久久男人的天堂| 亚洲国产精品久久精品怡红院| 久久久精品一区二区| 久热精品在线视频| 久久久精品电影| www日韩中文字幕在线看| 欧美国产精品va在线观看| 欧美亚洲国产精品| 欧美午夜激情小视频| 亚洲人成在线播放| 狠狠色狠狠色综合日日小说| www.亚洲一二| 亚洲欧美综合图区| 91免费精品视频| 黑人极品videos精品欧美裸| 精品久久久中文| 亚洲iv一区二区三区| 亚洲成人黄色在线| 亚洲第一区第一页| 2019中文字幕在线免费观看| 国产亚洲欧美一区| 2019av中文字幕| 久久久黄色av| 68精品久久久久久欧美| 91亚洲精品在线| 精品国产91乱高清在线观看| 日本午夜在线亚洲.国产| 日韩毛片在线观看| 91精品久久久久久久久久久| 亚洲欧美制服综合另类| 亚洲福利视频在线| 国产精品视频yy9099| 亚洲变态欧美另类捆绑| 欧美黑人又粗大| 亚洲丁香婷深爱综合| 欧美精品videos性欧美| 亚洲免费成人av电影| 亚洲天堂一区二区三区| 成人欧美一区二区三区黑人| 国产欧美日韩丝袜精品一区| 不用播放器成人网| 久久精品视频播放| 欧美日韩一区免费| 热久久99这里有精品| 精品欧美aⅴ在线网站| 久久高清视频免费| 清纯唯美日韩制服另类| 成人免费视频xnxx.com| 亚洲激情视频在线观看| 欧美肥臀大乳一区二区免费视频| 永久免费毛片在线播放不卡| 亚洲在线视频福利| 国产69精品久久久久9999| 国产综合视频在线观看| 亚洲国产精品中文| 亚洲午夜小视频| 中文字幕在线看视频国产欧美| 国产精品jvid在线观看蜜臀|