Redisphp instance
/**
* Redis study notes
*/
// Echo 8% (-2); // print 0
/**
Link a redis instance
Return TRUE if the return link is successful; otherwise, false is returned.
*/
$ Redis = new Redis ();
$ Result = $ redis-> connect ('2017. 0.0.1 ', 127 );
If ($ result ){
Echo "redis conn is success ";
} Else {
Echo "redis conn is fail ";
}
Echo '';
/**
Set the value of key and value and read the set key and value
*/
$ Result = $ redis-> set ('test', "hello word redis ");
Var_dump ($ result ).'
'; // Result: bool (true)
$ Result = $ redis-> get ('test ');
Var_dump ($ result ).'
'; // Result: string (16) "hello word redis"
$ Redis-> delete ('test'); // delete the key test that stores redis in the memory
$ Result = $ redis-> get ('test'); // The result is boolean false because the memory is nearly deleted.
Var_dump ($ result ).'
';
/**
Redis judge whether the key exists
*/
$ Seting = $ redis-> set ("demo", 'redis key exists? ');
Var_dump ($ seting ).'
'; // Boolean true
$ Exists = $ redis-> exists ('demo ');
Var_dump ($ exists ).'
'; // Boolean true
/**
Decr
Description: The storage key value of the decimal number.
Parameter: key value: value to be added to the key
Return value: INT the new value
Incr
Description: The storage key value of the decimal number.
Parameter: key value: value to be added to the key
Return value: INT the new value
*/
$ Redis-> set ('test', "100 ");
Var_dump ($ redis-> incr ("test ")).'
'; // Result: int (101)
Var_dump ($ redis-> incr ("test ")).'
'; // Result: int (102)
$ Redis-> set ('test1', "10 ");
Var_dump ($ redis-> decr ("test1 ")).'
'; // Result: int (9)
Var_dump ($ redis-> decr ("test1 ")).'
'; // Result: int (8)
/**
GetMultiple
Description: Gets the values of all specified keys. If one or more keys do not exist, the value of the key in the array is false.
Parameter: list array containing key values
Return value: returns an array containing values of all keys.
*/
$ Arr = array ('20140901', 'zhuwawa ');
$ Int = 100;
$ String = 'My love ....';
$ Redis-> set ('demo2', $ arr );
$ Redis-> set ('demo3', $ int );
$ Redis-> set ('demo4', $ string );
$ Result1 = $ redis-> getMultiple (array ('demo2', 'demo3', 'demo4 '));
Echo'
';
Var_dump ($ result1 ).'
';
/**
Lpush
Insert one or more values to the header of the list key.
Description: Adds a string value to the list header. If this key does not exist, the list is created. If the key exists and is not a list, FALSE is returned.
Parameter: key, value
Returned Value: The array length is returned successfully. if the returned value is false
*/
$ Redis-> delete ('test ');
Var_dump ($ redis-> lpush ("test", "111 ")).'
'; // Result: int (1)
Var_dump ($ redis-> lpush ("test", "222 ")).'
'; // Result: int (2)
/**
Rpush
Description: Adds a string value at the end of the list. If this key does not exist, the list is created. If the key exists and is not a list, FALSE is returned.
Parameter: key, value
Returned Value: The array length is returned successfully. if the returned value is false
*/
$ Redis-> delete ('test ');
Var_dump ($ redis-> lpush ("test", "111 ")).'
'; // Result: int (1)
Var_dump ($ redis-> lpush ("test", "222 ")).'
'; // Result: int (2)
Var_dump ($ redis-> rpush ("test", "333 ")).'
'; // Result: int (3)
Var_dump ($ redis-> rpush ("test", "444 ")).'
'; // Result: int (4)
/**
Lpop
Description: returns and removes the first element of the list.
Parameter: key
Return value: the value of the first element is returned. if the return value fails, false is returned.
*/
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
$ Redis-> rpush ("test", "333 ");
$ Redis-> rpush ("test", "444 ");
Var_dump ($ redis-> lpop ("test ")).'
'; // Result: string (3) "222"
/**
Rpop
Description: returns and removes the last element of the list.
Parameter: key
Return value: returns the value of the last element. if the return value fails, false is returned.
*/
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
$ Redis-> rpush ("test", "333 ");
$ Redis-> rpush ("test", "444 ");
Var_dump ($ redis-> rpop ("test ")).'
'; // Result: string (3) "444"
/**
Lsize, llen
Description: the length of the returned list. If the list does not exist or is empty, this command returns 0. If the key is not a list, the command returns FALSE.
Parameter: Key
Returned Value: The array length is returned successfully. if the returned value is false
*/
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
$ Redis-> rpush ("test", "333 ");
$ Redis-> rpush ("test", "444 ");
Var_dump ($ redis-> lsize ("test ")).'
'; // Result: int (4)
/**
Lget
Description: return the elements specified by the specified key stored in the list. 0: The first element, 1: The second... -1: The last element,-2's penultimate... If the wrong index or key does not point to the list, FALSE is returned.
Parameter: key index
Returned value: the value of the specified element is returned successfully. if the returned value is false
*/
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
$ Redis-> rpush ("test", "333 ");
$ Redis-> rpush ("test", "444 ");
Var_dump ($ redis-> lget ("test", 3 )).'
'; // Result: string (3) "444"
/**
Lset
Description: Adds a value to the index specified in the list. if the index does not exist, false is returned.
Parameter: key index value
Return value: true if the call is successful, false if the call fails.
*/
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
Var_dump ($ redis-> lget ("test", 1 )).'
'; // Result: string (3) "111"
Var_dump ($ redis-> lset ("test", 1, "333 ")).'
'; // Result: bool (true)
Var_dump ($ redis-> lget ("test", 1 )).'
'; // Result: string (3) "333"
/**
Lgetrange
Description:
Returns the specified element lGetRange (key, start, end) from start to end in the specified key list of the region ). 0: The first element, 1: The second element... -1: The last element,-2's penultimate...
Parameter: key start end
Return value: the search value is returned successfully. if the query fails, false is returned.
*/
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
Print_r ($ redis-> lgetrange ("test", 0,-1); // result: Array ([0] => 222 [1] => 111)
/**
Lremove
Description: removes count matched values from the header of the list. If count is zero, all matched elements are deleted. If count is a negative number, the content is deleted from the end.
Parameter: key count value
Return value: the number of deleted items returned. if the deletion fails, the return value is false.
*/
$ Redis-> delete ('test ');
$ Redis-> lpush ('test', 'A ');
$ Redis-> lpush ('test', 'B ');
$ Redis-> lpush ('test', 'C ');
$ Redis-> rpush ('test', 'A ');
Print_r ($ redis-> lgetrange ('test', 0,-1); // result: array ([0] => c [1] => B [2] => a [3] =>)
Var_dump ($ redis-> lremove ('test', 'A', 2); // Result: int (2)
Print_r ($ redis-> lgetrange ('test', 0,-1); // result: Array ([0] => c [1] => B)
/**
Sadd
Description: Adds a value to a Key. If the value is already in this Key, FALSE is returned.
Parameter: key value
Return value: true if the call is successful, false if the call fails.
*/
$ Redis-> delete ('test ');
Var_dump ($ redis-> sadd ('test', '2013'); // Result: bool (true)
Var_dump ($ redis-> sadd ('test', '2013'); // Result: bool (true)
Print_r ($ redis-> sort ('test'); // result: Array ([0] => 111 [1] => 333)
/**
Sremove
Description: deletes the specified value in the Key.
Parameter: key member
Return value: true or false
*/
$ Redis-> delete ('test ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sremove ('test', '123 ');
Print_r ($ redis-> sort ('test'); // result: Array ([0] => 333)
/**
Smove
Description: move the value in Key1 to Key2.
Parameter: srcKey dstKey member
Return value: true or false
*/
$ Redis-> delete ('test ');
$ Redis-> delete ('test1 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test1', '123 ');
$ Redis-> sadd ('test1', '123 ');
$ Redis-> smove ('test', "test1", '123 ');
Print_r ($ redis-> sort ('test1'); // result: Array ([0] => 111 [1] => 222 [2] => 444 );
/**
Scontains
Description: checks whether a specified value exists in the set.
Parameter: key value
Return value: true or false
*/
$ Redis-> delete ('test ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
Var_dump ($ redis-> scontains ('test', '2013'); // Result: bool (true)
/**
Ssize
Description: number of stored values in the collection.
Parameter: key
Returned value: the number of returned arrays. if the return value is successful, the return value is 0.
*/
$ Redis-> delete ('test ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
Echo $ redis-> ssize ('test'); // Result: 2
/**
Spop
Description: random removal and return a value in the key.
Parameter: key
Return value: the deleted value is returned. if the deletion fails, false is returned.
*/
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
Var_dump ($ redis-> spop ("test"); // The result is: string (3) "333"
/**
Sinter
Description: returns the intersection of all specified keys. If only one key is specified, this command generates the members of this set. If a key does not exist, FALSE is returned.
Parameter: key1, key2, keyN
Returned value: Array intersection is returned successfully. if the return value is false
*/
$ Redis-> delete ('test ');
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Var_dump ($ redis-> sinter ("test", "test1"); // result: array (1) {[0] => string (3) "111 "}
/**
Sinterstore
Description: run the sInter command and save the result to the new variable.
Parameters:
Key: dstkey, the key to store the diff.
Keys: key1, key2... KeyN. key1.. keyN are intersected as in sInter.
Return value: success, number of intersections, failure, false
*/
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Var_dump ($ redis-> sinterstore ('new', "test", "test1"); // Result: int (1)
Var_dump ($ redis-> smembers ('New'); // result: array (1) {[0] => string (3) "111 "}
/**
Sunion
Description:
Returns the union of all specified keys.
Parameters:
Keys: key1, key2 ,... , KeyN
Returned value: the merged set is returned successfully. if the merging fails, false is returned.
*/
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Print_r ($ redis-> sunion ("test", "test1"); // result: array ([0] => 111 [1] => 222 [2] => 333 [3] => 444)
/**
Sunionstore
Description: execute the sunion command and save the result to the new variable.
Parameters:
Key: dstkey, the key to store the diff.
Keys: key1, key2... KeyN. key1.. keyN are intersected as in sInter.
Return value: success, number of intersections, failure, false
*/
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Var_dump ($ redis-> sinterstore ('new', "test", "test1"); // Result: int (4)
Print_r ($ redis-> smembers ('New'); // result: array ([0] => 111 [1] => 222 [2] => 333 [3] => 444)
/**
Description: return the result that exists in the first set and does not exist in all other sets.
Parameter: Keys: key1, key2 ,... , KeyN: Any number of keys corresponding to sets in redis.
Returned Value: The array is returned successfully, and the returned value is false.
*/
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Print_r ($ redis-> sdiff ("test", "test1"); // result: Array ([0] => 222 [1] => 333)
/**
Sdiffstore
Description: execute the sdiff command and save the result to the new variable.
Parameters:
Key: dstkey, the key to store the diff.
Keys: key1, key2 ,... , KeyN: Any number of keys corresponding to sets in redis
Return value: a number is returned for success, and false is returned for failure.
*/
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Var_dump ($ redis-> sdiffstore ('new', "test", "test1"); // Result: int (2)
Print_r ($ redis-> smembers ('New'); // result: Array ([0] => 222 [1] => 333 );
/**
Smembers, sgetmembers
Description:
Returns the content of the set.
Parameter: Key: key
Returned value: An array of elements, the contents of the set.
*/
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
Print_r ($ redis-> smembers ('test'); // result: Array ([0] => 111 [1] => 222)
?>