Repository navigation
Expand file tree
/
Copy pathCacheClientExtensions.cs
More file actions
596 lines (502 loc) · 24.3 KB
/
Copy pathCacheClientExtensions.cs
File metadata and controls
596 lines (502 loc) · 24.3 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
using System;
using System.Collections.Generic;
using ServiceStack.Caching;
using ServiceStack.Web;
using ServiceStack.Text;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
namespace ServiceStack
{
public static class CacheClientExtensions
{
public static void Set<T>(this ICacheClient cache, string cacheKey, T value, TimeSpan? expireCacheIn)
{
if (expireCacheIn.HasValue)
cache.Set(cacheKey, value, expireCacheIn.Value);
else
cache.Set(cacheKey, value);
}
public static async Task SetAsync<T>(this ICacheClientAsync cache, string cacheKey, T value, TimeSpan? expireCacheIn, CancellationToken token=default)
{
if (expireCacheIn.HasValue)
await cache.SetAsync(cacheKey, value, expireCacheIn.Value, token);
else
await cache.SetAsync(cacheKey, value, token);
}
private static string DateCacheKey(string cacheKey) => cacheKey + ".created";
public static DateTime? GetDate(this IRequest req)
{
var date = req.Headers[HttpHeaders.Date];
if (date == null)
return null;
if (!DateTime.TryParse(date, new DateTimeFormatInfo(), DateTimeStyles.RoundtripKind, out var value))
return null;
return value;
}
public static DateTime? GetIfModifiedSince(this IRequest req)
{
var ifModifiedSince = req.Headers[HttpHeaders.IfModifiedSince];
if (ifModifiedSince == null)
return null;
if (!DateTime.TryParse(ifModifiedSince, new DateTimeFormatInfo(), DateTimeStyles.RoundtripKind, out var value))
return null;
return value;
}
public static bool HasValidCache(this ICacheClient cache, IRequest req, string cacheKey, DateTime? checkLastModified, out DateTime? lastModified)
{
lastModified = null;
if (!HostContext.GetPlugin<HttpCacheFeature>().ShouldAddLastModifiedToOptimizedResults())
return false;
var ticks = cache.Get<long>(DateCacheKey(cacheKey));
if (ticks > 0)
{
lastModified = new DateTime(ticks, DateTimeKind.Utc);
if (checkLastModified == null)
return false;
return checkLastModified.Value <= lastModified.Value;
}
return false;
}
public struct ValidCache
{
public static ValidCache NotValid = new ValidCache(false, DateTime.MinValue);
public bool IsValid { get; }
public DateTime LastModified { get; }
public ValidCache(bool isValid, DateTime lastModified)
{
IsValid = isValid;
LastModified = lastModified;
}
}
public static async Task<ValidCache> HasValidCacheAsync(this ICacheClientAsync cache, IRequest req, string cacheKey, DateTime? checkLastModified,
CancellationToken token=default)
{
if (!HostContext.GetPlugin<HttpCacheFeature>().ShouldAddLastModifiedToOptimizedResults())
return ValidCache.NotValid;
var ticks = await cache.GetAsync<long>(DateCacheKey(cacheKey), token).ConfigAwait();
if (ticks > 0)
{
if (checkLastModified == null)
return ValidCache.NotValid;
var lastModified = new DateTime(ticks, DateTimeKind.Utc);
return new ValidCache(checkLastModified.Value <= lastModified, lastModified);
}
return ValidCache.NotValid;
}
public static object ResolveFromCache(this ICacheClient cache, string cacheKey, IRequest req)
{
var checkModifiedSince = GetIfModifiedSince(req);
if (!req.ResponseContentType.IsBinary())
{
string modifiers = null;
if (req.ResponseContentType == MimeTypes.Json)
{
string jsonp = req.GetJsonpCallback();
if (jsonp != null)
modifiers = ".jsonp," + jsonp.SafeVarName();
}
var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers);
var compressionType = req.GetCompressionType();
bool doCompression = compressionType != null;
if (doCompression)
{
var cacheKeySerializedZip = GetCacheKeyForCompressed(cacheKeySerialized, compressionType);
if (cache.HasValidCache(req, cacheKeySerializedZip, checkModifiedSince, out var lastModified))
return HttpResult.NotModified();
if (req.Response.GetHeader(HttpHeaders.CacheControl) != null)
lastModified = null;
var compressedResult = cache.Get<byte[]>(cacheKeySerializedZip);
if (compressedResult != null)
{
return new CompressedResult(
compressedResult,
compressionType,
req.ResponseContentType)
{
LastModified = lastModified,
};
}
}
else
{
if (cache.HasValidCache(req, cacheKeySerialized, checkModifiedSince, out _))
return HttpResult.NotModified();
var serializedResult = cache.Get<string>(cacheKeySerialized);
if (serializedResult != null)
{
return serializedResult;
}
}
}
else
{
var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers:null);
if (cache.HasValidCache(req, cacheKeySerialized, checkModifiedSince, out _))
return HttpResult.NotModified();
var serializedResult = cache.Get<byte[]>(cacheKeySerialized);
if (serializedResult != null)
{
return serializedResult;
}
}
return null;
}
public static async Task<object> ResolveFromCacheAsync(this ICacheClientAsync cache, string cacheKey, IRequest req,
CancellationToken token=default)
{
var checkModifiedSince = GetIfModifiedSince(req);
if (!req.ResponseContentType.IsBinary())
{
string modifiers = null;
if (req.ResponseContentType == MimeTypes.Json)
{
string jsonp = req.GetJsonpCallback();
if (jsonp != null)
modifiers = ".jsonp," + jsonp.SafeVarName();
}
var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers);
var compressionType = req.GetCompressionType();
bool doCompression = compressionType != null;
if (doCompression)
{
var cacheKeySerializedZip = GetCacheKeyForCompressed(cacheKeySerialized, compressionType);
var validCache = await cache.HasValidCacheAsync(req, cacheKeySerializedZip, checkModifiedSince, token).ConfigAwait();
if (validCache.IsValid)
return HttpResult.NotModified();
DateTime? lastModified = validCache.LastModified;
if (req.Response.GetHeader(HttpHeaders.CacheControl) != null)
lastModified = null;
var compressedResult = await cache.GetAsync<byte[]>(cacheKeySerializedZip, token).ConfigAwait();
if (compressedResult != null)
{
return new CompressedResult(
compressedResult,
compressionType,
req.ResponseContentType)
{
LastModified = lastModified,
};
}
}
else
{
if ((await cache.HasValidCacheAsync(req, cacheKeySerialized, checkModifiedSince, token).ConfigAwait()).IsValid)
return HttpResult.NotModified();
var serializedResult = await cache.GetAsync<string>(cacheKeySerialized, token).ConfigAwait();
if (serializedResult != null)
{
return serializedResult;
}
}
}
else
{
var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers:null);
if ((await cache.HasValidCacheAsync(req, cacheKeySerialized, checkModifiedSince, token).ConfigAwait()).IsValid)
return HttpResult.NotModified();
var serializedResult = await cache.GetAsync<byte[]>(cacheKeySerialized, token).ConfigAwait();
if (serializedResult != null)
{
return serializedResult;
}
}
return null;
}
internal static string SerializeToString(this IRequest request, object responseDto)
{
var str = responseDto as string;
return str ?? HostContext.ContentTypes.SerializeToString(request, responseDto);
}
public static object Cache(this ICacheClient cache,
string cacheKey,
object responseDto,
IRequest req,
TimeSpan? expireCacheIn = null)
{
req.Response.Dto = responseDto;
cache.Set(cacheKey, responseDto, expireCacheIn);
if (!req.ResponseContentType.IsBinary())
{
string serializedDto = SerializeToString(req, responseDto);
string modifiers = null;
if (req.ResponseContentType.MatchesContentType(MimeTypes.Json))
{
var jsonp = req.GetJsonpCallback();
if (jsonp != null)
{
modifiers = ".jsonp," + jsonp.SafeVarName();
serializedDto = jsonp + "(" + serializedDto + ")";
//Add a default expire timespan for jsonp requests,
//because they aren't cleared when calling ClearCaches()
if (expireCacheIn == null)
expireCacheIn = HostContext.Config.DefaultJsonpCacheExpiration;
}
}
var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers);
cache.Set(cacheKeySerialized, serializedDto, expireCacheIn);
var compressionType = req.GetCompressionType();
bool doCompression = compressionType != null;
if (doCompression)
{
var lastModified = HostContext.GetPlugin<HttpCacheFeature>().ShouldAddLastModifiedToOptimizedResults()
&& string.IsNullOrEmpty(req.Response.GetHeader(HttpHeaders.CacheControl))
? DateTime.UtcNow
: (DateTime?)null;
var cacheKeySerializedZip = GetCacheKeyForCompressed(cacheKeySerialized, compressionType);
byte[] compressedSerializedDto = serializedDto.Compress(compressionType);
cache.Set(cacheKeySerializedZip, compressedSerializedDto, expireCacheIn);
if (lastModified != null)
cache.Set(DateCacheKey(cacheKeySerializedZip), lastModified.Value.Ticks, expireCacheIn);
return compressedSerializedDto != null
? new CompressedResult(compressedSerializedDto, compressionType, req.ResponseContentType)
{
Status = req.Response.StatusCode,
LastModified = lastModified,
}
: null;
}
return serializedDto;
}
else
{
string modifiers = null;
byte[] serializedDto = HostContext.ContentTypes.SerializeToBytes(req, responseDto);
var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers);
cache.Set(cacheKeySerialized, serializedDto, expireCacheIn);
return serializedDto;
}
}
public static async Task<object> CacheAsync(this ICacheClientAsync cache,
string cacheKey,
object responseDto,
IRequest req,
TimeSpan? expireCacheIn = null,
CancellationToken token=default)
{
req.Response.Dto = responseDto;
await cache.SetAsync(cacheKey, responseDto, expireCacheIn, token).ConfigAwait();
if (!req.ResponseContentType.IsBinary())
{
string serializedDto = SerializeToString(req, responseDto);
string modifiers = null;
if (req.ResponseContentType.MatchesContentType(MimeTypes.Json))
{
var jsonp = req.GetJsonpCallback();
if (jsonp != null)
{
modifiers = ".jsonp," + jsonp.SafeVarName();
serializedDto = jsonp + "(" + serializedDto + ")";
//Add a default expire timespan for jsonp requests,
//because they aren't cleared when calling ClearCaches()
if (expireCacheIn == null)
expireCacheIn = HostContext.Config.DefaultJsonpCacheExpiration;
}
}
var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers);
await cache.SetAsync(cacheKeySerialized, serializedDto, expireCacheIn, token).ConfigAwait();
var compressionType = req.GetCompressionType();
bool doCompression = compressionType != null;
if (doCompression)
{
var lastModified = HostContext.GetPlugin<HttpCacheFeature>().ShouldAddLastModifiedToOptimizedResults()
&& string.IsNullOrEmpty(req.Response.GetHeader(HttpHeaders.CacheControl))
? DateTime.UtcNow
: (DateTime?)null;
var cacheKeySerializedZip = GetCacheKeyForCompressed(cacheKeySerialized, compressionType);
byte[] compressedSerializedDto = serializedDto.Compress(compressionType);
await cache.SetAsync(cacheKeySerializedZip, compressedSerializedDto, expireCacheIn, token).ConfigAwait();
if (lastModified != null)
await cache.SetAsync(DateCacheKey(cacheKeySerializedZip), lastModified.Value.Ticks, expireCacheIn, token).ConfigAwait();
return compressedSerializedDto != null
? new CompressedResult(compressedSerializedDto, compressionType, req.ResponseContentType)
{
Status = req.Response.StatusCode,
LastModified = lastModified,
}
: null;
}
return serializedDto;
}
else
{
string modifiers = null;
byte[] serializedDto = HostContext.ContentTypes.SerializeToBytes(req, responseDto);
var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers);
await cache.SetAsync(cacheKeySerialized, serializedDto, expireCacheIn, token).ConfigAwait();
return serializedDto;
}
}
private static List<string> GetAllContentCacheKeys(string[] cacheKeys)
{
var allContentTypes = new List<string>(HostContext.ContentTypes.ContentTypeFormats.Values) {
MimeTypes.XmlText, MimeTypes.JsonText, MimeTypes.JsvText
};
var allCacheKeys = new List<string>();
foreach (var cacheKey in cacheKeys)
{
allCacheKeys.Add(cacheKey);
foreach (var serializedExt in allContentTypes)
{
var serializedCacheKey = GetCacheKeyForSerialized(cacheKey, serializedExt, null);
allCacheKeys.Add(serializedCacheKey);
allCacheKeys.Add(DateCacheKey(serializedCacheKey));
foreach (var compressionType in CompressionTypes.AllCompressionTypes)
{
var compressedCacheKey = GetCacheKeyForCompressed(serializedCacheKey, compressionType);
allCacheKeys.Add(compressedCacheKey);
allCacheKeys.Add(DateCacheKey(compressedCacheKey));
}
}
}
return allCacheKeys;
}
public static void ClearCaches(this ICacheClient cache, params string[] cacheKeys)
{
var allCacheKeys = GetAllContentCacheKeys(cacheKeys);
cache.RemoveAll(allCacheKeys);
}
public static async Task ClearCachesAsync(this ICacheClientAsync cache, string[] cacheKeys, CancellationToken token=default)
{
var allCacheKeys = GetAllContentCacheKeys(cacheKeys);
await cache.RemoveAllAsync(allCacheKeys, token);
}
public static string GetCacheKeyForSerialized(string cacheKey, string mimeType, string modifiers)
{
return cacheKey + MimeTypes.GetExtension(mimeType) + modifiers;
}
public static string GetCacheKeyForCompressed(string cacheKeySerialized, string compressionType)
{
return cacheKeySerialized + "." + compressionType;
}
/// <summary>
/// Removes items from cache that have keys matching the specified wildcard pattern
/// </summary>
/// <param name="cacheClient">Cache client</param>
/// <param name="pattern">The wildcard, where "*" means any sequence of characters and "?" means any single character.</param>
public static void RemoveByPattern(this ICacheClient cacheClient, string pattern)
{
if (!(cacheClient is IRemoveByPattern canRemoveByPattern))
throw new NotImplementedException(
"IRemoveByPattern is not implemented on: " + cacheClient.GetType().FullName);
canRemoveByPattern.RemoveByPattern(pattern);
}
/// <summary>
/// Removes items from cache that have keys matching the specified wildcard pattern
/// </summary>
/// <param name="cacheClient">Cache client</param>
/// <param name="pattern">The wildcard, where "*" means any sequence of characters and "?" means any single character.</param>
/// <param name="token"></param>
public static Task RemoveByPatternAsync(this ICacheClientAsync cacheClient, string pattern, CancellationToken token=default)
{
if (!(cacheClient is IRemoveByPatternAsync canRemoveByPattern))
throw new NotImplementedException(
"IRemoveByPattern is not implemented on: " + cacheClient.GetType().FullName);
return canRemoveByPattern.RemoveByPatternAsync(pattern, token);
}
/// <summary>
/// Removes items from the cache based on the specified regular expression pattern
/// </summary>
/// <param name="cacheClient">Cache client</param>
/// <param name="regex">Regular expression pattern to search cache keys</param>
public static void RemoveByRegex(this ICacheClient cacheClient, string regex)
{
if (!(cacheClient is IRemoveByPattern canRemoveByPattern))
throw new NotImplementedException("IRemoveByPattern is not implemented by: " + cacheClient.GetType().FullName);
canRemoveByPattern.RemoveByRegex(regex);
}
/// <summary>
/// Removes items from the cache based on the specified regular expression pattern
/// </summary>
/// <param name="cacheClient">Cache client</param>
/// <param name="regex">Regular expression pattern to search cache keys</param>
public static Task RemoveByRegexAsync(this ICacheClientAsync cacheClient, string regex)
{
if (!(cacheClient is IRemoveByPatternAsync canRemoveByPattern))
throw new NotImplementedException("IRemoveByPattern is not implemented by: " + cacheClient.GetType().FullName);
return canRemoveByPattern.RemoveByRegexAsync(regex);
}
public static IEnumerable<string> GetKeysByPattern(this ICacheClient cache, string pattern)
{
if (!(cache is ICacheClientExtended extendedCache))
throw new NotImplementedException("ICacheClientExtended is not implemented by: " + cache.GetType().FullName);
return extendedCache.GetKeysByPattern(pattern);
}
public static IEnumerable<string> GetAllKeys(this ICacheClient cache)
{
return cache.GetKeysByPattern("*");
}
public static IEnumerable<string> GetKeysStartingWith(this ICacheClient cache, string prefix)
{
return cache.GetKeysByPattern(prefix + "*");
}
public static IAsyncEnumerable<string> GetKeysByPatternAsync(this ICacheClientAsync cache, string pattern)
{
return cache.GetKeysByPatternAsync(pattern);
}
public static async IAsyncEnumerable<string> GetAllKeysAsync(this ICacheClientAsync cache)
{
await foreach (var key in cache.GetKeysByPatternAsync("*"))
{
yield return key;
}
}
public static async IAsyncEnumerable<string> GetKeysStartingWithAsync(this ICacheClientAsync cache, string prefix)
{
await foreach (var key in cache.GetKeysByPatternAsync(prefix + "*"))
{
yield return key;
}
}
public static T GetOrCreate<T>(this ICacheClient cache,
string key, Func<T> createFn)
{
var value = cache.Get<T>(key);
if (Equals(value, default(T)))
{
value = createFn();
cache.Set(key, value);
}
return value;
}
public static async Task<T> GetOrCreateAsync<T>(this ICacheClientAsync cache,
string key, Func<Task<T>> createFn)
{
var value = await cache.GetAsync<T>(key);
if (Equals(value, default(T)))
{
value = await createFn().ConfigAwait();
await cache.SetAsync(key, value);
}
return value;
}
public static T GetOrCreate<T>(this ICacheClient cache,
string key, TimeSpan expiresIn, Func<T> createFn)
{
var value = cache.Get<T>(key);
if (Equals(value, default(T)))
{
value = createFn();
cache.Set(key, value, expiresIn);
}
return value;
}
public static async Task<T> GetOrCreateAsync<T>(this ICacheClientAsync cache,
string key, TimeSpan expiresIn, Func<Task<T>> createFn)
{
var value = await cache.GetAsync<T>(key);
if (Equals(value, default(T)))
{
value = await createFn().ConfigAwait();
await cache.SetAsync(key, value, expiresIn);
}
return value;
}
public static TimeSpan? GetTimeToLive(this ICacheClient cache, string key)
{
if (!(cache is ICacheClientExtended extendedCache))
throw new Exception("GetTimeToLive is not implemented by: " + cache.GetType().FullName);
return extendedCache.GetTimeToLive(key);
}
}
}