-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCacheHelper.cs
More file actions
154 lines (147 loc) · 5.38 KB
/
Copy pathCacheHelper.cs
File metadata and controls
154 lines (147 loc) · 5.38 KB
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
using Newtonsoft.Json;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Midgard.Common
{
/// <summary>
/// 缓存管理
/// </summary>
public class CacheManager
{
private volatile static CacheManager helper = null;
private static readonly object lockHelper = new object();
private CacheManager()
{
cache = new ConcurrentDictionary<string, ConcurrentDictionary<string, object>>();
}
/// <summary>
/// 取得单例
/// </summary>
/// <returns></returns>
public static CacheManager GetInstance()
{
if (helper == null)
{
lock (lockHelper)
{
if (helper == null)
helper = new CacheManager();
}
}
return helper;
}
private ConcurrentDictionary<string, ConcurrentDictionary<string, object>> cache = null;
/// <summary>
/// 获取区块下的缓存
/// </summary>
/// <param name="region"></param>
/// <returns></returns>
public ConcurrentDictionary<string, object> GetRegion(string region)
{
if (cache.ContainsKey(region))
return cache[region];
else
return new ConcurrentDictionary<string, object>();
}
/// <summary>
/// 新增缓存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="region"></param>
public object AddOrUpdate(string key, object value, string region)
{
var Region = GetRegion(region);
if (Region.Count > 0)
{
if (Region.ContainsKey(key))
{
//var json = JsonConvert.SerializeObject(cache, LogManager.Get().IsDebugEnabled ? Formatting.Indented : Formatting.None);
//LogManager.Get().ErrorFormat("cache333: {0}", json);
Region[key] = value;
return value;
}
else
{
var va = Region.AddOrUpdate(key, value, (k, v) => value);
var di = cache.AddOrUpdate(region, Region, (k, v) => Region);
//LogManager.Get().Error("va2:" + JsonConvert.SerializeObject(va));
//LogManager.Get().Error("di2:" + JsonConvert.SerializeObject(di));
//var json = JsonConvert.SerializeObject(cache, LogManager.Get().IsDebugEnabled ? Formatting.Indented : Formatting.None);
//LogManager.Get().ErrorFormat("cache222: {0}", json);
return Region[key];
}
}
else
{
var dict = new ConcurrentDictionary<string, object>();
var va = dict.AddOrUpdate(key, value, (k, v) => value);
var di = cache.AddOrUpdate(region, dict, (k, v) => dict);
//LogManager.Get().Error("va:" + JsonConvert.SerializeObject(va));
//LogManager.Get().Error("di:" + JsonConvert.SerializeObject(di));
//var json = JsonConvert.SerializeObject(cache, LogManager.Get().IsDebugEnabled ? Formatting.Indented : Formatting.None);
//LogManager.Get().ErrorFormat("cache111: {0}", json);
return value;
}
}
/// <summary>
/// 删除缓存 找不到就删除失败返回false
/// </summary>
/// <param name="key"></param>
/// <param name="region"></param>
/// <returns></returns>
public bool Remove(string key, string region)
{
object obj = null;
var Region = GetRegion(region);
if (Region.ContainsKey(key))
return Region.TryRemove(key, out obj);
else
return false;
}
/// <summary>
/// 获取某个缓存
/// </summary>
/// <param name="key"></param>
/// <param name="region"></param>
/// <returns></returns>
public object Get(string key, string region)
{
var Region = GetRegion(region);
if (Region.ContainsKey(key))
return Region[key];
else
return null;
}
/// <summary>
/// 通过区块前缀获取
/// </summary>
/// <param name="regionPrefix"></param>
/// <returns></returns>
public IEnumerable<ConcurrentDictionary<string, object>> GetRegionByPrefix(string regionPrefix)
{
List<ConcurrentDictionary<string, object>> result = new List<ConcurrentDictionary<string, object>>();
foreach (var key in cache.Keys)
{
if (key.StartsWith(regionPrefix))
{
result.Add(cache[key]);
}
}
return result;
}
/// <summary>
/// 清空区域
/// </summary>
/// <param name="region"></param>
/// <returns></returns>
public bool ClearRegion(string region)
{
ConcurrentDictionary<string, object> obj = null;
if (cache.ContainsKey(region))
return cache.TryRemove(region, out obj);
else
return false;
}
}
}