StackExchange Redis - StringSet vs. SetAdd and Expiration

In StackExchange.Redis , STRING operations set expiration, for example:

  Task<bool> StringSetAsync( RedisKey key, RedisValue value, TimeSpan? expiry = null, When when = When.Always, CommandFlags flags = CommandFlags.None); 

Why does the SET operation not work?

  Task<long> SetAddAsync( RedisKey key, RedisValue[] values, CommandFlags flags = CommandFlags.None); 

Basically, this is what I want to achieve:

Given a List<T> , add the items to the Redis set (create or add to the existing one) after 1 hour.

How can I do it? Or should I serialize List<T> , then use StringSet ?

I want to use SET functions, such as SREM , and add individual elements to an existing SET (instead of overwriting the entire SET), so I try not to use STRING .

Any tips?

+6
source share
1 answer

For the first question (why string operations have optional expiration when dialing operations are not performed): this is simply because this is what the redis operations call: SET (particularly with EX and PX modifiers) and SETEX are string operations that allow you to set the expiration date. The set SADD does not provide such a parameter. One of the reasons for this is probably to avoid confusion that the expiration will be applied to the element when the expiration will actually be applied to the whole key (i.e. the entire set).

Therefore, it is best to establish expiration explicitly; at the redis level, this is through EXPIRE , EXPIREAT , PEXPIRE or PEXPIREAT ; or on the IDatabase in SE.Redis: KeyExpire or KeyExpireAsync . This must be done after there are many; if the set is large and you are sending several batches and want the timeout to be set even if it is throttled towards the end, you may need to send an expiration after the first batch.

+7
source

Source: https://habr.com/ru/post/984877/


All Articles