Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic read and delete from Redis using StackExchange.Redis

Is there an easy way to atomically read a value and then delete it from Redis using StackExchange c# driver?
I am buffering items in Redis and when they reach a certain threshold I retrieve them, but I also want to flush my buffer. I need to mention that I store items in a list and by "flushing the buffer" I mean I want to delete the list.
"key" : [list of items]

like image 507
Ben Avatar asked Nov 16 '16 14:11

Ben


1 Answers

You can create a transaction and do the GET/DEL atomically, like this:

var db = connectionMultiplexer.GetDatabase();
var tran = db.CreateTransaction();
var getResult = tran.StringGetAsync(key);
tran.KeyDeleteAsync(key);
tran.Execute();
var value = getResult.Result;

This will send the following commands to Redis:

MULTI
GET "key"
DEL "key"
EXEC
like image 161
thepirat000 Avatar answered Nov 02 '22 04:11

thepirat000