-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemoriser.cs
executable file
·164 lines (151 loc) · 4.82 KB
/
Memoriser.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using Pfs;
namespace pfs
{
public class Memoriser
{
private readonly TimeSpan _maxCacheAge;
private readonly IDictionary<long, CacheItem> _resultCache;
public Memoriser(Configuration config)
{
_maxCacheAge = TimeSpan.FromMilliseconds(config.CacheAge);
_resultCache = new Dictionary<long, CacheItem>();
}
private static long GenerateHashCode(params object[] input)
{
return input.Aggregate<object, long>(27, (current, i) => 13 * current + i.GetHashCode());
}
private T GetCachedResult<T>(long key)
{
if (!this._resultCache.ContainsKey(key))
{
return default(T);
}
var cached = _resultCache[key];
if (DateTimeOffset.Now - cached.AddedAt >= _maxCacheAge)
{
_resultCache.Remove(key);
return default(T);
}
if (cached.Value is Exception exception)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}
return (T) cached.Value;
}
public Q Memorise<T, Q>(Func<T, Q> callback, T arg)
{
var hash = GenerateHashCode(callback, arg);
var cacheResult = GetCachedResult<Q>(hash);
if (!object.Equals(cacheResult, default(Q)))
{
return cacheResult;
}
try
{
var result = callback(arg);
_resultCache[hash] = new CacheItem(result);
return result;
}
catch (Exception e)
{
_resultCache[hash] = new CacheItem(e);
throw;
}
}
public async Task<Q> Memorise<T, Q>(Func<T, Task<Q>> callback, T arg)
{
var hash = GenerateHashCode(callback, arg);
var cacheResult = GetCachedResult<Q>(hash);
if (!object.Equals(cacheResult, default(Q)))
{
return cacheResult;
}
try
{
var result = await callback(arg);
_resultCache[hash] = new CacheItem(result);
return result;
}
catch (Exception e)
{
_resultCache[hash] = new CacheItem(e);
throw;
}
}
public Q Memorise<P, T, Q>(Func<P, T, Q> callback, P arg1, T arg2)
{
var hash = GenerateHashCode(callback, arg1, arg2);
var cacheResult = GetCachedResult<Q>(hash);
if (!object.Equals(cacheResult, default(Q)))
{
return cacheResult;
}
try
{
var result = callback(arg1, arg2);
_resultCache[hash] = new CacheItem(result);
return result;
}
catch (Exception e)
{
_resultCache[hash] = new CacheItem(e);
throw;
}
}
public async Task<Q> Memorise<P, T, Q>(Func<P, T, Task<Q>> callback, P arg1, T arg2)
{
var hash = GenerateHashCode(callback, arg1, arg2);
var cacheResult = GetCachedResult<Q>(hash);
if (!object.Equals(cacheResult, default(Q)))
{
return cacheResult;
}
try
{
var result = await callback(arg1, arg2);
_resultCache[hash] = new CacheItem(result);
return result;
}
catch (Exception e)
{
_resultCache[hash] = new CacheItem(e);
throw;
}
}
public async Task<Q> Memorise<P, T, R, Q>(Func<P, T, R, Task<Q>> callback, P arg1, T arg2, R arg3)
{
var hash = GenerateHashCode(callback, arg1, arg2, arg3);
var cacheResult = GetCachedResult<Q>(hash);
if (!object.Equals(cacheResult, default(Q)))
{
return cacheResult;
}
try
{
var result = await callback(arg1, arg2, arg3);
_resultCache[hash] = new CacheItem(result);
return result;
}
catch (Exception e)
{
_resultCache[hash] = new CacheItem(e);
throw;
}
}
private class CacheItem
{
public CacheItem(object value)
{
Value = value;
AddedAt = DateTime.Now;
}
public object Value { get; }
public DateTime AddedAt { get; }
}
}
}