Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis "nil" or "empty list or set"

Tags:

redis

I'm currently working with redis using "set" structure.

I want to know if it's possible to clean automatically empty "set" ?

Else find a cron/process to clean periodically empty "set"


UPDATE:

More generic question, there is a diff (memory usage) between "(nil)" and "(empty list or set)"

example:

sadd x 1
srem x
smembers x
(empty list or set)

or

sadd x 1
del x
smembers x
(nil)
like image 330
Kakawait Avatar asked Dec 11 '12 10:12

Kakawait


People also ask

How can I tell if Redis is empty?

The way to test for this in Redis is to simply query the key. If the key is empty, populate it. If it is a string use get (or exists). If it is a hash then query for the specific members you need.

How do I delete a set in Redis?

To delete a large set in Redis: Rename the key to a unique, namespaced key so that the set appears “deleted” to other Redis clients immediately. Incrementally delete members from the set in small batches until it is empty.

How do I get all Redis keys?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

Does Redis delete keys?

Deleting Keys Deleting a key in Redis is as simple as creating one. We use the DEL command followed by the name of the key we wish to remove. Redis will drop the key and its associated data if the specified key exists in the data store. You can also use the DEL command to remove multiple keys in a single instance.


1 Answers

This is already automatic. When a set is empty, it is removed from the namespace.

> flushall
OK
> sadd x 1 2 3
(integer) 3
> keys *
1) "x"
> srem x 1 2 3
(integer) 3
> keys *
(empty list or set)

You do not have to do anything specific to benefit from this behavior.

To answer your second question, (nil) or (empty list or set) is just an interpretation of the client program. In the Redis server, in both cases, the entry has been physically removed, and the associated memory freed.

like image 80
Didier Spezia Avatar answered Dec 09 '22 22:12

Didier Spezia