对于安装Redis后 很是不明白如何建立Redis 和 .net 的链接配置 于是查找了很多的资料
首先第一步:安装ASP.NET NuGet 包 (ServiceStack.Redis) 安装好后 查看引用如下:
这时候 首先在 ASP.NET Web.Config中<appSettings>节点中配置如下
1
2
3
4
5
6
<!--Redis 配置-->
<add key="redis_server_write" value="Admin2018@127.0.0.1:6379" />
<add key="redis_server_read" value="Admin2018@127.0.0.1:6379" />
<add key="redis_max_read_pool" value="3" />
<add key="redis_max_write_pool" value="1" />
<!--Redis 配置-->
第二步:就开始配置链接Redis的链接了:
1>自定义创建一个RedisCacheHelper的配置类,代码如下:
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
public class RedisCacheHelper : ConfigurationSection
{
//读取Redis接口
private static string GetRedis = ConfigurationManager.AppSettings["redis_server_read"];
//读取数量
private static int GetRedisNum =Convert.ToInt32(ConfigurationManager.AppSettings["redis_max_read_pool"]);
//写入Redis接口
private static string SetRedis = ConfigurationManager.AppSettings["redis_server_write"];
//写入数量
private static int SetRedisNum = Convert.ToInt32(ConfigurationManager.AppSettings["redis_max_write_pool"]);
//定义连接池
private static readonly PooledRedisClientManager Pool = null;
//定义构造函数
static RedisCacheHelper()
{
string [] GetRedisHost = GetRedis.Split(',');
string [] SetRedisHost = SetRedis.Split(',');
if(GetRedisHost.Length>0&&SetRedisHost.Length>0)
{
Pool = new PooledRedisClientManager(GetRedisHost, SetRedisHost, new RedisClientManagerConfig()
{
MaxWritePoolSize = SetRedisNum,
MaxReadPoolSize = GetRedisNum,
AutoStart = true
});
}
}
/// <summary>
/// 添加缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
public static void Add<T>(string key,List<T> value)
{
if (value == null)
return;
try
{
if (Pool != null)
{
using (var r = Pool.GetClient())
{
if (r != null)
{
r.SendTimeout = 1000;
r.Set<List<T>>(key, value);
}
}
}
}
catch (Exception ex)
{
ErrorLog.WriteLog(ex);
}
}
/// <summary>
/// 查询单挑
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(string key)
{
if(key==null)
return default(T);
try
{
if (Pool != null)
{
using (var r = Pool.GetClient())
{
if (r != null)
{
r.SendTimeout = 1000;
return r.Get<T>(key);
}
}
}
}
catch (Exception ex)
{
ErrorLog.WriteLog(ex);
}
return default(T);
}
/// <summary>
/// 查询多条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static List<T> GetAll<T>(string key)
{
if (key == null)
return null;
try
{
if (Pool != null)
{
using (var r = Pool.GetClient())
{
if (r != null)
{
r.SendTimeout = 1000;
return r.Get<List<T>>(key);
}
}
}
}
catch (Exception ex)
{
ErrorLog.WriteLog(ex);
}
return null;
}
/// <summary>
/// 删除指定key缓存
/// </summary>
/// <param name="key"></param>
public static void Remove(string key)
{
if (key == null)
return;
try
{
if (Pool != null)
{
using (var r = Pool.GetClient())
{
if (r != null)
{
r.SendTimeout = 1000;
r.Remove(key);
}
}
}
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 判断缓存是否存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Exists(string key)
{
if (key == null)
return false;
try
{
if (Pool != null)
{
using (var r = Pool.GetClient())
{
if (r != null)
{
r.SendTimeout = 1000;
return r.ContainsKey(key);
}
}
}
}
catch (Exception ex)
{
ErrorLog.WriteLog(ex);
}
return false;
}
}
在调用的时候 我们还可以在自定义一个Key键的类 用于方便操作存储已经修改。