[redis]c# Redis Cache Helper Class

Source: Internet
Author: User
Tags redis cluster redis server

UsingNservicekit.redis;UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;UsingSystem.Threading.Tasks;NamespaceH5. utility{///<summary>///Distributed cache///</summary>PublicStaticClassRediscache {Privatestatic Iredisclient rcclient =Null;///<summary>///Setting up the Redis cache///</summary>///<typeparam name= "T" >Generic class</typeparam>///<param name= "Key" >Cache key</param>///<param name= "Value" >Generic Entities</param>///<param name= "Expire" >Expiry time</param>///<returns></returns>PublicStaticBOOL Set<t> (StringKey, T value, DateTime expire) {Try{using (Rcclient =Getclient ()) {Return rcclient.set<t>(Key, value, expire); } }Catch{ReturnFalse; } }///<summary>///Get Cache///</summary>///<typeparam name= "T" >Entity</typeparam>///<param name= "Key" >Key value</param>///<returns></returns>PublicStatic T get<t> (StringKey) {Try{using (Rcclient =Getclient ()) {Return rcclient.get<t>(key); } }Catch{//If a Redis exception occurs, the default value is returned directlyReturnDefault(T); } }///<summary>///Remove Cache///</summary>///<param name= "Key" ></param>PublicStaticvoid Remove (StringKey) {using (Rcclient =Getclient ()) {rcclient.remove (key);}}///<summary>///Get Client///</summary>///<returns></returns>PrivateStaticIredisclient getclient () {Redisclientfactory factory =Redisclientfactory.instance; Redisclient client =Null;if (String. IsNullOrEmpty (Configmanager.redisserver)) {ThrowNew ArgumentNullException ( "redis server IP is empty." if (stringthrow new ArgumentNullException ( "redis server pwd is Empty. "); } client = factory. Createredisclient (Configmanager.redisserver, Configmanager.redisport); Client. Password = configmanager.redispwd, client. Db = Configmanager.redisserverdb; return client;}}       

The Assembly to use

Function description

You can cache the entity classes directly, set the expiration time, remove the cache, and get the cache functionality.

Use the Redisclientfactory factory to obtain a Redis client instance. If Redis has a password set, add the modifications in the configuration file

            Client = Factory. Createredisclient (Configmanager.redisserver, configmanager.redisport);            Client. Password = configmanager.redispwd;

Modify the Redis IP and port number, and then the password.

Usage Scenarios

In the specific use of the process, the use of redis time-out can be some persistent management of data, for some data consistency is not high data cache, so that the read speed, the use of Redis cluster can be a master-slave replication function, Redis cluster has no central node, and with replication and failover characteristics , which prevents a single node from becoming a performance bottleneck, or because a node is offline and the entire cluster is offline.

Reprint: Blog Address: http://www.cnblogs.com/wolf-sun/

[redis]c# Redis Cache Helper Class

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.