-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheParameters.cs
More file actions
161 lines (144 loc) · 4.72 KB
/
Copy pathCacheParameters.cs
File metadata and controls
161 lines (144 loc) · 4.72 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
namespace DigitalRuby.SimpleCache;
/// <summary>
/// Caching parameters that includes duration and size
/// </summary>
public struct CacheParameters
{
/// <summary>
/// Default cache duration
/// </summary>
public static readonly TimeSpan DefaultDuration = TimeSpan.FromMinutes(30.0);
/// <summary>
/// Default cache size of 128
/// </summary>
public const int DefaultSize = 128;
/// <summary>
/// Constructor
/// </summary>
/// <param name="duration">Duration</param>
/// <param name="jitterDuration">Whether to jitter the duration</param>
public CacheParameters(TimeSpan duration, bool jitterDuration = true)
{
Duration = duration;
Size = DefaultSize;
if (jitterDuration)
{
JitterDuration();
}
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="duration">Duration</param>
/// <param name="size">Size</param>
/// <param name="jitterDuration">Whether to jitter the duration</param>
public CacheParameters(TimeSpan duration, int size, bool jitterDuration = true)
{
Duration = duration;
Size = size <= 0 ? DefaultSize : size;
if (jitterDuration)
{
JitterDuration();
}
}
/// <summary>
/// Assign default cache parameters if duration is less than or equal to zero
/// </summary>
/// <param name="cacheParameters">Cache parameters</param>
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
internal static void AssignDefaultIfNeeded(ref CacheParameters cacheParameters)
{
if (cacheParameters.Duration.Ticks <= 0)
{
cacheParameters.Duration = DefaultDuration;
}
}
/// <summary>
/// Wiggle the cache duration slightly to avoid having multiple cache items expire all at once
/// </summary>
private void JitterDuration()
{
double upperJitter;
if (Duration.TotalMinutes <= 1.0)
{
// don't jitter cache times 1 minute or less
return;
}
else if (Duration.TotalMinutes <= 15.0)
{
// up to 15 min
upperJitter = 1.2;
}
else if (Duration.TotalMinutes <= 60.0)
{
// up to 1 hour
upperJitter = 1.15;
}
else if (Duration.TotalMinutes <= 360.0)
{
// up to 6 hours
upperJitter = 1.1;
}
else if (Duration.TotalMinutes <= 1440.0)
{
// up to 24 hours
upperJitter = 1.05;
}
else
{
// > 1 day
upperJitter = 1.025;
}
double randomDouble = Random.Shared.NextDouble();
double multiplier = 1.0 + (randomDouble * upperJitter);
long jitteredTicks = (long)(Duration.Ticks * multiplier);
Duration = TimeSpan.FromTicks(jitteredTicks);
}
/// <summary>
/// Implicit operator of cache parameters to tuple
/// </summary>
/// <param name="value">Value</param>
public static implicit operator ValueTuple<TimeSpan, int>(CacheParameters value) => new(value.Duration, value.Size);
/// <summary>
/// Implicit operator of int minutes to cache parameters
/// </summary>
/// <param name="minutes">Minutes to cache</param>
public static implicit operator CacheParameters(int minutes) => new(TimeSpan.FromMinutes(minutes), DefaultSize);
/// <summary>
/// Implicit operator of TimeSpan to cache parameters
/// </summary>
/// <param name="timespan">Timespan</param>
public static implicit operator CacheParameters(TimeSpan timespan) => new(timespan, DefaultSize);
/// <summary>
/// Implicit operator of tuple of timespan, int size to cache parameters
/// </summary>
/// <param name="tuple">Value</param>
public static implicit operator CacheParameters(ValueTuple<TimeSpan, int> tuple) => new(tuple.Item1, tuple.Item2);
/// <summary>
/// Implicit operator of tuple of int minutes, int size to cache parameters
/// </summary>
/// <param name="tuple">Value</param>
public static implicit operator CacheParameters(ValueTuple<int, int> tuple) => new(TimeSpan.FromMinutes(tuple.Item1), tuple.Item2);
/// <summary>
/// Implicit operator of tuple of float minutes, int size to cache parameters
/// </summary>
/// <param name="tuple">Value</param>
public static implicit operator CacheParameters(ValueTuple<float, int> tuple) => new(TimeSpan.FromMinutes(tuple.Item1), tuple.Item2);
/// <summary>
/// Implicit operator of tuple of double minutes, int size to cache parameters
/// </summary>
/// <param name="tuple">Value</param>
public static implicit operator CacheParameters(ValueTuple<double, int> tuple) => new(TimeSpan.FromMinutes(tuple.Item1), tuple.Item2);
/// <summary>
/// Default cache parameters
/// </summary>
public static readonly CacheParameters Default = new(DefaultDuration);
/// <summary>
/// The duration before the cache item expires.
/// </summary>
public TimeSpan Duration { get; private set; }
/// <summary>
/// The estimated size, in bytes, of the cached object. Default is 128. Ignored for some cache like distributed caches.
/// </summary>
public int Size { get; set; }
}