標籤:cep ges 資料 ima lex password 設定 tle name
using NServiceKit.Redis;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace H5.Utility{ /// <summary> /// 分布式緩衝 /// </summary> public static class RedisCache { private static IRedisClient RCClient = null; /// <summary> /// 設定redis緩衝 /// </summary> /// <typeparam name="T">泛型類</typeparam> /// <param name="key">緩衝鍵</param> /// <param name="value">泛型實體</param> /// <param name="expire">到期時間</param> /// <returns></returns> public static bool Set<T>(string key, T value, DateTime expire) { try { using (RCClient = GetClient()) { return RCClient.Set<T>(key, value, expire); } } catch { return false; } } /// <summary> /// 擷取緩衝 /// </summary> /// <typeparam name="T">實體</typeparam> /// <param name="key">索引值</param> /// <returns></returns> public static T Get<T>(string key) { try { using (RCClient = GetClient()) { return RCClient.Get<T>(key); } } catch { //如果redis出現異常,則直接返回預設值 return default(T); } } /// <summary> /// 移除緩衝 /// </summary> /// <param name="key"></param> public static void Remove(string key) { using (RCClient = GetClient()) { RCClient.Remove(key); } } /// <summary> /// 擷取用戶端 /// </summary> /// <returns></returns> private static IRedisClient GetClient() { RedisClientFactory factory = RedisClientFactory.Instance; RedisClient client = null; if (string.IsNullOrEmpty(ConfigManager.RedisServer)) { throw new ArgumentNullException("redis server ip is empty."); } if (string.IsNullOrEmpty(ConfigManager.RedisPwd)) { throw new ArgumentNullException("redis server pwd is empty."); } client = factory.CreateRedisClient(ConfigManager.RedisServer, ConfigManager.RedisPort); client.Password = ConfigManager.RedisPwd; client.Db = ConfigManager.RedisServerDb; return client; } }}
用到的程式集
功能描述
可以直接緩衝實體類,設定到期時間,移除緩衝,擷取緩衝功能。
使用RedisClientFactory工廠擷取redis用戶端執行個體。如果Redis設定了密碼,在設定檔中添加修改
client = factory.CreateRedisClient(ConfigManager.RedisServer, ConfigManager.RedisPort); client.Password = ConfigManager.RedisPwd;
修改redis的ip和連接埠號碼,密碼即可。
使用情境
具體的使用過程中,使用redis的逾時可以對資料進行一些持久化管理,對於一些資料一致性不高的資料進行緩衝,使得讀取速度提高,使用redis叢集時可以是用主從複製功能,Redis叢集沒有中心節點,並且帶有複製和容錯移轉特性,這可以避免單個節點成為效能瓶頸,或者因為某個節點下線而導致整個叢集下線。
轉載:部落格地址:http://www.cnblogs.com/wolf-sun/
[Redis]c# redis緩衝輔助類