Call on everyone to improve an IM system---------C#+JS (ii)-------service-side package Redis

Source: Internet
Author: User

The service side of this IM, using Redis as the database

I started using the Redis SDK is Servicestack.redis, but it is said to be charged, the beta version can only send 1000 requests within one hours??

Later, I changed to use Stackexchange.redis as the SDK, and according to its characteristics carried out some encapsulation. Here's how to encapsulate and encapsulate the content.

Let's take a look at how the package is called (the final goal is to do so)

Use the relational database (Mysql,sqlserver. ), we all have a DAL layer, each class of the DAL layer, corresponding to a table, the method in the class, it is this table additions and deletions of the check.

Redis, a non-relational database, does not have the concept of a table, it has only one library concept. I just contacted Redis and I like to think of this library as a table in SQL Server.

But the Redis storage data uses the KEY,VALUE structure, so I have no way to build a DAL layer to encapsulate the operations of each library in the previous layered way. Although it is possible to do so, it is not worth it.

The operation of the Redis class package should be based on the type of operation (increase, delete, change, check), so should be encapsulated in these classes (Add,delete,update,get), here we remove the Update. Update is not required here. When Redis assigns a value to the same key, it overrides its value, without the practice of a small part of the update.

I encapsulated it like this (theOpretionbase class, which is their parent class, the parent class records the generic context )

The context in opretionbase is all defined in the Stackexchange.redis. Iserver is used for the library's overview query, ITransaction for transactional operations, idatabase for general operations.

Here's the last class: The Opretionpublic class (only it provides calls externally, and here I have no way to correspond to a design pattern, only to put a diagram, you understand.) But external calls are simple)

  Public classopretionpublic:idisposable {//do not instantiate this class multiple times, or there will be bugs        Private Static ReadOnlyConnectionmultiplexer _client =Connectionmultiplexer.connect (Config.Config.redisHost); Private StaticIServer _server =_client.        Getserver (Config.Config.redisIP, Config.Config.redisPort); Private StaticList<opretionpublic> _options =NewList<opretionpublic>(); Private Static Objectobj =New Object(); //The Redis database has no name and can only be described by a number         Public StaticOpretionpublic Getoperation (intdb)            {Monitor.Enter (opretionpublic.obj); varOP = _options. FirstOrDefault (o = o._db = =db); if(OP! =NULL) {monitor.exit (opretionpublic.obj); returnop; }            Else            {                varNewop =Newopretionpublic (DB); _options.                ADD (NEWOP);                Monitor.Exit (Opretionpublic.obj); returnNewop; }        }         PublicAdd Add {Get;Set; }  PublicDelete Delete {Get;Set; }  PublicGet get {Get;Set; } PrivateITransaction _tran; Private int_db {Get;Set; } InternalOpretionpublic (intdb) {_db=DB; varDatabase =_client.            Getdatabase (DB); _tran=database.            CreateTransaction (); ADD=NewAdd (Database, _tran, _server); Delete=NewDelete (Database, _tran, _server); Get=NewGet (Database, _tran, _server); }        voidIDisposable.Dispose () {//Add.dispose (); //Delete.dispose (); //get.dispose ();        }         Public voidCommittran () {_tran.        Execute (); }    }

Now let's show you how external calls are made (does this call have the taste of EF?) But the cross-library transactions here are not supported because the context is created by library and each library has its own corresponding context .

Call on everyone to improve an IM system---------C#+JS (ii)-------service-side package Redis

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.