-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeckoboardCache.cs
More file actions
67 lines (55 loc) · 1.68 KB
/
GeckoboardCache.cs
File metadata and controls
67 lines (55 loc) · 1.68 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
using Countersoft.Foundation.Commons.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GeckoboardConnector
{
internal static class GeckoboardCache
{
class GeckoboardCacheItem
{
internal DateTime Created { get; set; }
internal object Data { get; set; }
}
private static Dictionary<string, GeckoboardCacheItem> _cache = new Dictionary<string, GeckoboardCacheItem>();
private static int _cacheDuration = 0;
private static int GetCacheDuration()
{
if(_cacheDuration != 0)
{
return _cacheDuration;
}
try
{
_cacheDuration = System.Configuration.ConfigurationManager.AppSettings["geckoboard.CacheDuration"].ToInt(300);
}
catch
{
_cacheDuration = 300;
}
return _cacheDuration;
}
public static object Get(string cacheKey)
{
GeckoboardCacheItem item;
if(_cache.TryGetValue(cacheKey, out item))
{
if((DateTime.UtcNow - item.Created).TotalSeconds < GetCacheDuration())
{
return item.Data;
}
}
return null;
}
public static void Set(string cacheKey, object data)
{
if(_cache.ContainsKey(cacheKey))
{
_cache.Remove(cacheKey);
}
_cache.Add(cacheKey, new GeckoboardCacheItem { Created = DateTime.UtcNow, Data = data });
}
}
}