Overview
Redis is a database of open source, Key-value (dictionary, Remote Dictionary Server, long-range dictionary server) that can be memory-based, and can be persisted in the C language, which supports network interaction.
Client: http://redis.io/clients
Command:http://redis.io/commands http://redisdoc.com
. NET Development Program Configuration
- ServiceStack.Common.dll
- ServiceStack.Interfaces.dll
- ServiceStack.Redis.dll
- ServiceStack.Text.dll
Program Configuration Redis Service IP and port
Static New Redisclient ("192.168.100.118"6379
Double-click Run: Redis-server.exe
Introduction to Redis Desktop Manager
The Redis Desktop Manager (REDISDESKTOPMANAGER,RDM) is a fast, simple, cross-platform, Redis-based management tool built on Qt 5 (a cross-platform C + + graphical user Interface application framework) that supports SSH Tunnel connection.
: Http://redisdesktop.com/download
To configure the Redis service address:
To view the value of the visual keys:
C # operates 5 types of basic data type 1. String
A: Store normal string and set expiration time
int expiretime = 5000;//5S
Storage: Client. Add<string> ("Stringkey", "StringValue", DateTime.Now.AddMilliseconds (Expiretime));
Get: Client. Get<string> ("Stringkey"), DateTime.Now);
B: Storage Class object
Student stud = new Student () {id = "$", name = "Zhang San"};
Storage: Client. Add<student> ("stringentity", stud);
Get: Student Get_stud = client. Get<student> ("stringentity");
Test Case Output Results:
2. Hashing
Storage: Client. Setentryinhash ("Hashid", "Name", "Zhang San");
A: Traverse the keys with a Hashid value of Hashid
Get:list<string> Haskkey = client. Gethashkeys ("Hashid");
B: Traverse values that Hashid value to Hashid
Get:list<string> haskvalue = client. Gethashvalues ("Hashid");
C: Traverse all keys
Get:list<string> Allkey = client. Getallkeys ();
Test Case Output Results:
3. Linked List
A: Queue
Queue: Client. Enqueueitemonlist ("Queuelistid", "1");
Queue: Long q = client. GetListCount ("Queuelistid");
Client. Dequeueitemfromlist ("Queuelistid"));
B: Stack
Into the stack: client. Pushitemtolist ("Stacklistid", "1");
Out of stack: client. Popitemfromlist ("Stacklistid")
Test Case output:
4. Unordered collection
Storage: Client. Additemtoset ("SetA", "1");
Get:hashset<string> SetA = client. Getallitemsfromset ("SetA");
A: and set
Hashset<string> hashunion = client. Getunionfromsets (new string[] {"SetA", "Setb"});
B: Intersection
Hashset<string> intersectset = client. Getintersectfromsets (new string[] {"SetA", "Setb"});
C: Difference Set
Hashset<string> SETOFDIFFSETATOSETB = client. Getdifferencesfromset ("SetA", new string[] {"SETB"});
Test Case output:
5. Ordered collection
Storage: Client. Additemtosortedset ("setsorted", "A");
Output:list<string> listsetsorted = client. Getallitemsfromsortedset ("setsorted");
Test Case output:
Redis Application Scenarios
It's just a personal view of the scenario I used when using Redis.
A. Grab the prize stock of XXX coupons and sweepstakes system, use the linked list in Redis
The previous night through the timer service to push the prize inventory, using the Lpush command to push the random order of prizes into the list, the lottery call Lpop command, the leftmost prize pop-up queue, prompting users to win. At the same time, send an asynchronous message to get the message to process the winning record and insert it into the relational database.
Benefits:
The operation speed of the team is very fast, it can satisfy the scene of multi-person concurrent lottery.
The use of Message Queuing avoids database concurrency.
B. An activity accumulated to recharge xxx yuan, reward xxx. Using the String/hash (hash) structure in Redis
Each time the user recharge, send a Recharge MQ event (using RABBITMQ), another program, the consumer recharge MQ event, the user ID in the event, the recharge amount is stored in Redis (String/hash).
Later, you can directly summarize the user's total recharge amount to meet the conditions of the customer gift prizes.
Benefits:
Completely avoids the query insert operation of the relational database
Redis queries are fast and improve the user experience
Extended Reading
1. Redis Persistent Rdb and aof http://my.oschina.net/davehe/blog/174662
2. Redis author on redis application scenario http://blog.nosqlfan.com/html/2235.html
3. Redis Usage Summary and memcached similarities and differences http://www.cnblogs.com/ceecy/p/3279407.html
4. Redis memory usage optimization and storage http://www.infoq.com/cn/articles/tq-redis-memory-usage-optimization-storage
5. Redis Learning Manual (catalogue) http://www.cnblogs.com/stephen-liu74/archive/2012/04/16/2370212.html
Demo Code Download: http://download.csdn.net/detail/jys1216/8991915
Redis Demo and Usage scenarios