Repository navigation
Expand file tree
/
Copy pathWebReportDistributedCache.cs
More file actions
85 lines (69 loc) · 2.22 KB
/
Copy pathWebReportDistributedCache.cs
File metadata and controls
85 lines (69 loc) · 2.22 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Diagnostics;
namespace FastReport.Web.Cache
{
internal class WebReportDistributedCache : IWebReportCache
{
private readonly IDistributedCache _cache;
private readonly DistributedCacheEntryOptions _cacheEntryOptions;
private static readonly IFormatter _bf = new BinaryFormatter();
public WebReportDistributedCache(IDistributedCache cache)
{
_cache = cache;
_cacheEntryOptions = new DistributedCacheEntryOptions()
{
SlidingExpiration = FastReportGlobal.FastReportOptions.CacheOptions.CacheDuration
};
}
public void Add(WebReport webReport)
{
if(_cache.Get(webReport.ID) != null)
{
Debug.WriteLine($"WebReport with '{webReport.ID}' id was added before, but someone is trying to rewrite it");
return;
}
_cache.Set(webReport.ID, WebReportToBytes(webReport), _cacheEntryOptions);
}
public void Touch(string id)
{
_cache.Refresh(id);
}
public WebReport Find(string id)
{
var bytes = _cache.Get(id);
var webReport = BytesToWebReport(bytes);
return webReport;
}
public void Remove(WebReport webReport)
{
_cache.Remove(webReport.ID);
}
public void Dispose()
{
}
private static byte[] WebReportToBytes(WebReport value)
{
var ms = new MemoryStream();
_bf.Serialize(ms, value);
return ms.ToArray();
}
private static WebReport BytesToWebReport(byte[] value)
{
using (var ms = new MemoryStream(value, false))
{
var obj = _bf.Deserialize(ms);
var webReport = obj as WebReport;
return webReport;
}
}
}
}