user60456
user60456

Reputation:

Servicestack.redis Transactions and Hashes

How can I get all the entries from a hash while in a transaction? I don't see an onSuccessCallback with the right type. I tried mapping it as a byte[][] thinking I could just manually deserialize it, but redis complains about that (Operation against a key holding the wrong kind of value)

Is there anyway to do this?

var hashValues
using (var trans = client.CreateTransaction())
{
    trans.QueueCommand(c => hashValues=c.GetAllEntriesFromHash("some key"));
    trans.Remove("some key");
    trans.Commit();
}

return hashValues;

So what I am trying to do is an atomic operation of getting all the values from the hash and then removing that hash.

Upvotes: 1

Views: 947

Answers (1)

paaschpa
paaschpa

Reputation: 4816

Best way I could come up with is below. Based on this from Redis it looks like the return from Redis is fieldname followed by it's value. ServiceStack.Redis takes care of the dictionary mapping for you. In order to get everything to work within a transaction I think you have to work at the 'Redis' level.

byte[][] hashAll = null;

var client = new BasicRedisClientManager("localhost:6379");
using (var trans = client.GetClient().CreateTransaction())
{
    trans.QueueCommand(r => ((RedisNativeClient)r).HGetAll("meHashKey"), result => hashAll = result);         
    trans.QueueCommand(r => r.Remove("meHashKey"));
    trans.Commit();
}

//variable map will hold the Hash/Dictionary<string, string>/Key-Value Pairings
var map = new Dictionary<string, string>(); 
for (var i = 0; i < hashAll.Length; i += 2)
{
    var key = hashAll[i].FromUtf8Bytes();
    map[key] = hashAll[i + 1].FromUtf8Bytes();
}

Upvotes: 3

Related Questions