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