电商系统架构总结2(Redis)
二 Redis缓存
考虑到将来服务器的升级扩展,使用redis代替.net内置缓存是比较理想的选择。redis是非常成熟好用的缓存系统,安装配置非常简单,直接上官网下载安装包 安装启动就行了。
1 配置。redis安装后默认bind 接口是127.0.0.1,也就是本地回环地址。在开发环境下为了允许多个开发机器作为客户端访问,bind配置后面加上了本机局域网ip,如 bind 127.0.0.1 192.168.1.100 。
2 配置了redis读写分离。开发web api端的web.config件 增加配置项
<add name="RedisConnection_read" connectionString="192.168.0.100:6379" />
<add name="RedisConnection_write" connectionString="192.168.0.100:6379" />
redis 读写辅助类代码如下
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | public class RedisCache: IRedisCache { private static string [] ReadWriteHosts = ConfigurationManager.ConnectionStrings[ "RedisConnection_read" ].ConnectionString.Split( ';' ); private static string [] ReadOnlyHosts = ConfigurationManager.ConnectionStrings[ "RedisConnection_write" ].ConnectionString.Split( ';' ); #region -- 连接信息 -- public static PooledRedisClientManager prcm = CreateManager(ReadWriteHosts, ReadOnlyHosts); private static PooledRedisClientManager CreateManager( string [] readWriteHosts, string [] readOnlyHosts) { // 支持读写分离,均衡负载 return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig { MaxWritePoolSize = 5, // “写”链接池链接数 MaxReadPoolSize = 5, // “读”链接池链接数 AutoStart = true , }); } #endregion #region -- Item -- /// <summary> /// 设置单体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="t"></param> /// <param name="timeSpan"></param> /// <returns></returns> public bool Item_Set<T>( string key, T t) { try { using (IRedisClient redis = prcm.GetClient()) { return redis.Set<T>(key, t, new TimeSpan(1, 0, 0)); } } catch (Exception ex) { throw ex; } } /// <summary> /// 设置单体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="t"></param> /// <param name="timeSpan"></param> /// <returns></returns> public bool Item_Set<T>( string key, T t, int timeout) { try { using (IRedisClient redis = prcm.GetClient()) { return redis.Set<T>(key, t, new TimeSpan(0, timeout, 0)); } } catch (Exception ex) { throw ex; } } /// <summary> /// 获取单体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public T Item_Get<T>( string key) where T : class { using (IRedisClient redis = prcm.GetReadOnlyClient()) { return redis.Get<T>(key); } } /// <summary> /// 设置缓存过期 /// </summary> /// <param name="key"></param> /// <param name="datetime"></param> public bool Item_Remove( string key) { using (IRedisClient redis = prcm.GetClient()) { return redis.Remove(key); } } /// <summary> /// 设置缓存过期 /// </summary> /// <param name="key"></param> public bool Item_SetExpire( string key, int timeout) { using (IRedisClient redis = prcm.GetClient()) { return redis.ExpireEntryIn(key, new TimeSpan(0, timeout, 0)); } } #endregion #region -- List -- public void List_Add<T>( string key, T t) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.As<T>(); redisTypedClient.AddItemToList(redisTypedClient.Lists[key], t); } } public void List_Set<T>( string key, List<T> list) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.As<T>(); redisTypedClient.Lists[key].RemoveAll(); if (list != null ) { list.ForEach(p => { redisTypedClient.AddItemToList(redisTypedClient.Lists[key], p); }); } } } public bool List_Remove<T>( string key, T t) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.As<T>(); return redisTypedClient.RemoveItemFromList(redisTypedClient.Lists[key], t) > 0; } } public void List_RemoveAll<T>( string key) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.As<T>(); redisTypedClient.Lists[key].RemoveAll(); } } public void List_RemoveRange<T>( string key,List<T> list) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.As<T>(); if (list != null && list.Count > 0) { for ( int i=0; i<list.Count;i++) { List_Remove<T>(key, list[i]); } } } } public long List_Count( string key) { using (IRedisClient redis = prcm.GetReadOnlyClient()) { return redis.GetListCount(key); } } public List<T> List_GetRange<T>( string key, int start, int count) { using (IRedisClient redis = prcm.GetReadOnlyClient()) { var c = redis.As<T>(); return c.Lists[key].GetRange(start, start + count - 1); } } public List<T> List_GetList<T>( string key) { RedisClient client = new RedisClient( "" ); using (IRedisClient redis = prcm.GetReadOnlyClient()) { var c = redis.As<T>(); return c.Lists[key].GetRange(0, c.Lists[key].Count); } } public List<T> List_GetList<T>( string key, int pageIndex, int pageSize) { int start = pageSize * (pageIndex - 1); return List_GetRange<T>(key, start, pageSize); } /// <summary> /// 设置缓存过期 /// </summary> /// <param name="key"></param> /// <param name="datetime"></param> public void List_SetExpire( string key, DateTime datetime) { using (IRedisClient redis = prcm.GetClient()) { redis.ExpireEntryAt(key, datetime); } } #endregion #region -- Set -- public void Set_Add<T>( string key, T t) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.As<T>(); redisTypedClient.Sets[key].Add(t); } } public bool Set_Contains<T>( string key, T t) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.As<T>(); return redisTypedClient.Sets[key].Contains(t); } } public bool Set_Remove<T>( string key, T t) { using (IRedisClient redis = prcm.GetClient()) { var redisTypedClient = redis.As<T>(); return redisTypedClient.Sets[key].Remove(t); } } #endregion #region -- Hash -- /// <summary> /// 判断某个数据是否已经被缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataKey"></param> /// <returns></returns> public bool Hash_Exist<T>( string key, string dataKey) { using (IRedisClient redis = prcm.GetReadOnlyClient()) { return redis.HashContainsEntry(key, dataKey); } } /// <summary> /// 存储数据到hash表 /// </summary> /// <typeparam name="T"></typeparam> Class1.cs /// <param name="key"></param> /// <param name="dataKey"></param> /// <returns></returns> public bool Hash_Set<T>( string key, string dataKey, T t) { using (IRedisClient redis = prcm.GetClient()) { string value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t); return redis.SetEntryInHash(key, dataKey, value); } } /// <summary> /// 移除hash中的某值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataKey"></param> /// <returns></returns> public bool Hash_Remove( string key, string dataKey) { using (IRedisClient redis = prcm.GetClient()) { return redis.RemoveEntryFromHash(key, dataKey); } } /// <summary> /// 移除整个hash /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataKey"></param> /// <returns></returns> public bool Hash_Remove( string key) { using (IRedisClient redis = prcm.GetClient()) { return redis.Remove(key); } } /// <summary> /// 从hash表获取数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="dataKey"></param> /// <returns></returns> public T Hash_Get<T>( string key, string dataKey) { using (IRedisClient redis = prcm.GetReadOnlyClient()) { string value = redis.GetValueFromHash(key, dataKey); return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(value); } } /// <summary> /// 获取整个hash的数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <returns></returns> public List<T> Hash_GetAll<T>( string key) { using (IRedisClient redis = prcm.GetReadOnlyClient()) { var list = redis.GetHashValues(key); if (list != null && list.Count > 0) { List<T> result = new List<T>(); foreach ( var item in list) { var value = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item); result.Add(value); } return result; } return null ; } } /// <summary> /// 设置缓存过期 /// </summary> /// <param name="key"></param> /// <param name="datetime"></param> public void Hash_SetExpire( string key, DateTime datetime) { using (IRedisClient redis = prcm.GetClient()) { redis.ExpireEntryAt(key, datetime); } } /// <summary> /// 获取Hash集合数量 /// </summary> /// <param name="key">Hashid</param> public long Hash_GetCount( string key) { using (IRedisClient redis = prcm.GetReadOnlyClient()) { return redis.GetHashCount(key); } } #endregion #region -- SortedSet -- /// <summary> /// 添加数据到 SortedSet /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="t"></param> /// <param name="score"></param> public bool SortedSet_Add<T>( string key, T t, double score) { using (IRedisClient redis = prcm.GetClient()) { string value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t); return redis.AddItemToSortedSet(key, value, score); } } /// <summary> /// 移除数据从SortedSet /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="t"></param> /// <returns></returns> public bool SortedSet_Remove<T>( string key, T t) { using (IRedisClient redis = prcm.GetClient()) { string value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t); return redis.RemoveItemFromSortedSet(key, value); } } /// <summary> /// 修剪SortedSet /// </summary> /// <param name="key"></param> /// <param name="size">保留的条数</param> /// <returns></returns> public long SortedSet_Trim( string key, int size) { using (IRedisClient redis = prcm.GetClient()) { return redis.RemoveRangeFromSortedSet(key, size, 9999999); } } /// <summary> /// 获取SortedSet的长度 /// </summary> /// <param name="key"></param> /// <returns></returns> public long SortedSet_Count( string key) { using (IRedisClient redis = prcm.GetReadOnlyClient()) { return redis.GetSortedSetCount(key); } } /// <summary> /// 获取SortedSet的分页数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public List<T> SortedSet_GetList<T>( string key, int pageIndex, int pageSize) { using (IRedisClient redis = prcm.GetReadOnlyClient()) { var list = redis.GetRangeFromSortedSet(key, (pageIndex - 1) * pageSize, pageIndex * pageSize - 1); if (list != null && list.Count > 0) { List<T> result = new List<T>(); foreach ( var item in list) { var data = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item); result.Add(data); } return result; } } return null ; } /// <summary> /// 获取SortedSet的全部数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public List<T> SortedSet_GetListALL<T>( string key) { using (IRedisClient redis = prcm.GetReadOnlyClient()) { var list = redis.GetRangeFromSortedSet(key, 0, 9999999); if (list != null && list.Count > 0) { List<T> result = new List<T>(); foreach ( var item in list) { var data = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item); result.Add(data); } return result; } } return null ; } /// <summary> /// 设置缓存过期 /// </summary> /// <param name="key"></param> /// <param name="datetime"></param> public void SortedSet_SetExpire( string key, DateTime datetime) { using (IRedisClient redis = prcm.GetClient()) { redis.ExpireEntryAt(key, datetime); } } #endregion } |
3 用 redis实现 分布式 跨应用session共享,同样,web api 的webconfig文件里session配置为
<system.web> <sessionState mode="Custom" customProvider="RedisSessionStateStore"> <providers> <add name="RedisSessionStateStore" type="MyProject.RedisSessionStateStore" /> </providers> </sessionState> ... <system.web>
RedisSessionStateStore实现代码如下:
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | public class RedisSessionStateStore : SessionStateStoreProviderBase { IRedisCache redis; public RedisSessionStateStore() { redis = IDAL.DALContainer.Resolve<IRedisCache>(); } public override SessionStateStoreData CreateNewStoreData(HttpContext context, int timeout) { return CreateLegitStoreData(context, null , null , timeout); } internal static SessionStateStoreData CreateLegitStoreData(HttpContext context, ISessionStateItemCollection sessionItems, HttpStaticObjectsCollection staticObjects, int timeout) { if (sessionItems == null ) sessionItems = new SessionStateItemCollection(); if (staticObjects == null && context != null ) staticObjects = SessionStateUtility.GetSessionStaticObjects(context); return new SessionStateStoreData(sessionItems, staticObjects, timeout); } public override void CreateUninitializedItem(HttpContext context, string id, int timeout) { RedisSessionState state = new RedisSessionState( null , null , timeout); redis.Item_Set< string >(id, state.ToJson(), timeout); } private SessionStateStoreData DoGet(HttpContext context, string id, bool exclusive, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actionFlags) { locked = false ; lockId = null ; lockAge = TimeSpan.Zero; actionFlags = SessionStateActions.None; RedisSessionState state = RedisSessionState.FromJson(redis.Item_Get< string >(id)); if (state == null ) { return null ; } redis.Item_SetExpire(id, state._timeout); return CreateLegitStoreData(context, state._sessionItems, state._staticObjects, state._timeout); } public override SessionStateStoreData GetItem(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actionFlags) { return this .DoGet(context, id, false , out locked, out lockAge, out lockId, out actionFlags); } public override SessionStateStoreData GetItemExclusive(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actionFlags) { return this .DoGet(context, id, true , out locked, out lockAge, out lockId, out actionFlags); } public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) { ISessionStateItemCollection sessionItems = null ; HttpStaticObjectsCollection staticObjects = null ; if (item.Items.Count > 0) sessionItems = item.Items; if (!item.StaticObjects.NeverAccessed) staticObjects = item.StaticObjects; RedisSessionState state2 = new RedisSessionState(sessionItems, staticObjects, item.Timeout); redis.Item_Set< string >(id, state2.ToJson(), item.Timeout); } #region "未实现方法" public override void Dispose() { } public override void EndRequest(HttpContext context) { } public override void InitializeRequest(HttpContext context) { } public override void ReleaseItemExclusive(HttpContext context, string id, object lockId) { } public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item) { redis.Item_Remove(id); } public override void ResetItemTimeout(HttpContext context, string id) { } public override bool SetItemExpireCallback(SessionStateItemExpireCallback expireCallback) { return true ; } #endregion } internal sealed class SessionStateItem { public Dictionary< string , SaveValue> Dict; public int Timeout; } internal sealed class SaveValue { public object Value { get ; set ; } public Type Type { get ; set ; } } internal sealed class RedisSessionState { internal ISessionStateItemCollection _sessionItems; internal HttpStaticObjectsCollection _staticObjects; internal int _timeout; internal RedisSessionState(ISessionStateItemCollection sessionItems, HttpStaticObjectsCollection staticObjects, int timeout) { this .Copy(sessionItems, staticObjects, timeout); } internal void Copy(ISessionStateItemCollection sessionItems, HttpStaticObjectsCollection staticObjects, int timeout) { this ._sessionItems = sessionItems; this ._staticObjects = staticObjects; this ._timeout = timeout; } public string ToJson() { // 这里忽略_staticObjects这个成员。 if (_sessionItems == null || _sessionItems.Count == 0) { return null ; } Dictionary< string , SaveValue> dict = new Dictionary< string , SaveValue>(_sessionItems.Count); NameObjectCollectionBase.KeysCollection keys = _sessionItems.Keys; string key; object objectValue = string .Empty; Type type = null ; //2016-09-04解决存入值没有类型导致读取值是jobject问题 for ( int i = 0; i < keys.Count; i++) { key = keys[i]; objectValue = _sessionItems[key]; if (objectValue != null ) { type = objectValue.GetType(); } else { type = typeof ( object ); } dict.Add(key, new SaveValue { Value = objectValue, Type = type }); } SessionStateItem item = new SessionStateItem { Dict = dict, Timeout = this ._timeout }; return JsonConvert.SerializeObject(item); } public static RedisSessionState FromJson( string json) { if ( string .IsNullOrEmpty(json)) { return null ; } try { SessionStateItem item = JsonConvert.DeserializeObject<SessionStateItem>(json); SessionStateItemCollection collections = new SessionStateItemCollection(); SaveValue objectValue = null ; //2016-09-04解决读取出来的值 没有类型的问题 JsonSerializer serializer = new JsonSerializer(); StringReader sr = null ; JsonTextReader tReader = null ; foreach (KeyValuePair< string , SaveValue> kvp in item.Dict) { objectValue = kvp.Value as SaveValue; if (objectValue.Value == null ) { collections[kvp.Key] = null ; } else { if (!IsValueType(objectValue.Type)) { sr = new StringReader(objectValue.Value.ToString()); tReader = new JsonTextReader(sr); collections[kvp.Key] = serializer.Deserialize(tReader, objectValue.Type); } else { collections[kvp.Key] = objectValue.Value; } } } return new RedisSessionState(collections, null , item.Timeout); } catch { return null ; } } /// <summary> /// 判断是否为值类型 /// </summary> /// <param name="type">Type</param> /// <returns></returns> public static bool IsValueType(Type type) { if (type.IsValueType) { return true ; } //基础数据类型 if (type == typeof ( string ) || type == typeof ( char ) || type == typeof ( ushort ) || type == typeof ( short ) || type == typeof ( uint ) || type == typeof ( int ) || type == typeof ( ulong ) || type == typeof ( long ) || type == typeof ( double ) || type == typeof ( decimal ) || type == typeof ( bool ) || type == typeof ( byte )) { return true ; } //可为null的基础数据类型 if (type.IsGenericType && !type.IsGenericTypeDefinition) { Type genericType = type.GetGenericTypeDefinition(); if (Object.ReferenceEquals(genericType, typeof (Nullable<>))) { var actualType = type.GetGenericArguments()[0]; return IsValueType(actualType); } } return false ; } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述