PHP encapsulation of redis

Source: Internet
Author: User
Tags time 0
: This article mainly introduces the PHP encapsulation of redis. if you are interested in the PHP Tutorial, please refer to it. Class MyRedis {private $ redis;/*** @ param string $ host * @ param int $ post */public function _ construct ($ host = 'xxxx ', $ 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 */public function zadd ($ key, $ value) {return $ this-> redis-> zadd ($ key, $ va Lue);}/*** 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 to a 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 * @ param 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 ;}}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the PHP encapsulation of redis, including some content, and hope to help those who are interested in the PHP Tutorial.

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.