C# Redis 缓存使用
2019-04-15 16:24
5972 人阅读

1. 先安装redis客户端,自己去网上下载安装,配置好链接账号,密码
2. vs使用nuget安装redis
Install-Package CachingFramework.Redis -Version 10.0.4
3. RedisHelper类
public class RedisHelper { /// <summary> /// redis连接字符串 /// </summary> public static readonly string RedisConnectionString = "RedisConnectionString".GetAppSetting<string>(); /// <summary> /// RedisCache上下文对象延迟 /// </summary> private static readonly Lazy<RedisContext> LazyContext = new Lazy<RedisContext>(() => new RedisContext(RedisConnectionString)); /// <summary> /// RedisCache上下文对象 /// </summary> private static RedisContext Context = LazyContext.Value; /// <summary> /// Redis连接状态 /// </summary> /// <returns>true:成功</returns> private static bool CanConnected() { try { return Context.GetConnectionMultiplexer().IsConnected; } catch (Exception ex) { LogHelper.Error("Redis 连接异常:" + ex.ToString()); } return false; } #region Object /// <summary> /// 设置redis /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expiry"></param> /// <returns></returns> public static bool Set(string key, object value, TimeSpan? expiry = null) { if (!CanConnected() || string.IsNullOrWhiteSpace(key)) return false; try { Context.Cache.SetObject(key, value, expiry); //value.GetType().IsValueType return true; } catch (Exception ex) { // 异常日志 LogHelper.Error("Redis Set异常:" + ex.ToString()); } return false; } /// <summary> /// 设置redis /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expiry"></param> /// <returns></returns> public static bool Set(string key, object value, string[] tags, TimeSpan? expiry = null) { if (!CanConnected() || string.IsNullOrWhiteSpace(key)) return false; try { Context.Cache.SetObject(key, value, tags, expiry); //value.GetType().IsValueType return true; } catch (Exception ex) { // 异常日志 LogHelper.Error("Redis Set异常:" + ex.ToString()); } return false; } /// <summary> /// 获取redis /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public static T Get<T>(string key) { bool flag; return Get<T>(key, out flag); } /// <summary> /// 获取redis /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="flag"></param> /// <returns></returns> public static T Get<T>(string key, out bool flag) { if (CanConnected() && !string.IsNullOrWhiteSpace(key)) { try { var result = Context.Cache.GetObject<T>(key); flag = null != result; return result; } catch (Exception ex) { // 异常日志 LogHelper.Error("Redis Get异常:" + ex.ToString()); } } flag = false; return default(T); } /// <summary> /// 获取redis /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="flag"></param> /// <returns></returns> public static IEnumerable<string> GetKeysByTag(string[] tags) { if (CanConnected()) { try { var result = Context.Cache.GetKeysByTag(tags); return result; } catch (Exception ex) { // 异常日志 LogHelper.Error("Redis Get异常:" + ex.ToString()); } } return default(IEnumerable<string>); } #endregion #region Check /// <summary> /// redis key是否存在 /// </summary> /// <param name="key"></param> /// <returns></returns> public static bool KeyExist(string key) { if (!CanConnected()) return false; try { return Context.Cache.KeyExists(key); } catch (Exception ex) { // 异常日志 LogHelper.Error("Redis KeyExist异常:" + ex.ToString()); } return false; } #endregion #region Remove /// <summary> /// 移除redis /// </summary> /// <param name="key"></param> /// <returns></returns> public static bool Remove(string key) { if (!CanConnected()) return false; try { return Context.Cache.Remove(key); } catch (Exception ex) { // 异常日志 LogHelper.Error("Redis Remove异常:" + ex.ToString()); } return false; } /// <summary> /// 移除redis /// </summary> /// <param name="keys"></param> public static void Remove(string[] keys) { if (!CanConnected()) return; if (null == keys || keys.Length == 0) return; try { Context.Cache.Remove(keys); } catch (Exception ex) { // 异常日志 LogHelper.Error("Redis Remove异常:" + ex.ToString()); } } /// <summary> /// 移除redis /// </summary> /// <param name="key"></param> /// <returns></returns> public static void RemoveTagsFromKey(string key, string[] tags) { if (!CanConnected()) return; try { Context.Cache.RemoveTagsFromKey(key, tags); } catch (Exception ex) { // 异常日志 LogHelper.Error("Redis Remove异常:" + ex.ToString()); } } #endregion }
4.添加缓存
[Serializable] public class UserInfo { /// <summary> /// 用户编号 /// </summary> public int UserId { get; set; } /// <summary> /// 用户名 /// </summary> public string UserName { get; set; } } var model = new UserInfo(); model.UserId = 100; model.UserName = "mike"; //设置缓存20分钟 RedisHelper.Set("UserInfo_" + model.UserId, JsonConvert.SerializeObject(model),new TimeSpan(0, 20, 0));
5.获取缓存内容
string userInfoCache = RedisHelper.Get<string>("UserInfo_100"); if (!string.IsNullOrEmpty(userInfoCache)) { var UserTokenModel = JsonConvert.DeserializeObject<UserInfo>(userInfoCache); }
和博主交个朋友吧
