Redis operation command for key (with PHP code)

Keywords: Redis PHP Database

Redis Operational Command for key

del key1 key2 … Keyn

Role: Delete one or more keys

Return value: The number of keys that do not exist is ignored and the number of keys that are actually deleted is returned

<?php
$redis->del('myname');# Return TRUE(1)
$redis->del('myname');# There is no return int(0)
$redis->del(array('key1','key2',...));#Delete multiple

rename key newkey

Function: Give a new key name

Note: If the new key already exists, the original value of the new key is overwritten.

<?php
$redis->rename('key','key1');#If key1 exists, override key does not return false, otherwise return true

renamenx key newkey

Function: Rename key to newkey

Return: Modified returns 1, unchanged returns 0
Note: nx -> not exists, that is, when new key does not exist, rename it

<?php
$redis->renamenx('key','key1');#If key1 exists or key does not exist, return false or true

move key db

redis 127.0.0.1:6379[1]> select 2
OK
redis 127.0.0.1:6379[2]> keys *
(empty list or set)
redis 127.0.0.1:6379[2]> select 0
OK
redis 127.0.0.1:6379> keys *
1) "name"
2) "cc"
3) "a"
4) "b"
redis 127.0.0.1:6379> move cc 2
(integer) 1
redis 127.0.0.1:6379> select 2
OK
redis 127.0.0.1:6379[2]> keys *
1) "cc"
redis 127.0.0.1:6379[2]> get cc
"3"

(Note: A redis process opens more than one database. By default, it opens 16 databases, numbered from 0 to 15. If you want to open more databases, you can modify them from the configuration file.)

<?php
$redis->move('key',2);#key does not exist to return false bool(true)

keys pattern queries for corresponding key s

In redis, fuzzy query key is allowed
There are three wildcards *,?,[]
* Wildcard any number of characters
?: wildcard single character
[]: A character in Wildcard brackets

redis 127.0.0.1:6379> flushdb
OK
redis 127.0.0.1:6379> keys *
(empty list or set)
redis 127.0.0.1:6379> mset one 1 two 2 three 3 four 4
OK
redis 127.0.0.1:6379> keys o*
1) "one"
redis 127.0.0.1:6379> key *o
(error) ERR unknown command 'key'
redis 127.0.0.1:6379> keys *o
1) "two"
redis 127.0.0.1:6379> keys ???
1) "one"
2) "two"
redis 127.0.0.1:6379> keys on?
1) "one"
redis 127.0.0.1:6379> set ons yes
OK
redis 127.0.0.1:6379> keys on[eaw]
1)"one"
<?php
var_dump($redis->keys('*o*')); //array(3) { [0]=> string(4) "four" [1]=> string(3) "two" [2]=> string(3) "one" }
var_dump($redis->keys('t??')); //array(1) { [0]=> string(3) "two" }
var_dump($redis->keys('t[w]*')); //array(1) { [0]=> string(3) "two" }
print_r($redis->keys('*')); //Array ( [0] => four [1] => three [2] => two [3] => one )

Random key returns random key

<?php
$redis->randomkey();#If the database is empty, false is returned, and random key is returned anyway.

exists key

To determine whether the key exists, return 1/0

<?php
$redis->exists('key');#bool(true)

type key

Returns the type of value stored in key

There are strings, links, sets, order sets, hash

<?php
$redis->type('key');#string,link,set,order set, hash

expire key integer value

Role: Set the life cycle of the key in seconds

The same reason:
pexpire key milliseconds, set life cycle
Pttl key, returning to life cycle in milliseconds

<?php
$redis->expire('name',30);  # Set the lifetime to 30 seconds//return (integer) 1

ttl key

Role: Query key life cycle

Return: seconds

Note: For nonexistent keys or expired keys / expired keys, return - 1
In Redis 2.8, for nonexistent key s, return - 2

<?php
$redis->expire('name',30);  # Set the lifetime to 30 seconds//return (integer) 1

persist key

Function: Set the specified key to permanent validity

<?php
$redis->persist('time_to_say_goodbye');  # Remove Lifetime

Posted by hardius on Mon, 25 Mar 2019 10:15:30 -0700