This article mainly introduces the Redis in PHP under the command Daquan, has a certain reference value, now share to everyone, the need for friends can refer to
Appointment of Redis
Application scenario cache, queue, data storage, for memory, more easily lost
$user =user::all ()->toarray ();
String type can only be a single string, not an array
Set add Redis::set (' number ', 1); Redis::append (' number ', 2);//Append DD (Redis::strlen (' number '));//Returns the length of the character get Get value DD (redis::get (' Nember ')); Getset first Fetch, After the end set the value DD (redis::getset (' Nember ', ' baidu123 ')), INCR is suitable for the counter REDIS::INCR (' number '); Redis::incrby (' number ', 3);//Direct plus 3incrByFloat floating point number directly plus 1.5redis::set (' number ', 1);dd(redis::incrbyfloat (' number ', 0.03)); exists determines whether the key value exists in DD (redis::exists (' key ')); The presence is 1, does not exist is 0mset mget bulk Operation Redis::set (' d ', 555); Redis::mset ([' A ' =>1, ' B ' =>2, ' C ' =>3]);p Rint_r (Redis::mget ([ ' A ', ' B ', ' C ', ' d ']);//array ([0] = 1 [1] = 2 [2] = 3 [3] = 555)
List type (list push pop Llen)
Lpus add left to right Redis::d El (' La '); Redis::lpush (' La ', 1); Redis::lpush (' La ', 2); Redis::lpush (' La ', 3);dd(Redis::lrange (' La ', 0,6));//show La from 0 to 6rpush add right to left Redis::rpush (' Ra ', 1); Redis::rpush (' Ra ', 2); Redis::rpush (' Ra ', 3);dd(Redis::lrange (' Ra ', 0,6)); DD (redis::lindex (' Ra ', 0));//Gets the value of the Linsert inserted in the column (this 3 is only the first) Redis::linsert (' Ra ', ' Before ', ' 3 ', ' 555 ');// Insert 555redis::linsert (' ra ', ' After ', ' 3 ', ' 666 ') in the RA queue before 3;//Insert 666DD after 3 in the RA queue (redis::lrange (' Ra ', 0,-1)); Lpop Returns and removes the first element of the list (queue or second kill) echo (Redis::lpop (' ra '));dd(redis::lrange (' ra ')), Rpop returns and deletes the last element of the list (queue or seconds Kill) echo (Redis:: Rpop (' RA '));//return NULL the list is empty DD (redis::lrange (' Ra ', 0,-1)), Blpop brpop message Queue Redis::lpush (' a ', si); Redis::brpop (' A ', 0);// The second parameter is that time 0 is never blocked DD (redis::lrange (' A ', 0,-1)), and if the list exists, the string value is added to the list header (left) redis::lpush (' La ', 5); Redis::lpushx (' La ', 6);/ The right side is Rpushredis::lpush (' La ', 8);dd(redis::lrange (' La ', 0,-1)), Lrem deletes the specified key value Redis::lrem (' ra ', 6,1);//lrem (' Ra ', 6, 1) Delete Left to right 6 1redis::lrem (' ra ', -1,1),//lrem (' Ra ', -1,1) Delete right to left 1 1redis::lrem (' RA ', 0,1),//lrem (' Ra ', 0, 1) Delete all 1DD (Redis:: Lrange(' Ra ', 0,-1)); LSet modifies the list to be set at the index with the new value. Redis::lset (' La ', 0, ' aaaa ');dd(redis::lrange (' La ', 0,-1)); LTrim Trim an existing list similar to the PHP substrredis::ltrim (' La ', 0,2);dd(Redis :: Lrange (' La ', 0,-1)); Llen the length of the list DD (Redis::llen (' La ')); POPs a value from the tail of the list and pushes it to the front of another list. Likewise returns this value Redis::rpoplpush (' La ', ' ra ');//pushes the tail of LA to the front of the list of general user queues to prevent loss
Set type (Sadd scard sismember Srem) collection content is not duplicated
Sadd scard sismember Add/sum/whether in the collection Redis::sadd (' sa ', 1); Redis::sadd (' sa ', 2); Redis::sadd (' sa ', 3);dd(redis::scard (' sa ') );//Determine whether the set exists redis::sismember (' sa ', 1)//existence is 1, does not exist is 0sdiff to judge the difference set between two sets Redis::sadd (' SB ', 2); Redis::sadd (' SB ', 3); Redis :: Sadd (' SB ', 4);dd(Redis::sdiff (' sa ', ' sb ')),//Return not in SB, but the value in SA smembers randomly delete redis::spop (' sb ');dd(redis::smembers (' SB ')); Srem Specifies to delete redis::srem (' SB ', 2),//To remove the value of 2 in the set SB (Redis::smembers (' SB ')), sinter to determine the intersection of two sets of DD (Redis::sinter (' Sa ', ' SB '); Perform several sets of intersections and save as a new collection of Redis::sinterstore (' SC ', ' sa ', ' sb '),//sa and SB's intersection, stored in SC DD (redis::smembers (' SC '));// Outputs a collection that moves the specified member from the collection in the Srckey to the collection at Dstkey redis::smove (' sa ', ' sb ', 1);//Moves the 1 in SA to the SB DD (redis::smembers (' SB ')); Returns the union DD of multiple collections (Redis::sunion (' sa ', ' sb ')), return union, coexist in a set of Redis::sunionstore (' SD ', ' sa ', ' SC ');//Combine SA and SC to return to SD in DD (redis::smembers (' SD '));
Hash (hash) type (Hset, Hget, Hlen, Hmget)
Hset Hgethdel Deposit/Query/delete redis::hdel (' a ');//must first remove key to A's health, otherwise a itself is occupied, can not give a value redis::hset (' A ', ' aaa ', ' hello ');dd(redis::hget (' A ', ' AAA ')); Hlen returns the length of the key a DD (Redis::hlen (' a ')), and the//a array below has two subsets hexists determine if an array contains a key value of DD (redis::hexists (' A ', ' qwe '));//1 is ture 0 is Falsehmset hmget Bulk deposit in full array/batch get Redis::hmset (' BBB ', $user [' 1 ']);dd(redis::hgetall (' a ')); hsetnx Given the default value of the hash itself a-aaa has value when redis::hsetnx (' A ', ' aaa ', ' Hello word ');dd(redis::hget (' A ', ' AAA ')),//hellob-aaa no value when Redis:: Hsetnx (' A ', ' BBB ', ' Hello word ');dd(redis::hget (' A ', ' BBB '));//hello Wordhkeys similar to Array_keys () DD (Redis::hkeys (' a ')); /array of kin and values are substituted hvals similar to array_values () DD (redis::hvals (' a '));//The number of keys of the array becomes the default of 0, 1, 2, etc. hstrlen related fields value of DD (Redis::hstrlen (' a ', ' BBB ');//Returns the number of the values of the healthy BBB below the array a
Sort set type (ordered collection)
Zadd zrem Add/Remove Redis::zadd (' Key1 ', 1, ' val1 '); Redis::zadd (' Key1 ', 0, ' val0 '); Redis::zadd (' Key1 ', 5, ' val5 '); Redis:: Zrem (' Key1 ', ' val1 ');dd(redis::zrange (' Key1 ', 0,-1)); Total number of zcard calculations
General Command
Key Fuzzy search redis::set (' user1 ', ' My name is Good man ');dd(redis::keys (' user* '));//Returns an array, and is the user's keydbsize calculates the total number of keys Redis ::d bsize (); exists determines if key exists redis::exists (' key '),//Exists return 1, does not exist return 0del Delete Keyredis::d El (' key '); Expire key expires in a few seconds Redis:: Expire (' key ');//key expires after 10 seconds. Persist Remove the expiry time of key Redis::p ersist (' key '); Type key is the kind of redis::type (' key ');
Other features
Publish release and subscription of the message Redis::p ublish (' aaa ', ' Hello, world!aaa '); Redis::subscribe (Array (' chan-1 '), ' f ');// callback function f ($redis, $chan, $msg) {DD ($msg);} Geo geolocation location geoadd add redis::geoadd (' GGG ', 112.531212,37.806616, ' AAA ', 112.130619,37.396616, ' BBB ');dd(Redis::geopos ( ' GGG ', ' Beijin ');//GGG calculates the distance DD (geodist (' redis::geodist ', ' aaa ', ' BBB ', ' km ')) for KEY,AAA and BBB for the identity GGG, and//calculates the distance between AAA and BBB, KM is a unit
1. master-Slave mode
In redis.conf, add slaveof IP ports available as from Redis, can only read and synchronize slaveof <masterip> <masterport>
2.redis pipeline mode Big Data execution file, use this value to reduce the time
Redis::multi ()
3. Publication and subscription of messages