Redis Learning note ~redis Transaction mechanism and implementation of LIND.DDD.REPOSITORIES.REDIS transaction mechanism

Source: Internet
Author: User

Back to Catalog

Redis itself supports transactions, which is that SQL databases have transaction, and Redis's driver supports transactions, which is reflected in Servicestack.redis, which is currently the most industry-recognized Redis driver, and it will be the Redis transaction mechanism ( MULTI , Exec,watch, etc.) are packaged into a more friendly implementation, such as the following code

    using (Iredisclient rclient = prcm. Getclient ())    {      using (iredistransaction IRT = rclient.createtransaction ())        {            IRT. Queuecommand (r = r.additemtolist ("Zzl", "2"));
Irt. Queuecommand (r = r.additemtolist ("LR", "2")); // Commit a transaction } }

Of course, the beautiful code above some credit to the C # beautiful syntax, you can not write in Java so beautiful things, of course, the above code is Servicestack.redis for us, usually we can use directly, Now again, Uncle Lind.ddd in the framework of the redisrepository support for it!

If Uncle Redisrepository wanted to support Redis transactions, the premise: the iredisclient of warehousing must be the same object as the iredisclient that generated the transaction, otherwise Redis transactions would not work in the uncle's frame.

Implementation method:

A redisrepository<t> implementation of the Setdatacontext method, the iredisclient from the outside, so that can save the transaction and warehousing is an object

 Public voidSetdatacontext (Objectdb) {            Try            {                //manual Redis database objects, enabling on Redis transactionsRedisdb =(iredisclient) DB; Redistypedclient= redisdb.gettypedclient<tentity>(); Table= redistypedclient.lists[typeof(TEntity).            Name]; }            Catch(Exception) {Throw NewArgumentException ("Redis. Setdatacontext Required db for iredisclient type"); }        }

Two add Redis-based transaction managers, so that the better combination of the Uncle Warehousing and business, to facilitate the use of developers

    /// <summary>    ///Redis transaction Management mechanism/// </summary>     Public classRedistransactionmanager {/// <summary>        ///Transaction block Processing/// </summary>        /// <param name= "Redisclient" >Current Redis Library</param>        /// <param name= "Action" >actions in a transaction</param>         Public Static voidTransaction (iredisclient redisclient, Action action) {using(Iredistransaction IRT =redisclient.createtransaction ()) {                Try{action ();                Irt.commit (); }                Catch(Exception) {IRT.                Rollback (); }            }        }    }

Three in the domain code, we can usually use the Uncle Redis's transaction block, see the code

            varRedis =NewLind.ddd.repositories.redis.redisrepository<user>(); Iredisclient redisclient=Lind.DDD.RedisClient.RedisManager.GetClient (); Redis.            Setdatacontext (redisclient); Lind.DDD.RedisClient.RedisTransactionManager.Transaction (Redisclient, ()={Redis. Insert (NewUser {UserName ="gogod111" }); Redis. Insert (NewUser {UserName ="gogod211" }); });

In this way, the Uncle Framework support Redis's business, I hope MongoDB can also support the transaction at an early stage, to that time, the uncle will provide it with a mechanism for implementation, hehe!

Here is the uncle of the distributed multi-data source transaction testing, can implement SQL Server and Redis transaction coexistence mechanism, the following is the code

Lind.DDD.RedisClient.RedisTransactionManager.Transaction (redisclient, () ={Redis. Insert (NewUser {UserName ="gogod111" }); Redis. Insert (NewUser {UserName ="gogod211" }); using(vartrans =NewTransactionScope ()) {Userrepository.insert (NewUserInfo {UserName ="Zzl3" });                Trans.complete (); }            });

The above code we can also do some encapsulation, some modifications, let IT support Redis and SQL two kinds of transactions, using the. net4.5 default parameters, you can save a method of overloading, the code is more and more concise!

        /// <summary>        ///Transaction block Processing/// </summary>        /// <param name= "Redisclient" >Current Redis Library</param>        /// <param name= "Redisaction" >actions in a Redis transaction</param>        /// <param name= "Sqlaction" >actions in a SQL transaction</param>         Public Static voidTransaction (iredisclient redisclient, action redisaction, action sqlaction =NULL)        {            using(Iredistransaction IRT =redisclient.createtransaction ()) {                Try{redisaction (); if(Sqlaction! =NULL)                    {                        using(vartrans =NewTransactionScope ())                            {sqlaction ();                        Trans.complete ();                }} irt.commit (); }                Catch(Exception) {IRT.                Rollback (); }            }        }

Code in the call, we are very convenient, simple!

 Lind.DDD.RedisClient.RedisTransactionManager.Transaction (Redisclient, () => {Redis. Insert ( new  User {UserName =  " Span style= "color: #800000;"                >gogod111   }); Redis. Insert ( new  User {UserName =  " Span style= "color: #800000;"            >gogod211   }); }, ()  => {Userrepository.insert ( new  UserInfo {UserName =  " zzl3  Span style= "color: #800000;"            > " }); });

For the C # code team's progress, but also our programmers love it one of the reasons, after all, people have a tired time, a lot of improvement, for themselves, for others is a good thing!

Back to Catalog

Redis Learning note ~redis Transaction mechanism and implementation of LIND.DDD.REPOSITORIES.REDIS transaction mechanism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.