PHP redis operations summary, phpredis

Source: Internet
Author: User
Tags delete key php redis time 0

PHP redis operations summary, phpredis

<Pre name = "code" class = "php"> <span style = "font-size: 18px;"> class MyRedis {private $ redis; /*** @ param string $ host * @ param int $ post */public function _ construct ($ host = '10. 102.1.8 ', $ port = 6379) {$ this-> redis = new Redis (); $ this-> redis-> connect ($ host, $ port ); return $ this-> redis ;} /*** set the value to construct a string * @ param string $ key KEY name * @ param string $ value set the value * @ param int $ timeOut time 0 indicates no expiration time */ Public function set ($ key, $ value, $ timeOut = 0) {$ retRes = $ this-> redis-> set ($ key, $ value ); if ($ timeOut> 0) $ redis-> expire ('$ key', $ timeOut); return $ retRes;}/** construct a set (unordered set) * @ param string $ key set Y name * @ param string | array $ value */public function sadd ($ key, $ value) {return $ this-> redis-> sadd ($ key, $ value);}/** build a set (ordered set) * @ param string $ key set name * @ param string | array $ value */publ Ic function zadd ($ key, $ value) {return $ this-> redis-> zadd ($ key, $ value );} /*** get the corresponding element of the Set * @ param string $ setName set name */public function smembers ($ setName) {return $ this-> redis-> smembers ($ setName);}/*** build a list (similar to stack) * @ param sting $ key KEY name * @ param string $ value */public function lpush ($ key, $ value) {echo "$ key-$ value \ n "; return $ this-> redis-> LPUSH ($ key, $ value);}/*** build a list (first, similar Queue) * @ param sting $ key KEY name * @ param string $ value */public function rpush ($ key, $ value) {echo "$ key-$ value \ n"; return $ this-> redis-> rpush ($ key, $ value );} /*** obtain all list data (from start to end) * @ param sting $ key KEY name * @ param int $ head start * @ param int $ tail end */public function lranges ($ key, $ head, $ tail) {return $ this-> redis-> lrange ($ key, $ head, $ tail);}/*** HASH type * @ param string $ tableName table name key * @ p Aram string $ key field name * @ param sting $ value */public function hset ($ tableName, $ field, $ value) {return $ this-> redis-> hset ($ tableName, $ field, $ value);} public function hget ($ tableName, $ field) {return $ this-> redis-> hget ($ tableName, $ field );} /*** set multiple values * @ param array $ keyArray KEY name * @ param string | the data obtained by array $ value * @ param int $ timeOut time */public function sets ($ keyArray, $ timeout) {if (is _ Array ($ keyArray) {$ retRes = $ this-> redis-> mset ($ keyArray); if ($ timeout> 0) {foreach ($ keyArray as $ key =>$ value) {$ this-> redis-> expire ($ key, $ timeout) ;}} return $ retRes ;} else {return "Call ". _ FUNCTION __. "method parameter Error! ";}}/*** Get data through key * @ param string $ key KEY name */public function get ($ key) {$ result = $ this-> redis-> get ($ key); return $ result ;} /*** get multiple values simultaneously * @ param ayyay $ keyArray get key value */public function gets ($ keyArray) {if (is_array ($ keyArray )) {return $ this-> redis-> mget ($ keyArray);} else {return "Call ". _ FUNCTION __. "method parameter Error! ";}}/*** Get all key names, not values */public function keyAll () {return $ this-> redis-> keys ('*');} /*** delete a data key * @ param string $ key delete KEY name */public function del ($ key) {return $ this-> redis-> delete ($ key );} /*** Delete multiple key data simultaneously * @ param array $ keyArray KEY set */public function dels ($ keyArray) {if (is_array ($ keyArray )) {return $ this-> redis-> del ($ keyArray);} else {return "Call ". _ FUNCTION __. "meth Od parameter Error! ";}}/*** Auto-increment data * @ param string $ key KEY name */public function increment ($ key) {return $ this-> redis-> incr ($ key);}/*** auto-Subtract data * @ param string $ key KEY name */public function decrement ($ key) {return $ this-> redis-> decr ($ key );} /*** determine whether the key exists * @ param string $ key KEY name */public function isExists ($ key) {return $ this-> redis-> exists ($ key);}/*** rename-change the key to newkey only when the newkey does not exist, when newkey exists, an error is reported. RENAME * is different from rename. It is directly updated (the existing value is also updated) * @ param string $ Key KEY name * @ param string $ newKey new key name */public function updateName ($ key, $ newKey) {return $ this-> redis-> RENAMENX ($ key, $ newKey);}/*** obtain the value type of the KEY storage * none (key does not exist) int (0) string (string) int (1) list (list) int (3) set (set) int (2) zset (sorted set) int (4) hash (hash table) int (5) * @ param string $ key KEY name */public function dataType ($ key) {return $ this-> redis-> type ($ key );} /*** clear data */public function flushAll () {return $ this-> redis-> flushAll ();} /*** return redis object * redis has many operation methods. We only encapsulate a part of * with this object, we can directly call redis's own Method * eg: $ redis-> redisOtherMethods ()-> keys ('* a *') keys method not blocked */public function redisOtherMethods () {return $ this-> redis ;}} </span>


 
 

How can php redis respond?

Redis stores data in the memory and loses power. Pay attention to this. Make a persistence if necessary. The persistence method is hard to understand. For more information, see the article on the Internet.

Php's redis extension is called php-redis. The php-redis Chinese manual is available on the Internet. The following is an example:
<? Php $ redis = new redis (); $ result = $ redis-> connect ('2017. 0.0.1 ', 6379); // 6379 is the default port $ result = $ redis-> set ('123', "comment "); // set the key value echo $ result = $ redis-> get ('20140901'); // get the key value $ all = $ redis-> getMultiple (array ('20140901 ', '9639002718'); // obtain multiple key values at the same time // The method for obtaining all key values is not provided. I'm not sure whether it can be used. You can give it a try. $ All = $ redis-> getMultiple (array ('*'));
Thank you for your support!

 
How can I determine whether redis is suspended when php is connected to Redis?

Ping redis command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.