laravel redis operations Daquan

General have set / get operation, set operation, if the key name exists, the original value would have to cover

$redis = app("redis.connection");

$ Redis-> set ( 'library', 'phpredis'); // key is stored library, to obtain the value of the recording phpredis

$ Redis-> get ( "library"); // get key value for the library was recorded

set / get key-value plurality

  $mkv = array(

                        "user:001"=>'First user',

                        "user:002"=>"Second user",

                        "user:003"=>"Third user"

   );

   $ Redis-> mset ($ mkv); // store a plurality of corresponding key value

   $ Retval = $ redis-> mget (array_keys ($ mkv)); // Get value corresponding to a plurality of key

In storing the memory limitation of record

   $ Redis-> setex ( "library", 10, 'phpredis'); // Library key is stored, is recorded phpredis value, when the effective length is 10 seconds

add operation does not overwrite the existing value

   $ Redis-> setnx ( "foo", 12); return true //, successfully added

    $ Redis-> setnx ( 'foo', 34); // returns false, addition failed, because of the record key name foo

    variants set, return the result value before replacing

   $ Redis-> getset ( 'foo', 56); // Returns 12; if the record does not exist before, or null

   incrby / incr / decrby / decr worth of increment and decrement

   $ Redis-> incr ( 'foo'); // returns 57, while the value of 57 foo

   $ Redis-> incrby ( 'foo', 2); // foo return 59 while the value 59

Detect the presence of value

   $redis->exists("foo");

type detection type, the returned string string, returns a list of list, set table returns set / zset, hash hash table returns

   $redis->type('foo');

 append a string connected to the existing

 $ Redis-> get ( 'str'); // return test

 $redis->append('str' , "_123");

Alternatively setrange operation section, and returns the string length

  $ Redis-> setrange ( 'str', 0, 'abc'); // returns 3, the second parameter set to 0 is equivalent to the operation

   $ Redis-> setrange ( 'str', 2, 'cd'); // Returns 4 shows replacement characters from the second, then the 'str' is 'abcd'

substr part of the get operation

   $ Redis-> substr ( 'str', 0, 2); // abc returns 0 represents starting from the first, the second to take the string

    $ Redis-> strlen ( 'str'); // Returns 4 The 'str' is 'abcd'

  setbit bit memory

   $ Redis-> setbit ( 'library', 31, 1); // stored in position 31 represents a

   getbit get bit

    $ Redis-> getbit ( 'library', 31); // Returns 1

    Fuzzy Lookup function keys, as well as support asterisk? No. (match one character)

    $redis->set('foo1',123);

    $redis->set('foo2' , 456);

    $ Redis-> keys ( 'foo *'); // returns the array foo1 and foo2

    $ Redis-> keys ( 'f 0??'); // Ibid.

   randomkey returns a random key

   $ Redis-> randomkey (); // may return 'foo1' or any other key foo2 and existing

 rename / renamenx be renamed key way, except that renamenx not allowed to change the existing key

$ Redis-> rename ( 'str', 'str2'); // The original name of the key changed str2 str

     expire timeliness set of key-value

     ttl acquire the remaining life

     persist reset to permanent storage

     $ Redis-> expire ( 'foo', 10); // set valid for 10 seconds

      $ Redis-> ttl ( 'foo'); // return the remaining life value 10 seconds

dbsize returns the total number redis database of the current record

  $ Redis-> dbsize ();

Queue operation

  rpush / rpushx ordered list of actions, the insertion element from the queue; difference lpush / lpushx and rpush / rpushx is inserted into the head of the queue, supra, 'x' means that the only existing operation key

 $ Redis-> rpush ( 'foolist', 'bar1'); // return a list of length 1

 $ Redis-> rpush ( 'foolist', 'bar0'); // return a list of length 2

 $ Redis-> rpushx ( 'foolist', 'bar2'); // returns 3, rpushx only do add to an existing queue, otherwise returns 0

 $ Redis-> llen ( 'foolist'); // Returns 3

lrange returns a queue element section

$ Redis-> lrange ( 'foolist', 0, 1); // Returns an array of 0 to 1, a total of 2 elements

$ Redis-> lrange ( 'foolist', 0, -1); // Returns 0 to last, and returns all equivalent elements  

It returns the ordinal position lindex list element

$ Redis-> lindex ( 'foolist', 1); // Returns bar1

lset modify the queue position designated value 

$ Redis-> lset ( 'foolist', 1, '123'); // modify the position of the element 1, returns true

lrem delete the queue specified number of characters from the left

$ Redis-> lrem ( "foolist", 1, '_'); // queue is removed from the left (from right to use -1), a character '_' (if any)

lpop / rpop stack structure similar to the pop-up (and delete) the leftmost or rightmost one element

$ Redis-> lpop ( 'foolist'); // return the left

$ Redis-> rpop ( 'foolist'); // return the right

ltrim modify queue while retaining certain elements from the left, delete the rest

$ Redis-> ltrim ( 'foolist', 0, 1); // Reserved from 0 to the left of the first element

rpoplpush pop from one queue to another queue element and push

$ Redis-> rpush ( 'list1' 'AB0');

$ Redis-> rpush ( 'list1' 'ab 1');

$ Redis-> rpush ( 'list2' 'ab 2');

$ Redis-> rpush ( 'list2' 'ab3 ");

$ Redis-> rpoplpush ( 'list1' "list2"); 

$ Redis-> rpoplpush ( 'list2', 'list2'); 

Linsert specified element in front of or after inserting the intermediate queue element

$ Redis-> linsert ( 'list2', 'before', 'ab1', '123'); // represents the element 'Ab1' prior to insertion of '123'

$ Redis-> linser ( 'list2', 'after', 'ab1', "456"); // represents inserted after the element 'ab1'

blpop / brpop block and wait for a queue is not empty, the most in pop right or left of an element (this feature can be very useful outside of php)

$ Redis-> blpop ( 'list3', 10); // if list3 is empty waits know not empty element to the first pop-up after 10 second timeout

set collection operation

sadd increase the collection element set, returns true, repeat returns false

$redis->sadd('set1' , 'ab');

$redis->sadd('set1' , 'cd');

$redis->sadd('set1' , 'ef');

srem Removes the specified element

$ Redis-> srem ( 'set1', 'cd'); // delete 'cd' elements

spop pop the first element

 $ Redis-> spop ( "set1"); // Returns 'ab'

 smove specified element moving this set of collection to another collection set

$redis->sadd("set2",'123');

$ Redis-> smove ( 'set1', 'set2', 'ab'); // ab to move in set1 set2, returns true or false; ab set value set1 this case does not exist

Returns the number of the current set scard table element

$ Redis-> scard ( 'set2'); // Returns 2

sismember determines whether the element belongs to the current collection set

$redis->sismember('set2','123'); //返回true or false

smembers returns all elements of the current set collection

$ Redis-> smember ( 'set2'); // returns the array (123, ab)

sinter / sunion / sdiff returns two tables intersection / union / complement

$redis->sadd('set1' , 'ab');

$ Redis-> sinter ( 'set2', 'set1'); // return array ( 'ab');

sinterstore / sunionstore / sdiffstore two tables intersection / union / complement element into a third copy of the table

$redis->set('foo' , 0);

$ Redis-> sinterstore ( 'foo', 'set1'); // set1 is equivalent to the contents of copy into foo, and set into the table foo

$ Redis-> sinterstore ( 'foo', array ( 'set1', 'set2')); // set1 and set2 to like elements in the copy table foo, covering the original content foo

srandmember immediately returns a table element

$ Redis-> srandmember ( 'Set1');

Ordered set operating table

zadd element increases, and set number, returns true if successful, returns false repeat

$redis->zadd("zset1" , 1 , 'ab');

$ Redis-> Zadd ( 'zset1', 2, 'cd');

$redis->zadd('zset1' , 3 , 'ef');

zincrby specified element is increased or decreased value of the index, change the sort order of elements

$ Redis-> zincryBy ( 'zset1', 10, 'ab'); // Returns 11

zrem Removes the specified element

$ Redis-> zrem ( 'zset1', 'ef'); // returns true or false

 Table zrange Returns the specified range order of the positions of the elements by

$ Redis-> zrange ( "zset1", 0, 1); // elements between 0 and 1 (2) of the return position

$ Redis-> zrange ( 'zset1', 1, -1); // returns the position of the element between the first element and the inverse of 0 (equivalent to all the elements)

zrevrange above, returns the elements specified in the table section, inverted in sequence

$ Redis-> zrevrange ( 'zset1', 0, -1); // do the opposite order and zrange

zrangeByscore / zrevrangeByscore Returns the specified index table section element in order / descending

$redis->zadd('zset1' , 3 , 'ef');

$redis->zadd('zset1' , 5 , 'gh');

$ Redis-> zrangeByscore ( 'zset1', 2, 9); // returns the index array elements between 2-9 ( 'ef', 'gh');

$ Redis-> zrangeByscore ( 'zset1', 2, 9, array ( 'withscores' => true, 'limit' => array (1,2))); // returns the index between the elements 2-9, withscores => true index values ​​represent comprising; limit => array (1,2), represents the offset 1, 2 return result to array (array ( 'ef', 3), array ( 'gh', 5))

Zcount number of elements in a statistical index range

$ Redis-> zcount ( 'zset1', 3, 5); // Returns 2

$ Redis-> zcount ( 'zset1', '(3', 5)); // '(3' represents the value of the index but no 3, may also be used in the same way between 3-5 '(5' represents 5 but no upper limit of 5

The number of elements zcard statistics

$ Redis-> zcard ( 'zset1'); // Returns 4

zremrangeByscore remove elements of an index range

$ Redis-> zremrangeByscore ( 'zset1', 0, 2); // delete the index element between 0-2 (ab, cd), returns the number of deleted elements 2

zrank / zrevrank return position table element at which the order / descending (not the index)

$ Redis-> zrank ( 'zset1', 'ef'); // returns 0, since it is an element; zrevrank Returns 1 (last)

zremrangeByrank delete the elements specified in the table position range

$ Redis-> zremrangeByrank ( 'zset1', 0, 10); // delete the element positions 0-10, and returns the number of elements removed 2

hash table operations

$ Redis-> hset ( 'hash1', 'key1', 'v1'); // as the key key1, value v1 of the table element is stored hash1

$redis->hset("hash1" , 'key2' , 'v2');

$ Redis-> hget ( 'hash1', 'key1'); // hash1 takes a value in the table key key key1, return v1

hexists returns the specified key hash table exists

$redis->hexists("hash1" , 'key1');//true 或 false

hdel delete hash table of the elements specified key

$redis->hdel('hash' , 'key2');//true  or  false

Returns the number of hash table element hlen

$ Redis-> hlen ( 'hash1'); // Returns 1

hsetnx add an element, but can not be repeated

$ Redis-> hsetnx ( 'hash1', 'key1' 'v2');

$ Redis-> hsetnx ( 'hash1', 'key2' 'v2');

hmset / hmget hash table access to the plurality of elements

$redis->hmset( 'hash1' , array('key3'=>'v3' , 'key4'=>'v4' ) );

$ Redis-> hmget ( 'hash1', array ( 'key3', 'key4')); // the return value of the array response ( 'v3', 'v4');

accumulating the specified key hincryby

$ Redis-> hincryBy ( 'hash1', 'key5', 3); // do not exist, and returns the memory 3; exists, i.e., returns the original value +3

$ Redis-> hincryBy ( "hash1", 'key5', 10); // Returns 13

hkeys return all key hash table

$redis->hkeys('hash1'); // 返回array('key1' , 'key2' , 'key3' , 'key4' , 'key5');

hvals return all value hash table

$ Redis-> hvals ( 'hash1'); // return array ( 'v1', 'v2', 'v3', 'v4', 13);

hgetall return the entire hash table element

$ Redis-> hgetall ( 'hash1'); // return all the table elements hash1

Sorting operation

sort sort

$redis->rpush('tab' , 3);

$redis->rpush('tab' , 2);

$redis->rpush('tab' , '17');

$ Redis-> sort ( 'tab'); // returns the array (2,3,17);

$ Redis-> sort ( 'tab', array ( 'sort' => 'desc')); // descending order, return array (17, 3, 2)

$ Redis-> sort ( 'tab', array ( 'limit' => array (1,2))); // returns the order of the elements 1 in two positions (where 2 is the number, instead of the position) return array (3,17)

$ Redis-> sort ( 'tab', array ( 'limit' => array ( 'alpha' => true))); // return sorted by the first character array (17, 2, 3), because the first 17 characters so first row is 1 location

$ Redis-> sort ( 'tab', array ( 'limit' => array ( 'store' => 'ordered'))); // represents a sort of permanent, returns the number of elements

$ Redis-> sort ( 'tab', array ( "limit" => array ( 'get' => 'pre _ *'))); // * wildcard filter element used, which returns only the beginning of the pre element

Redis management operations

info display when the state information service

$redis->info();

select the specified database to be operated

$ Redis-> select (4); // the specified index database

flushdb clear the current library

$redis->flushdb();

When the moving element library move to another database

$redis->set('tomove' , 'bar');

$redis->move('tomove' , 4);

slaveof configuration from the server

$ Redis-> slaveof ( '127.0.0.1', 80); // configuration server 127.0.0.1 port 80 from the server

$ Redis-> slaveof (); // eliminated from the server

Synchronization server to save data to disk

$redis->save();

Asynchronous server save data to disk

$ Redis-> bgsave ()

Return time of last update disk

$ Redis-> LASTSAVE ();

Guess you like

Origin www.cnblogs.com/shangfz/p/11454733.html