Skip to content

Latest commit

 

History

History
83 lines (68 loc) · 2.2 KB

'redis-cli'.md

File metadata and controls

83 lines (68 loc) · 2.2 KB

Redis basic commads

complete data types
complete commands

redis-cli : interractive shell
redis-cli command ... : syntax

keys * : list all keys
type key : get type of key

var=$( redis-cli get key ) - already null stdout ( if &> /dev/null > $var == null ) string
set key value : set value of key
get key : get value of key

hash
hset key name value : set value of name in key
hmset key name value name1 value1 : set values of multiple names in key
hget key name get value of name in key
hgetall key : get all name:value pairs in key hdel key name : delete a hash

set
mset key value : set value of key
mget key : get value of key

list
keys "*" : list all keys lpush key value value1 : insert values to the left of key
rpush key value value1 : insert values to right of key
lindex key index : get value at index of key
llen key : get length of key
lpop key : get value and delete from the Left of key
rpop key : get value and delete from the right of key

delete
del key : delete key (all types)
redis-cli --scan --pattern 'abc*' | xargs redis-cli del : delete by wildcard

backup / restore

### backup
redis-cli get config dir           # saved directory
redis-cli save                     # save latest database
cp /var/lib/dump.rdb /destination  # copy database

### restore
systemctl stop redis
mv /var/lib/redis/dump.rdb{,.backup}
cp /destination/dump.rdb /var/lib/redis
systemctl start redis

copy / convert key:value hash
bash: hgetall > array > for loop

keyvalue=$( redis-cli hgetall hash0 )
readarray -t array <<<"$keyvalue"

ilength=${#array[@]}

# copy
for (( i = 0; i < $ilength; i+=2 )); do
    redis-cli hset hash1 "${array[i]}" "${array[i+1]}" 
done

# convert key:value to value:key
for (( i = 0; i < $ilength; i+=2 )); do
    redis-cli hset hash1 "${array[i+1]}" "${array[i]}" 
done

PHP: hgetall > foreach loop

$redis = new Redis(); 
$redis->pconnect( '127.0.0.1' );

$hash0 = $redis->hGetAll( 'hash0' );
foreach ( $hash0 as $key => $value ) {
    $redis->hSet( 'hash1', $key, $value );
}