Redis installation and C # calls

Source: Internet
Author: User
Tags redis cluster

I. Introduction of Redis

Redis is an open source (BSD licensed), memory-stored data structure server that can be used as a database, cache, and Message Queuing agent. It supports data types such as strings, hash tables, lists, collections, ordered collections, bitmaps, hyperloglogs, and so on. Built-in replication, LUA scripting, LRU retract, transactions, and different levels of disk persistence, while providing high availability through Redis Sentinel, automatic partitioning via Redis cluster. Sd

Redis official web-related installation, command, configuration and other information written already very clear (see official website) I will briefly introduce.

Second, download the installation

(1) Download

The Windows environment produces good files (the version here is old.) There are other ways you can download files that others have created. Here are redis-2.8.17), where the version may have been relatively old, the new edition can download its own source code generation github.com (MSVs folder has a solution can be generated Vs2013, here need to update to UPDATE5).

Download the file to unzip the directory as follows:

(2) Installation:

Several more common profiles in configuration files

1. Port 6379 Port number

2. Bind 127.0.0.1 IP

3. Password for requirepass access

4. Maxheap Remember to open this configuration node and the Redis service will not start. For example Maxheap 1024000000 (I need to comment out this node when I test myself with 3.0.504)

5. Timeout: Request time-out

Additional configuration instructions. The configuration file has a clearer description in English. Can't you read English? Never mind. The download has a Chinese description.

Configure the configuration file. Just click on the startup.bat to get it right. At this point you will see the following

When you see this page is explained. Your service has been successfully launched. (This page cannot be closed.) Shut down the service then stopped)

If you don't have a flash, you don't. You may be configured or in a different error. We will honestly enter the cmd-"file directory-" execute the command. Find problems and resolve problems based on logs

Let's do a storage test.

Open a different window (the service window cannot be closed)

The test results are as follows

The client statement is as follows

$ redis-cli-h host-p port-a Password

If you need to load-balance multiple machines, you only need to configure the information on another machine.
# slaveof <masterip> <masterport> This node will be configured on it.

Third, C # calls

1. Download the relevant DLL

2, common methods of finishing

public class Rediscachehelper {private static readonly Pooledredisclientmanager pool = null;      private static readonly string[] writehosts = null;      private static readonly string[] readhosts = null; public static int redismaxreadpool = Int.      Parse (configurationmanager.appsettings["Redis_max_read_pool"]); public static int redismaxwritepool = Int.      Parse (configurationmanager.appsettings["Redis_max_write_pool"]);          Static Rediscachehelper () {var rediswritehost = configurationmanager.appsettings["Redis_server_write"];          var redisreadhost = configurationmanager.appsettings["Redis_server_read"]; if (!string.              IsNullOrEmpty (Rediswritehost)) {writehosts = Rediswritehost.split (', ');              readhosts = Redisreadhost.split (', ');                      if (Readhosts.length > 0) {pool = new Pooledredisclientmanager (writehosts, Readhosts,      New Redisclientmanagerconfig ()                {maxwritepoolsize = Redismaxwritepool, maxreadpoolsize              = Redismaxreadpool, AutoStart = true}); }}} public static void Add<t> (string key, T value, DateTime expiry) {if (value =          = null) {return;                } if (expiry <= datetime.now) {Remove (key);          Return The try {if (pool! = null) {using (var r = Pool). Getclient ()) {if (r! = null) {R.send                          Timeout = 1000;                      R.set (key, value, Expiry-datetime.now); }}}} catch (Exception ex) {string msg = STRING.F Ormat ("{0}:{1} exception occurred!{ 2} "," Cache ", "Storage", key); }} public static void Add<t> (string key, T value) {rediscachehelper.add<t> (key,      Value, DateTime.Now.AddDays (1)); public static void Add<t> (string key, T value, TimeSpan slidingexpiration) {if (value = = nul          L) {return;              } if (slidingexpiration.totalseconds <= 0) {Remove (key);          Return The try {if (pool! = null) {using (var r = Pool). Getclient ()) {if (r! = null) {R.send                          Timeout = 1000;                      R.set (key, value, slidingexpiration); }}}} catch (Exception ex) {string msg = STRING.F Ormat ("{0}:{1} exception occurred!{          2} "," Cache "," Storage ", key); }} PUBlic static T get<t> (string key) {if (string.          IsNullOrEmpty (key)) {return default (T);          } T obj = default (t); try {if (pool! = null) {using (var r = Pool. Getclient ()) {if (r! = null) {R.send                          Timeout = 1000;                      obj = r.get<t> (key); }}}} catch (Exception ex) {string msg = STRING.F Ormat ("{0}:{1} exception occurred!{          2} "," Cache "," get ", key);      } return obj;              } public static void Remove (string key) {try {if (pool! = null) {using (var r = Pool. Getclient ()) {if (r! = null) {R.send          Timeout = 1000;                R.remove (key); }}}} catch (Exception ex) {string msg = STRING.F Ormat ("{0}:{1} exception occurred!{          2} "," Cache "," delete ", key);              }} public static bool Exists (string key) {try {if (pool! = null) {using (var r = Pool. Getclient ()) {if (r! = null) {R.send                          Timeout = 1000;                      return R.containskey (key); }}}} catch (Exception ex) {string msg = STRING.F Ormat ("{0}:{1} exception occurred!{          2} "," Cache "," presence ", key);      } return false;           } public static idictionary<string, t> getall<t> (ienumerable<string> keys) where T:class { if (keys = = null) {return null; } keys = keys. Where (k =!string.            Isnullorwhitespace (k)); if (keys. Count () = = 1) {T obj = get<t> (keys.                single ()); if (obj! = null) {return new dictionary<string, t> () {keys.              Single (), obj}};          } return null; } if (!keys.          Any ()) {return null; The try {using (var r = Pool).                      Getclient ()) {if (r! = null) {r.sendtimeout = 1000;                  return r.getall<t> (keys); }}} catch (Exception ex) {string msg = string. Format ("{0}:{1}" exception occurred!{ 2} "," Cache "," get ", keys.          Aggregate ((A, B) + A + "," + b));      } return null;   }  }

Setting Configuration information

Then you can

Rediscachehelper.add<string> (key, value);

This invokes the.

Redis installation and C # calls

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.