C # install and use Redis,
Redis is a widely used Key/Value memory database and one of the fastest key-value Distributed caches.
Redis Official Website: http://redis.io/
Redis Quick Start Tutorial: http://www.yiibai.com/redis/redis_quick_guide.html
Unzipping: https://github.com/dmajkic/redis/downloads
Installation: https://github.com/rgl/redis/downloads
ServiceStack. Redis: https://github.com/ServiceStack/ServiceStack.Redis
Refer:
Http://www.cnblogs.com/zhangweizhong/p/4969240.html
Http://blog.csdn.net/qiujialongjjj/article/details/16945569
1. Install Redis
Select a version to download. The package contains 32-bit and 64-bit Installation Tools, or you can directly use the installation version for installation. The installation result is the same.
After installation, a Redis Server System Service is displayed in the system service.
Redis-server.exe: daemon boot program for the server.
Redis. conf: configuration file.
Redis-cli.exe: command line client.
Redis-check-dump.exe: local database check
Redis-check-aof.exe: Update log check
Redis-benchmark.exe: performance testing tool to test the read/write performance of Redis in your system and your configuration.
Ii. Redis Configuration
1. port number, for example, 6379
2. Access address bound to the bind instance 127.0.0.1
3. requirepass Password
4. Remember to open this configuration node for maxheap. Otherwise, the redis service cannot be started. For example, maxheap 1024000000
5. timeout: Request timeout
6. logfile: Location of the log file
7. databases: number of databases Enabled
8. dbfilename: Data snapshot file name (only file name, excluding directory)
Iii. Redis usage
Find the redis-cli.exe file in the redisinstallation directory and double-click it to start it:
Test OK. Now the Redis installation is complete, and the next step is the practical application in C.
Ii. Redis practice
1. Create a project and reference the four class libraries related to ServiceStack. Redis.
2. Configuration File
<!-- redis Start --> <add key="SessionExpireMinutes" value="180" /> <add key="redis_server_session" value="127.0.0.1:6379" /> <add key="redis_max_read_pool" value="3" /> <add key="redis_max_write_pool" value="1" /> <!--redis end-->
3. Create a public RedisCacheHelper for Redis operations
Using ServiceStack. redis; using System. collections. generic; using System. configuration; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleApplication {class RedisCacheHelper {private static readonly PooledRedisClientManager pool = null; private static readonly string [] redisHosts = null; public static int RedisMaxReadPool = int. parse (ConfigurationManager. A PpSettings ["redis_max_read_pool"]); public static int RedisMaxWritePool = int. parse (ConfigurationManager. deleetask[ "redis_max_write_pool"]); static RedisCacheHelper () {var redisHostStr = ConfigurationManager. deleetask[ "redis_server_session"]; if (! String. isNullOrEmpty (redisHostStr) {redisHosts = redisHostStr. split (','); if (redisHosts. length> 0) {pool = new PooledRedisClientManager (redisHosts, redisHosts, new partition () {MaxWritePoolSize = RedisMaxWritePool, MaxReadPoolSize = RedisMaxReadPool, AutoStart = true });}}} public static void Add <T> (string key, T value, DateTime expiry) {if (value = null) {return;} if (e Xpiry <= DateTime. Now) {Remove (key); return;} try {if (pool! = Null) {using (var r = pool. GetClient () {if (r! = Null) {r. sendTimeout = 1000; r. set (key, value, expiry-DateTime. now) ;}}} catch (Exception ex) {string msg = string. format ("{0 }:{ 1} exception! {2} "," cache "," Storage ", key) ;}} public static void Add <T> (string key, T value, TimeSpan slidingExpiration) {if (value = null) {return;} if (slidingExpiration. totalSeconds <= 0) {Remove (key); return ;}try {if (pool! = Null) {using (var r = pool. GetClient () {if (r! = Null) {r. sendTimeout = 1000; r. set (key, value, slidingExpiration) ;}}} catch (Exception ex) {string msg = string. format ("{0 }:{ 1} exception! {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. sendTimeout = 1000; obj = r. get <T> (key) ;}}} catch (Exception ex) {string msg = string. format ("{0 }:{ 1} exception! {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. sendTimeout = 1000; r. remove (key) ;}}} catch (Exception ex) {string msg = string. format ("{0 }:{ 1} exception! {2} "," cache "," delete ", key) ;}} public static bool Exists (string key) {try {if (pool! = Null) {using (var r = pool. GetClient () {if (r! = Null) {r. sendTimeout = 1000; return r. containsKey (key) ;}}} catch (Exception ex) {string msg = string. format ("{0 }:{ 1} exception! {2} "," cache "," Whether ", key) ;}return false ;}}}View Code
4. program testing
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleApplication {class Program {static void Main (string [] args) {Console. writeLine ("Redis write cache:"); RedisCacheHelper. add ("name1", "test1", DateTime. now. addDays (1); Console. writeLine ("Redis get cache:"); string str = RedisCacheHelper. get <string> ("name1"); Console. writeLine (str); Console. readKey ();}}}View Code
5. input results
Download Demo