Stackexchange.redis Client

Source: Internet
Author: User

Stackexchange.redis Client

In this issue StackExchange.Redis , this is one of the Redis's. NET clients. Redis is an open-source memory data store that can be used as a database, cache, or message broker service. There are many people using ServiceStack.Redis this. NET client, but the latest version of this is now a commercial software. For ServiceStack.Redis This kind of behavior, we have nothing to say, the choice left to us is to use the lower version of the open source version or to move to other clients.

When it comes to Stackexchange.redis, we have to say that it has a relationship with Booksleeve. Booksleeve is already a complete Redis SDK, but why does the Booksleeve author want to re-write a Redis client SDK? Interested students can look here why I wrote another Redis client summed up in fact, a word: feel uncomfortable on the re-demolition.

(╯????? ‵) ╯︵┴─┴ (╯-_-) ╯╧╧ (╯‵-′) ╯︵┴─┴ (╯ '-') ╯︵┻━┻┬─┬ノ

Stackexchange.redis Installation

Install-Package StackExchange.Redis

If you need a strong signed version go to the following command, of course, the author of the strong signature is also full of resentment

PM> Install-Package StackExchange.Redis.StrongName
Connectionmultiplexer

The Connectionmultiplexer object is the most central object of the Stackexchange.redis. Instances of this class need to be shared and reused throughout the application domain, and you do not keep creating instances of the object in each operation, so it is necessary to use Singleton to create and store this object.

    PublicStatic Connectionmultiplexer Manager {get {if (_redis = null) {lock (_locker {if (_redis! = null) return _ Redis _redis = GetManager (); return _redis;} } return _redis;}} private static Connectionmultiplexer getmanager ( String connectionString = null) {if ( string. IsNullOrEmpty (connectionString)) {connectionString = Getdefaultconnectionstring ();} return connectionmultiplexer.connect (connectionString);}      

Although Connectionmultiplexer implements the IDisposable interface, we generally do not need to release it, based on the consideration of reuse.

Use as a memory database
IDatabase db = redis.GetDatabase();

The DB object returned by Getdatabase () Here is lightweight and does not need to be cached, each time it is taken. All methods of idatabase have synchronous and asynchronous implementations. Asynchronous implementations of these can be await.

The encapsulation of some underlying operations.

PublicboolRemove(String key) {key = MergeKey (key);var db = RedisManager.Manager.GetDatabase (Database);Return DB. Keydelete (key); }PublicStringGet(String key) {key =This. MergeKey (key);var db = RedisManager.Manager.GetDatabase (Database);Return DB. Stringget (key); }PublicboolSet(String key,StringValueint expireminutes =0) {key = MergeKey (key); var db = RedisManager.Manager.GetDatabase (Database); if (expireminutes > 0) {db. Stringset (Key, value, Timespan.fromminutes (expireminutes));} else {db. Stringset (Key, value);} return db. Stringset (Key, value);} public bool haskey (string key) {key = MergeKey ( Key); var db = RedisManager.Manager.GetDatabase (Database); return db. Keyexists (key); } 

Here the MergeKey prefix used for stitching Key , the specific different business modules using different prefixes.

There ServiceStack.Redis is a big difference here that there is no default connection pool management. There is no connection pool naturally has its pros and cons, the greatest benefit is that waiting time to obtain the connection is not, and will not because the connection pool inside the connection due to the lack of proper release and other reasons caused by infinite wait for the deadlock state. The downside is that some low-quality code can cause server resources to run out. However, the means to provide blocking and waiting for connection pooling are contrary to the author's design philosophy. Stackexchange.redis here uses a pipeline and multiplexing technology to achieve reduced connectivity, which is discussed later.

Use as Message Broker middleware

In the message formation, the important concept is producer, consumer, message middleware.

sub = redis.GetSubscriber();

First, get a Isubscriber object first. On the producer side we post a message:

sub.Publish("messages", "hello");

To get the message on the consumer side and output

sub.Subscribe("messages", (channel, message) => {    Console.WriteLine((string)message);});

A more specialized message queue is typically used to handle this business scenario, so this is skipped.

Three Modes of command

Sync vs Async vs Fire-and-forget

Finally, there are three StackExchange.Redis different types of usage scenarios for each of the three command modes.

Sync, synchronous mode blocks the caller directly, but obviously does not block other threads.

Async, the asynchronous pattern goes directly to the task model.

Fire-and-forget, is to send a command, and then completely do not care when the final time to complete the command operation.

db.StringIncrement(pageKey, flags: CommandFlags.FireAndForget);

It is worth noting that in Fire-and-forget mode, all commands immediately get the return value, which is, of course, the default value of the return value type, such as if the operation return type is bool will get false immediately, because false = Default (BOOL).

Stackexchange.redis Client

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.