The Redis website offers many open-source C # clients. For example, Nhiredis, Servicestack.redis, Stackexchange.redis and so on. Among them, Servicestack.redis should be considered more popular. It provides a complete set of mechanisms for converting the object from Redis data structures to strongly typed objects and serializing the object json. So here we only introduce Servicestack.redis, which is also the client currently used in our products.
Servicestack.redis address : Https://github.com/ServiceStack/ServiceStack.Redis
1. Create a console application and refer to the following Servicestack.redis related four class libraries. Or install the Redis common components Servicestack.redis through NuGet. Download the sample code.
2. Create a common class rediscachehelper for Redis operations,
+ View Code
Description: Rediscachehelper uses the client link pool mode, which should be the highest access efficiency. At the same time, it is more convenient to support read/write separation and load balancing.
3. Configuration files
| 123456 |
<!-- 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--> |
4. Test program calls
| 1234567891011121314151617181920212223242526 |
classProgram { staticvoidMain(string[] args) { Console.WriteLine("Redis写入缓存:zhong"); RedisCacheHelper.Add("zhong", "zhongzhongzhong", DateTime.Now.AddDays(1)); Console.WriteLine("Redis获取缓存:zhong"); string str3 = RedisCacheHelper.Get<string>("zhong"); Console.WriteLine(str3); Console.WriteLine("Redis获取缓存:nihao"); string str = RedisCacheHelper.Get<string>("nihao"); Console.WriteLine(str); Console.WriteLine("Redis获取缓存:wei"); string str1 = RedisCacheHelper.Get<string>("wei"); Console.WriteLine(str1); Console.ReadKey(); } } |
5. Output results
(reproduced). NET using Redis (ii) How to use Redis in C #