-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskMemCacheTests.cs
More file actions
147 lines (115 loc) · 5 KB
/
DiskMemCacheTests.cs
File metadata and controls
147 lines (115 loc) · 5 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
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
using System.Diagnostics;
using Temppus.Caching;
// ReSharper disable once CheckNamespace
namespace Temppus.Tests
{
public class DiskMemCacheTests : IDisposable
{
public DiskMemCacheTests()
{
DiskMemCache.PurgeAll();
}
public class Item
{
public int Value { get; set; }
}
private static Task<Item> ComputeItem10() => Task.FromResult(new Item { Value = 10 });
private static Task<Item> ComputeItem20() => Task.FromResult(new Item { Value = 20 });
private static async Task<Item> LongRunningOperationFunctionToCacheAsync()
{
// expensive computation / long IO operation
await Task.Delay(1000);
return new Item { Value = 9000 };
}
[Fact]
public async Task Test_Caching_Example()
{
// first operation with this key will call function and cache result in memory cache + also on file as JSON
var result = await DiskMemCache.GetOrComputeAsync("my-expensive-func", LongRunningOperationFunctionToCacheAsync);
Assert.Equal(9000, result.Value);
var sw = Stopwatch.StartNew();
// all next results are returned from cache
// if cached item spend in cache is less than 1 hour
for (int i = 0; i < 100; i++)
{
var resultFromMemCache = await DiskMemCache.GetOrComputeAsync("my-expensive-func",
LongRunningOperationFunctionToCacheAsync,
t => t > TimeSpan.FromHours(1));
Assert.Equal(9000, resultFromMemCache.Value);
}
// process re-started (assuming less than 1 hour from item being cached)
// result will be returned from serialized file and not computed again
var resultReturnedFromSerializedFile = await DiskMemCache.GetOrComputeAsync("my-expensive-func",
LongRunningOperationFunctionToCacheAsync,
t => t > TimeSpan.FromHours(1));
Assert.Equal(9000, resultReturnedFromSerializedFile.Value);
sw.Stop();
Assert.True(sw.Elapsed < TimeSpan.FromSeconds(2));
}
[Fact]
public async Task Test_Caching_Simple()
{
var key = Guid.NewGuid().ToString();
var x = await DiskMemCache.GetOrComputeAsync(key, ComputeItem10);
Assert.Equal(10, x.Value);
x = await DiskMemCache.GetOrComputeAsync(key, ComputeItem20);
Assert.Equal(10, x.Value);
x = await DiskMemCache.GetOrComputeAsync(key, ComputeItem20, t => t > TimeSpan.FromMilliseconds(500));
Assert.Equal(10, x.Value);
await Task.Delay(500);
x = await DiskMemCache.GetOrComputeAsync(key, ComputeItem20, t => t > TimeSpan.FromMilliseconds(100));
Assert.Equal(20, x.Value);
}
[Fact]
public async Task Test_Caching_Scalar()
{
var key = Guid.NewGuid().ToString();
var x = await DiskMemCache.GetOrComputeAsync(key, () => Task.FromResult(9));
Assert.Equal(9, x);
x = await DiskMemCache.GetOrComputeAsync(key, () => Task.FromResult(9));
Assert.Equal(9, x);
}
[Fact]
public async Task Test_Caching_Keys()
{
DiskMemCache.PurgeAll();
{
var key1 = Guid.NewGuid().ToString();
var x = await DiskMemCache.GetOrComputeAsync(key1, ComputeItem10);
Assert.Equal(10, x.Value);
x = await DiskMemCache.GetOrComputeAsync(key1, ComputeItem20);
Assert.Equal(10, x.Value);
}
{
var key2 = Guid.NewGuid().ToString();
var y = await DiskMemCache.GetOrComputeAsync(key2, ComputeItem20);
Assert.Equal(20, y.Value);
y = await DiskMemCache.GetOrComputeAsync(key2, ComputeItem10);
Assert.Equal(20, y.Value);
}
}
[Fact]
public async Task Test_Cache_Purging()
{
var key1 = Guid.NewGuid().ToString();
var x = await DiskMemCache.GetOrComputeAsync(key1, ComputeItem10);
Assert.Equal(10, x.Value);
x = await DiskMemCache.GetOrComputeAsync(key1, ComputeItem20);
Assert.Equal(10, x.Value);
var key2 = Guid.NewGuid().ToString();
var y = await DiskMemCache.GetOrComputeAsync(key2, ComputeItem10);
Assert.Equal(10, y.Value);
y = await DiskMemCache.GetOrComputeAsync(key2, ComputeItem20);
Assert.Equal(10, y.Value);
DiskMemCache.Purge(k => k == key1);
x = await DiskMemCache.GetOrComputeAsync(key1, ComputeItem20);
Assert.Equal(20, x.Value);
y = await DiskMemCache.GetOrComputeAsync(key2, ComputeItem20);
Assert.Equal(10, y.Value);
}
public void Dispose()
{
DiskMemCache.PurgeAll();
}
}
}