(reproduced). NET using Redis (ii) How to use Redis in C #

Source: Internet
Author: User

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 #

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.