-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoystickDefaultCacheService.cs
47 lines (40 loc) · 1.24 KB
/
JoystickDefaultCacheService.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
using System;
using System.Runtime.Caching;
using Joystick.Client.Models;
namespace Joystick.Client.Services.Cache
{
internal class JoystickDefaultCacheService : IJoystickCacheService
{
private JoystickCacheOptions options;
private MemoryCache cache;
public JoystickDefaultCacheService(JoystickCacheOptions options)
{
this.options = options ?? new JoystickCacheOptions();
this.cache = MemoryCache.Default;
}
public void Set(string key, string value)
{
var policy = new CacheItemPolicy
{
AbsoluteExpiration = DateTimeOffset.UtcNow.AddSeconds((double)this.options.CacheExpirationSeconds),
};
this.cache.Add(key, value, policy);
}
public bool TryGet(string key, out string value)
{
var cachedValue = this.cache.Get(key);
if (cachedValue != null)
{
value = (string)cachedValue;
return true;
}
value = default(string);
return false;
}
public void ClearAll()
{
this.cache.Dispose();
this.cache = MemoryCache.Default;
}
}
}