-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTimeCache.cs
More file actions
37 lines (32 loc) · 895 Bytes
/
TimeCache.cs
File metadata and controls
37 lines (32 loc) · 895 Bytes
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;
namespace PropertyChange
{
public interface ICache<T>
{
T Get(string key, Func<T> getValue);
}
public class TimeCache<T> : ICache<T> where T : class
{
public MemoryCache m_cache = new MemoryCache(Guid.NewGuid().ToString());
TimeSpan m_cacheLife;
public TimeCache(TimeSpan cacheLifeTime)
{
m_cacheLife = cacheLifeTime;
}
public T Get(string key, Func<T> getValue)
{
T value = m_cache[key] as T;
if (value == null)
{
value = getValue();
m_cache.Add(key, value, new CacheItemPolicy() { SlidingExpiration = m_cacheLife });
}
return value;
}
}
}