Stackexchange.redis Use (i)

Source: Internet
Author: User


The most important object in Stackexchange.redis is the Connectionmultiplexer class, which exists in the Stackexchange.redis namespace.
This class hides the operation details of the Redis service, and the Connectionmultiplexer class does a lot of things, which are designed to be shared and reused across all calls.
You should not create a connectionmultiplexer for each operation. Connectionmultiplexer is thread-safe, it is recommended to use the following method.
In all subsequent examples, it is assumed that you have instantiated a connectionmultiplexer class that will always be reused,
Now let's create a connectionmultiplexer instance. It is through Connectionmultiplexer.connect or Connectionmultiplexer.connectasync,
Pass a connection string or a Configurationoptions object to create.
The connection string can be a node of multiple services separated by commas, we just need to connect to a Redis service on the local computer, and the default port for the Redis service is 6379.

Using Stackexchange.redis;
...
Connectionmultiplexer Redis = Connectionmultiplexer.connect ("localhost");
^^ ^ store and re-use this!!!

Note: Connectionmultiplexer implements the IDisposable interface when we no longer need to be able to release it, here I deliberately do not use using to release him. Simply speaking, creating a connectionmultiplexer is very expensive, and a good idea is that we always reuse a Connectionmultiplexer object.
A complex scenario may contain a master-slave copy, in which case you only need to specify all addresses in the connection string (it will automatically recognize the primary server)
Connectionmultiplexer Redis = Connectionmultiplexer.connect ("server1:6379,server2:6379");

Assuming that two primary servers are found here, it is very rare that the two services will be ruled out as a primary server to solve this problem, and we should avoid this situation.


Now that you have a connectionmultiplexer, here are three things you might want to do.

1. Access the database. (Note that in the case of a cluster, a database may be partial to multiple nodes)
2. Using the Publish subscription feature of Redis
3. Maintenance and monitoring of a single server

Accessing the database
Accessing the database is simple:

Idatabase db = Redis. Getdatabase ();

Getdatabase returns a Idatabase interface. Note Redis allows you to configure multiple databases, and you can specify the database when you call Getdatabase (). Also, if you plan to use an asynchronous API, Task.asyncstate must have a value, or you can specify it.

int Databasenumber = ...
Object asyncstate = ...
Idatabase db = Redis. Getdatabase (Databasenumber, asyncstate);

Now that you have a Idatabase object, he can manipulate the Redis database. All methods have synchronous and asynchronous two versions, and according to Microsoft's naming convention, all async methods end in Async.

The simplest operation stores and gets a value.

String value = "ABCDEFG";
Db. Stringset ("MyKey", value);
...
String value = db. Stringget ("MyKey");
Console.WriteLine (value); Writes: "ABCDEFG"

The string prefix represents a string type in Redis, which differs greatly from the string type in. NET, although both can hold string types. Redis then allows key values to be binary data, as in the following example:

byte[] key = ..., value = ...;
Db. Stringset (key, value);
...
Byte[] Value = db. Stringget (key);

Stackexchange.redis supports all Redis shell commands and can refer to the Redis official website.


Stackexchange.redis Use (i)

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.