Repository navigation
Expand file tree
/
Copy pathModelCache.cs
More file actions
128 lines (107 loc) · 4.28 KB
/
Copy pathModelCache.cs
File metadata and controls
128 lines (107 loc) · 4.28 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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
// TODO: replace or extend with Redis
// TODO: debug potential exceptions and remove try catch to improve performance (if it helps)
namespace App.DL.Module.Cache {
public static class ModelCache {
private const int CacheSize = 100;
private static Dictionary<string, Dictionary<int, object>> _intCache = new Dictionary<string, Dictionary<int, object>>();
private static Dictionary<string, Dictionary<string, object>> _strCache = new Dictionary<string, Dictionary<string, object>>();
public static void Store(string cacheName, string guid, object newObject) {
try {
if (!_strCache.ContainsKey(cacheName)) {
_strCache.Add(cacheName, new Dictionary<string, object>());
}
if (_strCache[cacheName].ContainsKey(guid)) {
return;
}
_strCache[cacheName][guid] = newObject;
CleanUp(cacheName);
}
catch (Exception e) {
//
}
}
public static void Store(string cacheName, int id, object newObject) {
try {
if (!_intCache.ContainsKey(cacheName)) {
_intCache.Add(cacheName, new Dictionary<int, object>());
}
if (_intCache[cacheName].ContainsKey(id)) {
return;
}
_intCache[cacheName].Add(id, newObject);
CleanUp(cacheName);
}
catch (Exception e) {
//
}
}
public static object Get(string cacheName, int id) {
try {
if (!_intCache.ContainsKey(cacheName)) {
_intCache.Add(cacheName, new Dictionary<int, object>());
}
if (!_intCache[cacheName].ContainsKey(id)) {
return null;
}
return _intCache[cacheName][id];
}
catch (Exception e) {
return null;
}
}
public static object Get(string cacheName, string guid) {
try {
if (!_strCache.ContainsKey(cacheName)) {
_strCache.Add(cacheName, new Dictionary<string, object>());
}
if (!_strCache[cacheName].ContainsKey(guid)) {
return null;
}
return _strCache[cacheName][guid];
}
catch (Exception e) {
return null;
}
}
public static void CleanUp(string cacheName) {
if (_intCache.ContainsKey(cacheName) && _intCache[cacheName].Count > CacheSize) {
foreach (int key in _intCache[cacheName].Keys) {
_intCache[cacheName].Remove(key);
return;
}
}
if (_strCache.ContainsKey(cacheName) && _strCache[cacheName].Count > CacheSize) {
foreach (string key in _strCache[cacheName].Keys) {
_strCache[cacheName].Remove(key);
return;
}
}
}
public static void CleanUp(string cacheName, int id) {
if (_intCache.ContainsKey(cacheName) && _intCache[cacheName].ContainsKey(id)) {
_intCache[cacheName].Remove(id);
}
}
public static void CleanUp(string cacheName, string guid) {
if (_strCache.ContainsKey(cacheName) && _strCache[cacheName].ContainsKey(guid)) {
_strCache[cacheName].Remove(guid);
}
}
public static void Reset() {
_intCache = new Dictionary<string, Dictionary<int, object>>();
_strCache = new Dictionary<string, Dictionary<string, object>>();
}
public static void StartAutoResetTask() {
Task.Run(async () => {
const int delayMs = 600000; // 10 minutes
while (true) {
Console.WriteLine("Cache cleaned up at: UTC " + DateTime.UtcNow);
await Task.Delay(delayMs);
}
});
}
}
}