Booksleeve setting expiration on multiple key/values

1k views Asked by At

Unless I am missing something, I don't see a Multiple Set/Add overload that allows you to set multiple keys with an expiration.

var conn = new RedisConnection("server");

Dictionary<string,string> keyvals;

conn.Strings.Set(0,keyvals,expiration);

or even doing it with multiple operations

conn.Strings.Set(0,keyvals);
conn.Expire(keyvals.Keys,expiration);
2

There are 2 answers

3
Marc Gravell On BEST ANSWER

No such redis operation exists - expire is not varadic. However, since the api is pipelined, just call the method multiple times. If you want to ensure absolute best performance, you can suspend eager socket flushing while you do this:

conn.SuspendFlush();
try {
    foreach(...)
        conn.Keys.Expire(...);
} finally {
    conn.ResumeFlush();
}
0
Seawyvern On

Here is my approach:

var expireTime = ...
var batchOp = redisCache.CreateBatch();
foreach (...) {
    batchOp.StringSetAsync(key, value, expireTime);
}
batchOp.Execute();