-
Notifications
You must be signed in to change notification settings - Fork 15
/
RedisUtil.cs
144 lines (143 loc) · 4.24 KB
/
RedisUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Redis;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace April.Util
{
public class RedisUtil
{
private static RedisCache _redisCache = null;
private static RedisCacheOptions options = null;
/// <summary>
/// 初始化Redis
/// </summary>
public static void InitRedis()
{
if (AprilConfig.IsOpenCache)
{
_redisCache = new RedisCache(GetOptions());
}
}
/// <summary>
/// 获取配置项信息
/// </summary>
/// <returns></returns>
protected static RedisCacheOptions GetOptions()
{
options = new RedisCacheOptions();
options.Configuration = AprilConfig.RedisConnectionString;
options.InstanceName = "April.Redis";
return options;
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="ExprireTime">过期时间</param>
public static void Add(string key, object value, int ExprireTime = 10)
{
if (string.IsNullOrEmpty(key))
{
return;
}
string strValue = string.Empty;
try
{
strValue = JsonConvert.SerializeObject(value);
}
catch (Exception ex)
{
LogUtil.Error($"Redis.Add转换失败:{ex.Message}");
}
if (!string.IsNullOrEmpty(strValue))
{
_redisCache.SetString(key, strValue, new DistributedCacheEntryOptions()
{
AbsoluteExpiration = DateTime.Now.AddMinutes(ExprireTime)
});
}
}
/// <summary>
/// 获取数据
/// </summary>
/// <param name="key">键</param>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
public static string Get(string key, string defaultValue = "")
{
if (string.IsNullOrEmpty(key))
{
return defaultValue;
}
string value = _redisCache.GetString(key);
if (string.IsNullOrEmpty(value))
{
value = defaultValue;
}
return value;
}
/// <summary>
/// 获取数据(对象)
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="key">键</param>
/// <returns></returns>
public static T Get<T>(string key)
{
string value = Get(key);
if (string.IsNullOrEmpty(value))
{
return default(T);
}
T obj = default(T);
try
{
obj = JsonConvert.DeserializeObject<T>(value);
}
catch (Exception ex)
{
LogUtil.Error($"Redis.Get转换失败:{ex.Message},数据:{value}");
}
return obj;
}
/// <summary>
/// 移除数据
/// </summary>
/// <param name="key">键</param>
public static void Remove(string key)
{
if (!string.IsNullOrEmpty(key))
{
_redisCache.Remove(key);
}
}
/// <summary>
/// 刷新数据
/// </summary>
/// <param name="key">键</param>
public static void Refresh(string key)
{
if (!string.IsNullOrEmpty(key))
{
_redisCache.Refresh(key);
}
}
/// <summary>
/// 重置数据
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="expireTime">过期时间</param>
public static void Replace(string key, object value, int expireTime = 10)
{
if (!string.IsNullOrEmpty(key))
{
Remove(key);
Add(key, value, expireTime);
}
}
}
}