RedisTypedTransaction IncrementValue return value

Hello!

I would like to use IncrementValue in Redis Typed Transaction but I always get 0 in return value.
How can I get the icremented value before the transaction commit with typed client?

This is my code:

using (IRedisClient redisClient = this.RedisClientsManager.GetClient())
{
    ServiceStack.Redis.Generic.IRedisTypedClient<DataManagementEntity> redisEntity = redisClient.As<DataManagementEntity>();
    ServiceStack.Redis.Generic.IRedisList<DataManagementEntity> redisDataManagements = redisEntity.Lists[string.Format(urnDataManagementPattern, entity.Name)];

    using (ServiceStack.Redis.Generic.IRedisTypedTransaction<DataManagementEntity> trans = redisEntity.CreateTransaction())
    {
        trans.QueueCommand(r => entity.Id = r.IncrementValue(string.Format(seqDataManagementPattern, entity.Name)));
        trans.QueueCommand(r => r.AddItemToList(redisDataManagements, entity));

        trans.Commit();
    }
}

Thanks,
Tom

You can’t, everything within a Redis Transaction is executed atomically, so you’ll only be able to access the result once the transaction has executed successfully.

What you can do is perform any operations the transaction needs outside the transaction and then watch the key which will prevent the transaction from executing if any of the “watched keys” were changed before the transaction starts executing:

var key = string.Format(seqDataManagementPattern, entity.Name);

var id = redisClient.IncrementValue(key);

redisClient.Watch(key);

using (var trans = redisEntity.CreateTransaction()))
{
  //..
}

Thank you for your support.

1 Like