Usage of Redis Cache server in Windows

Source: Internet
Author: User
Tags redis desktop manager
The Redis Cache server is a keyvalue database that reads 110000 s and writes 81000 s. it is fast because it is a memory operation, common usage is to store user tokens, text message verification codes, and other official websites to show that Redis itself does not have a Windows version, Microsoft officially developed a Windows-based Redis server: MSOpenTechredis I, Redis server


First download the Redis server, click to download the. msi version, double-click to install the Redis server, and start it with the system as a service:

The tree on the left shows that there is already a connection. Click Connect to Redis Server at the bottom to add another connection:

Name: connection Name.

Host: Host address, which is 127.0.0.1.

Port: Port. default Port 6379 of the Redis server

Auth: Password. if it is set, it will be lost. leave it blank if it is not set.

Connect to the Redis server and you will see that the default 16 databases (the configuration file can be changed), and the index starts from 0. A common usage is to create a library for a project. different functional modules under the project are stored in different directories.

With the visualization tool, you can simply double-click it and right-click it to create or delete it... This tool will be used in all Windows systems. Compared with the command line, the Redis Desktop Manager visualization tool is more user-friendly and easier to debug data on remote servers.

Note: the local machine can be connected to the remote server. you need to go to the Redis installation directory on the server and find redis. windows-service.conf file, find bind 127.0.0.1 plus "#" comment out, and then right-click the service to restart the redis service, or restart the Windows system

III. C # Operating Redis servers

The above are command line and visual tools to operate Redis server, C # program to operate Redis needs to use StackExchange. Redis (https://github.com/StackExchange/StackExchange.Redis), in order to unified call, encapsulates a RedisHelper help class:

Using Newtonsoft. Json; using StackExchange. Redis; using System. ComponentModel; using System. Configuration; using System. Reflection; using System. Text; namespace redis_Demo {////// Redis help class ///Public static class RedisHelper {private static string _ conn = ConfigurationManager. deleettings ["redis_connection_string"]? "127.0.0.1: 6379"; private static string _ pwd = ConfigurationManager. deleettings ["redis_connection_pwd"]? "123456"; static ConnectionMultiplexer _ redis; static readonly object _ locker = new object (); # region Singleton mode public static ConnectionMultiplexer Manager {get {if (_ redis = null) {lock (_ locker) {if (_ redis! = Null) return _ redis; _ redis = GetManager (); return _ redis;} private static ConnectionMultiplexer GetManager (string connectionString = null) {if (string. isNullOrEmpty (connectionString) {connectionString = _ conn;} var options = ConfigurationOptions. parse (connectionString); options. password = _ pwd; return ConnectionMultiplexer. connect (options) ;}# endregion ////// Add //////Directory///Key///Value///Expiration Time, in minutes. 600 minutes by default///Library, the first database by default. 0 ~ 16 databases in total///
 Public static bool StringSet (CacheFolderEnum folder, string key, string value, int expireMinutes = 600, int db =-1) {string fd = GetDescription (folder); return Manager. getDatabase (db ). stringSet (string. isNullOrEmpty (fd )? Key: fd + ":" + key, value, TimeSpan. FromMinutes (expireMinutes ));}////// Get //////Directory///Key///Library, the first database by default. 0 ~ 16 databases in total///
 Public static string StringGet (CacheFolderEnum folder, string key, int db =-1) {try {string fd = GetDescription (folder); return Manager. getDatabase (db ). stringGet (string. isNullOrEmpty (fd )? Key: fd + ":" + key) ;}catch (Exception) {return null ;}}////// Delete //////Directory///Key///Library, the first database by default. 0 ~ 16 databases in total///
 Public static bool StringRemove (CacheFolderEnum folder, string key, int db =-1) {try {string fd = GetDescription (folder); return Manager. getDatabase (db ). keyDelete (string. isNullOrEmpty (fd )? Key: fd + ":" + key) ;}catch (Exception) {return false ;}}////// Whether it exists //////Key///Library, the first database by default. 0 ~ 16 databases in totalPublic static bool KeyExists (CacheFolderEnum folder, string key, int db =-1) {try {string fd = GetDescription (folder); return Manager. getDatabase (db ). keyExists (string. isNullOrEmpty (fd )? Key: fd + ":" + key) ;}catch (Exception) {return false ;}}////// Extension //////Directory///Key///Extended time. unit: Minute. the default value is 600 minutes.///Library, the first database by default. 0 ~ 16 databases in totalPublic static bool AddExpire (CacheFolderEnum folder, string key, int min = 600, int db =-1) {try {string fd = GetDescription (folder); return Manager. getDatabase (db ). keyExpire (string. isNullOrEmpty (fd )? Key: fd + ":" + key, DateTime. Now. AddMinutes (min);} catch (Exception) {return false ;}}////// Add an object //////Directory///Key///Entity///Extended time. unit: Minute. the default value is 600 minutes.///Library, the first database by default. 0 ~ 16 databases in totalPublic static bool Set
 
  
(CacheFolderEnum folder, string key, T t, int expireMinutes = 600, int db =-1) {string fd = GetDescription (folder); var str = JsonConvert. serializeObject (t); return Manager. getDatabase (db ). stringSet (string. isNullOrEmpty (fd )? Key: fd + ":" + key, str, TimeSpan. FromMinutes (expireMinutes ));}///
  /// Obtain the object //////
  Directory///
  Key///
  Library, the first database by default. 0 ~ 16 databases in totalPublic static T Get
  
   
(CacheFolderEnum folder, string key, int db =-1) where T: class {string fd = GetDescription (folder); var strValue = Manager. getDatabase (db ). stringGet (string. isNullOrEmpty (fd )? Key: fd + ":" + key); return string. IsNullOrEmpty (strValue )? Null: JsonConvert. DeserializeObject
   
    
(StrValue );}///
    /// Obtain the enumerated Description //////
    Enumerated value///
    If DescriptionAttribute is not defined for the enumerated value, whether to use the enumerated name instead. the default value is DescriptionAttribute.///
    
     
Enumeration Description
    Private static string GetDescription (this Enum value, Boolean nameInstead = true) {Type type = value. getType (); string name = Enum. getName (type, value); if (name = null) {return null;} FieldInfo field = type. getField (name); DescriptionAttribute attribute = Attribute. getCustomAttribute (field, typeof (DescriptionAttribute) as DescriptionAttribute; if (attribute = null & nameInstead = true) {r Eturn name;} return attribute = null? Null: attribute. Description ;}}}
   
  
 

Add a record with the key name and value wangjie to the fd1 Directory of the first database of the redis server:

RedisHelper.StringSet(CacheFolderEnum.Folder1, "name", "wangjie");

Obtain this record:

String key = RedisHelper. StringGet (CacheFolderEnum. Folder1, "name"); Console. WriteLine ("value of the record whose key is name:" + key );

Delete this record:

Bool result = RedisHelper. stringRemove (CacheFolderEnum. folder1, "name"); if (result) {Console. writeLine ("records whose key is name are successfully deleted");} else {Console. writeLine ("failed to delete the record whose key is name ");}

Query whether this record exists:

Bool ifExist = RedisHelper. keyExists (CacheFolderEnum. folder1, "name"); if (ifExist) {Console. writeLine ("the record with the key name exists");} else {Console. writeLine ("the record with the key name does not exist ");}

Add a record whose key is sd1 and whose value is an object to the fd2 Directory of the second redis database:

Student student = new Student () {Id = 1, Name = "zhang san", Class = "second Class"}; RedisHelper. Set
 
  
(CacheFolderEnum. Folder2, "sd1", student, 10, 1 );
 

Get this object:

Student sdGet = RedisHelper. Get
 
  
(CacheFolderEnum. Folder2, "sd1", 1); if (sdGet! = Null) {Console. writeLine ("Id:" + sdGet. id + "Name:" + sdGet. name + "Class:" + sdGet. class);} else {Console. writeLine ("records with sd1 key Not Found ");}
 

Source code: (http://files.cnblogs.com/files/oppoic/redis_Demo.zip)

The environment is VS 2013. if you cannot run it, copy the code in cs and compile and run it.


4. others

MSOpenTech developed the Redis Cache server with persistence. after writing, the computer key-value pair is restarted and still exists. Generally, you need to set the expiration time for writing the key-value pair. Otherwise, the memory that has been used for a long time will not be released. Redis storage methods include not only keys and strings, but also List and HashTable. of course, Redis uses more advanced commands in Linux.

For more articles about using Redis Cache servers in Windows, please refer to PHP!

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.