Redis

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the databases category.

Last Updated: 2024-11-02

Delete all keys with prefix

Say you want to delete keys starting with :1:collectfast

From within the redis-cli

EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 :1:collectfast*

EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 asgi:group:messenger_* Regex works in the key pattern here:

asgi:group:messenger_[a-zA-Z]*

Get TTL of a key (how long before auto-deleted)

TTL key

Delete an individual key

DEL key

Get the value of one key

The correct command depends on the type of the key. This is determined with type KEY

For strings

GET KEY

For sets

smembers KEY

For hashes

HGETALL KEY

For zsets

ZRANGE KEY 0 -1

Zset

A Redis ZSET contains unique members (the keys) and scores (confined to values of floating-point numbers). The items in a ZSET are sorted and accessible by scores and the order by which the items in the ZSET were sorted. This differs from a hash because hashes can only be accessed by scores.