ServiceStack.Redis常用操作 - 事務、並發鎖


一、事務

  使用IRedisClient執行事務示例:

    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"));
    }

 

二、並發鎖

  使用IRedisClient申請鎖示例:

    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"));
        }
    }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM