使用IRedisClient执行事务示例:

ServiceStack.Redis常用操作 - 事务、并发锁
    using (IRedisClient RClient = prcm.GetClient())
    {
        RClient.Add("key",1);
        using (IRedisTransaction IRT = RClient.CreateTransaction())
        {
            IRT.QueueCommand(r => r.Set("key", 20));
            IRT.QueueCommand(r => r.Increment("key",1)); 

            IRT.Commit(); // 提交事务
        }
        Response.Write(RClient.Get<string>("key"));
    }
ServiceStack.Redis常用操作 - 事务、并发锁

 

二、并发锁

使用IRedisClient申请锁示例:

ServiceStack.Redis常用操作 - 事务、并发锁
    using (IRedisClient RClient = prcm.GetClient())
    {
        RClient.Add("mykey",1);
        // 支持IRedisTypedClient和IRedisClient
        using (RClient.AcquireLock("testlock")) 
        {
            Response.Write("申请并发锁<br/>");
            var counter = RClient.Get<int>("mykey");

            Thread.Sleep(100);

            RClient.Set("mykey", counter + 1);
            Response.Write(RClient.Get<int>("mykey"));
        }
    }
ServiceStack.Redis常用操作 - 事务、并发锁

相关文章:

  • 2022-12-23
  • 2021-10-02
  • 2021-12-03
  • 2021-05-18
  • 2021-07-07
  • 2022-02-23
  • 2021-05-29
  • 2021-10-08
猜你喜欢
  • 2021-06-13
  • 2019-08-19
  • 2022-03-07
  • 2022-01-15
  • 2022-12-23
相关资源
相似解决方案