-
Notifications
You must be signed in to change notification settings - Fork 1
/
HelperCache.cs
133 lines (122 loc) · 3.77 KB
/
HelperCache.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Vsr.Hawaii.EmotionmlLib
{
//TODO: remove public
public class HelperCache
{
/// <summary>
/// default cachetime in seconds: 1 day (24 * 60 * 60)
/// </summary>
public const long DEFAULT_CACHETIME = 86400;
/// <summary>
/// data of cache: key => data
/// </summary>
protected SortedList<string, object> cacheData = new SortedList<string, object>();
/// <summary>
/// cache livetime: key => cache invalidate after this time
/// nothing exists = no lifetime end
/// </summary>
protected SortedList<string, DateTime> cacheLifetime = new SortedList<string, DateTime>();
/// <summary>
/// is cache is in time
/// </summary>
/// <param name="key">cache key</param>
/// <returns>cache is valid</returns>
protected bool isValid(string key)
{
if (!cacheLifetime.Keys.Contains(key))
{
return true; //cache has no end of life
}
else if(DateTime.Now < cacheLifetime[key])
{
return true;
}
return false;
}
/// <summary>
/// stores a object in cache
/// </summary>
/// <param name="key">key for item, to access it later</param>
/// <param name="cacheObject">the object to cache</param>
/// <param name="lifetime">lifetime of cache in seconds; null = endless life</param>
public void storeItem(string key, object cacheObject, long? lifetime = DEFAULT_CACHETIME)
{
//store data
if (cacheData.Keys.Contains(key))
{
cacheData[key] = cacheObject;
}
else
{
cacheData.Add(key, cacheObject);
}
//store lifetime
if (lifetime != null)
{
DateTime endTime = DateTime.Now.AddSeconds((double)lifetime);
if (cacheLifetime.Keys.Contains(key))
{
cacheLifetime[key] = endTime;
}
else
{
cacheLifetime.Add(key, endTime);
}
}
}
/// <summary>
/// reads an entry from the cache
/// </summary>
/// <param name="key">key for item</param>
/// <returns>object stored in cache or null, if cache lifetime reached or object is not in cache</returns>
public object getItem(string key)
{
if(cacheData.Keys.Contains(key)) {
if (isValid(key))
{
return cacheData[key];
}
else
{
flushCache(key);
return null;
}
} else {
return null;
}
}
/// <summary>
/// flush cache of item
/// </summary>
/// <param name="key">key of item in cache</param>
public void flushCache(string key)
{
if (cacheData.Keys.Contains(key))
{
cacheData.Remove(key);
}
if (cacheLifetime.Keys.Contains(key))
{
cacheLifetime.Remove(key);
}
}
/// <summary>
/// flush whole cache
/// </summary>
public void flushCache()
{
foreach (string key in cacheData.Keys)
{
cacheData.Remove(key);
}
foreach (string key in cacheLifetime.Keys)
{
cacheLifetime.Remove(key);
}
}
}
}