Redis cache public method class, redis Cache

Source: Internet
Author: User
Tags delete cache

Redis cache public method class, redis Cache

Redis cache public methods

Config configuration file:

<! -- Redis settings --> <! -- Whether to enable cache 1 is 0 No --> <add key = "IsOpenCache" value = "1"/> <! -- Redis IP Address --> <add key = "RedisIP" value = "192.168.1.1"/> <! -- Redis port (default 6379 is not set) --> <add key = "RedisPort" value = "6379"/> <! -- Redis password (no password) --> <add key = "RedisPassword" value = "@ 123456"/> <! -- Redis database quotation marks (integer, which has five databases by default, starting from 0, indicating 0) --> <add key = "RedisDb" value = "2"/> <! -- End of related Redis settings --> <add key = "ClientSettingsProvider. ServiceUri" value = ""/>View Code

 

RedisHelper:

Using System; using System. collections. generic; using System. text; using ServiceStack. redis; public class RedisHelper {private static object lockobj = new object (); /// <summary> /// add cache /// </summary> /// <typeparam name = "T"> Object Type </typeparam> /// <param name = "cacheKey"> Key (Key value naming rules: RK _ field name_table name_condition Field 1 _ value _ condition field n _ value ......, all key values are in lowercase, and the table name does not contain the dbo prefix) </param> /// <param name = "obj"> Object </param> /// <param name = "expiresAt"> Cache Time (Except that the expiration time of XianZhiAccounts is one hour, the remaining expiration time is two days) </param> // <returns> whether the cache is successful </returns> public static bool Set <T> (string cacheKey, T obj, DateTime expiresAt) {bool result = false; lock (lockobj) {try {if (IsOpenCache) {if (! (Obj is System. Data. DataTable) |! (Obj is System. Data. DataSet) |! (Obj is System. data. dataRow) {using (IRedisClient Redis = prcm. getClient () {result = Redis. set <T> (cacheKey, obj, expiresAt) ;}}} catch {}} return result ;}/// <summary >/// add to the cache (permanent cache) /// </summary> /// <typeparam name = "T"> Object Type </typeparam> /// <param name = "cacheKey"> Key (Key Value name specifications: RK _ field name_table name_condition Field 1 _ value _ condition field n _ value ......, all key values are in lowercase.) </param> /// <param name = "obj"> Object </param> /// <returns> indicates whether the cache is successful </returns> p Rivate static bool Set <T> (string cacheKey, T obj) {lock (lockobj) {try {if (! IsOpenCache) return false; return Set <T> (cacheKey, obj, DateTime. now. addYears (100);} catch {return false ;}}} /// <summary> /// obtain the cached content /// </summary> /// <param name = "cacheKey"> Key </param> /// <returns> </returns> public static T Get <T> (string cacheKey) {lock (lockobj) {try {if (! IsOpenCache) return default (T); using (IRedisClient Redis = prcm. getClient () {return Redis. get <T> (cacheKey) ;}} catch {string str = string. empty; return default (T );}}} /// <summary> /// Delete the cache /// </summary> /// <param name = "cacheKey"> Key </param> /// <returns> </returns> public static bool Del (string cacheKey) {try {if (! IsOpenCache) return false; using (IRedisClient Redis = prcm. getClient () {return Redis. remove (cacheKey) ;}} catch {return false ;}} /// <summary> /// batch Delete cache /// </summary> /// <param name = "cacheKeys"> Key </param> /// <returns> </returns> public static void Del (List <string> cacheKeys) {try {if (! IsOpenCache) return; using (IRedisClient Redis = prcm. getClient () {Redis. removeAll (cacheKeys) ;}} catch {return ;}/// <summary> /// clear the cache /// </summary> public static void revoke AchE () {try {if (! IsOpenCache) return; using (IRedisClient Redis = prcm. getClient () {Redis. flushAll () ;}} catch {return ;}# region private static string RedisIP = ConfigHelper. getdeleettingvalue ("RedisIP"); private static string RedisPassword = ConfigHelper. getdeleettingvalue ("RedisPassword"); private static int RedisPort = _ 51Sole. DJG. common. dataConvert. toInt (ConfigHelper. getdeleettingvalue ("RedisIP"), 6 379); private static int DbIndex = _ 51Sole. DJG. common. dataConvert. toInt (ConfigHelper. getdeleettingvalue ("RedisDb"), 2); private static PooledRedisClientManager prcm = CreateManager (new string [] {"" + RedisPassword + "@" + RedisIP + ": "+ RedisPort +" "}, new string [] {" "+ RedisPassword +" @ "+ RedisIP +": "+ RedisPort + ""}); private static PooledRedisClientManager CreateManager (string [] ReadWriteHosts, string [] readOnlyHosts) {// read/write splitting is supported, and the load balancing RedisClientManagerConfig clientConfig = new RedisClientManagerConfig (); clientConfig. maxWritePoolSize = 10000; clientConfig. maxReadPoolSize = 10000; clientConfig. autoStart = true; clientConfig. defaultDb = DbIndex; PooledRedisClientManager clientManager = new PooledRedisClientManager (readWriteHosts, readOnlyHosts, clientConfig); return client Manager;} /// <summary> /// whether to use the cache switch /// </summary> private static bool IsOpenCache {get {if (ConfigHelper. getdeleettingvalue ("IsOpenCache ")! = "1") return false; return true ;}# endregion}View Code

 

ConfigHelper:

/// <Summary> // Redis configuration file help class // </summary> public class ConfigHelper {# region configuration file reads public static string getdeleettingvalue (string key) {try {return System. configuration. configurationManager. deleetask[ key];} catch {return string. empty ;}# endregion}View Code

 

TypeConverter:

Using System; using System. globalization; public class TypeConverter {// <summary> // convert to int type /// </summary> /// <param name = "obj"> </param>/ // <param name = "defaultValue"> default value returned </param> // <param name = "numStyle"> digital format </param> /// <returns> </returns> public static int ConvertToInt (object obj, numberStyles numStyle) {int result = 0; if (obj! = Null & obj! = DBNull. Value) {if (! Int. tryParse (obj. toString (). trim (), numStyle, null, out result) {result = 0 ;}} return result ;} /// <summary> /// convert to int type /// </summary> /// <param name = "obj"> </param> /// <param name = "defaultValue"> default value returned </param> // <returns> </returns> public static int ConvertToInt (object obj) {return ConvertToInt (obj, NumberStyles. number );}}View Code

 

Add cache method:

String keyWord = "TestRedis"; string cacheKey_keyWord = "RK_RedisCache_KeyWord _" + keyWord; string cacheKey_Data = "RK_RedisCache_Data _" + keyWord; RedisHelper. set <string> (cacheKey_keyWord, "Put cache value here", DateTime. now. addDays (5); // Validity PeriodView Code

 

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.