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