/
MemcacedCache.cs
76 lines (59 loc) · 1.91 KB
/
MemcacedCache.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
using System;
using System.Net;
using System.Threading.Tasks;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
namespace MemcachedTryout
{
internal class MemcacedCache : ICache
{
private readonly MemcachedClient _cache;
private readonly ILogger _logger;
public MemcacedCache()
{
_logger = new Logger();
var config = new MemcachedClientConfiguration();
config.Servers.Add(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11211));
config.Protocol = MemcachedProtocol.Binary;
_cache = new MemcachedClient(null, config);
_cache.NodeFailed += NodeFailedEvent;
}
public async Task<bool> AddAsync<T>(string key, T value)
{
_logger.Debug("Adding item to cache with key {0}", key);
return await _cache.StoreAsync(StoreMode.Set, key, value, TimeSpan.FromDays(90));
}
public async Task<T> GetWithSetAsync<T>(string cacheKey, Func<Task<T>> initializeFunction)
{
var data = await GetAsync<T>(cacheKey);
if (data != null)
{
return data;
}
data = await initializeFunction();
await AddAsync(cacheKey, data);
return data;
}
public async Task<T> GetAsync<T>(string key)
{
var cacheItem = await _cache.GetAsync<T>(key);
if (cacheItem == null)
{
_logger.Debug("Cache miss with key {0}", key);
return default(T);
}
_logger.Debug("Cache hit with key {0}", key);
return cacheItem;
}
public async Task<bool> RemoveAsync(string key)
{
_logger.Debug("Removing item from cache with key {0}", key);
return await _cache.RemoveAsync(key);
}
private void NodeFailedEvent(IMemcachedNode memcachedNode)
{
_logger.Error("Node unavailable");
}
}
}