PHPRedis operations
- /*************************************** **************************************** **
- * InitPHP 2.0 domestic php development framework Dao-Nosql-Redis
- *-------------------------------------------------------------------------------
- * CopyRight: CopyRight By initphp.com
- * You can use the source code freely, but keep the author information during use. Respecting Others means respecting yourself.
- *-------------------------------------------------------------------------------
- * $ Author: zhuli
- * $ Dtime: 2011-10-09
- **************************************** **************************************** ***/
- Class redisInit {
-
- Private $ redis; // redis object
-
- /**
- * Initialize Redis
- * $ Config = array (
- * 'Server' => '127. 0.0.1 'server
- * 'Port' => '123' port number
- *)
- * @ Param array $ config
- */
- Public function init ($ config = array ()){
- If ($ config ['server'] = '') $ config ['server'] = '2017. 0.0.1 ';
- If ($ config ['port'] = '') $ config ['port'] = '123 ';
- $ This-> redis = new Redis ();
- $ This-> redis-> connect ($ config ['server'], $ config ['port']);
- Return $ this-> redis;
- }
-
- /**
- * Set value
- * @ Param string $ key KEY name
- * @ Param string | the data obtained by array $ value
- * @ Param int $ timeOut time
- */
- Public function set ($ key, $ value, $ timeOut = 0 ){
- $ Value = json_encode ($ value, TRUE );
- $ RetRes = $ this-> redis-> set ($ key, $ value );
- If ($ timeOut> 0) $ this-> redis-> setTimeout ($ key, $ timeOut );
- Return $ retRes;
- }
-
- /**
- * Get data through KEY
- * @ Param string $ key KEY name
- */
- Public function get ($ key ){
- $ Result = $ this-> redis-> get ($ key );
- Return json_decode ($ result, TRUE );
- }
-
- /**
- * Delete a piece of data
- * @ Param string $ key KEY name
- */
- Public function delete ($ key ){
- Return $ this-> redis-> delete ($ key );
- }
-
- /**
- * Clear data
- */
- Public function flushAll (){
- Return $ this-> redis-> flushAll ();
- }
-
- /**
- * Data entry queue
- * @ Param string $ key KEY name
- * @ Param string | the data obtained by array $ value
- * @ Param bool $ right indicates whether to start from the right
- */
- Public function push ($ key, $ value, $ right = true ){
- $ Value = json_encode ($ value );
- Return $ right? $ This-> redis-> rPush ($ key, $ value): $ this-> redis-> lPush ($ key, $ value );
- }
-
- /**
- * Data output queue
- * @ Param string $ key KEY name
- * @ Param bool $ whether left outputs data from the left
- */
- Public function pop ($ key, $ left = true ){
- $ Val = $ left? $ This-> redis-> lPop ($ key): $ this-> redis-> rPop ($ key );
- Return json_decode ($ val );
- }
-
- /**
- * Auto-increment data
- * @ Param string $ key KEY name
- */
- Public function increment ($ key ){
- Return $ this-> redis-> incr ($ key );
- }
-
- /**
- * Data auto-subtraction
- * @ Param string $ key KEY name
- */
- Public function decrement ($ key ){
- Return $ this-> redis-> decr ($ key );
- }
-
- /**
- * If the key exists, true is returned.
- * @ Param string $ key KEY name
- */
- Public function exists ($ key ){
- Return $ this-> redis-> exists ($ key );
- }
-
- /**
- * Returns the redis object.
- * Redis has many operation methods. we only encapsulate some of them.
- * With this object, you can directly call the redis method.
- */
- Public function redis (){
- Return $ this-> redis;
- }
- }
|
PHP, Redis
This topic was moved by Beckham