-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCache.Helper.cs
More file actions
548 lines (486 loc) · 20.6 KB
/
Cache.Helper.cs
File metadata and controls
548 lines (486 loc) · 20.6 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
#region Related components
using System;
using System.Net;
using System.Xml;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using net.vieapps.Components.Caching;
using CacheUtils;
#endregion
namespace net.vieapps.Components.Caching
{
public static class Helper
{
#region Data
public const int FlagOfFirstFragmentBlock = 0xfe52;
public static readonly int FragmentSize = (1024 * 1024) - 256;
internal static readonly string RegionsKey = "VIEApps-NGX-Regions";
public static Random Random { get; } = new Random();
public static int ExpirationTime => Cache.Configuration != null && Cache.Configuration.ExpirationTime > 0 ? Cache.Configuration.ExpirationTime : 30;
public static string GetRegionName(string name)
=> Regex.Replace(!string.IsNullOrWhiteSpace(name) ? name : Cache.Configuration?.RegionName ?? "VIEApps-NGX-Cache", "[^0-9a-zA-Z:-]+", "");
public static string GetCacheKey(string region, string key)
=> region + "@" + key.Replace(" ", "-");
public static string GetFragmentKey(string key, int index)
{
var fragmentKey = "0" + index.ToString();
return key.Replace(" ", "-") + "$[Fragment<" + fragmentKey.Substring(fragmentKey.Length - 2) + ">]";
}
internal static List<string> GetFragmentKeys(string key, int max)
{
var keys = new List<string> { key };
for (var index = 1; index <= max; index++)
keys.Add(Helper.GetFragmentKey(key, index));
return keys;
}
#endregion
#region Serialize & Deserialize
/// <summary>
/// Gets the flags
/// </summary>
/// <param name="data"></param>
/// <param name="getLength"></param>
/// <returns></returns>
public static (int TypeFlag, int Length) GetFlags(this byte[] data, bool getLength = false)
{
if (data == null || data.Length < 4)
return (0, 0);
var tmp = new byte[4];
Buffer.BlockCopy(data, 0, tmp, 0, 4);
var typeFlag = BitConverter.ToInt32(tmp, 0);
var length = data.Length - 4;
if (getLength && data.Length > 7)
{
Buffer.BlockCopy(data, 4, tmp, 0, 4);
length = BitConverter.ToInt32(tmp, 0);
}
return (typeFlag, length);
}
/// <summary>
/// Serializes an object into array of bytes
/// </summary>
/// <param name="value"></param>
/// <param name="addFlags"></param>
/// <returns></returns>
public static byte[] Serialize(object value, bool addFlags = true)
{
var data = CacheUtils.Helper.Serialize(value);
return addFlags
? CacheUtils.Helper.Concat(new[] { BitConverter.GetBytes(data.TypeFlag), data.Data })
: data.Data;
}
/// <summary>
/// Deserializes an object from the array of bytes
/// </summary>
/// <param name="data"></param>
/// <param name="typeFlag"></param>
/// <param name="start"></param>
/// <param name="count"></param>
/// <returns></returns>
public static object Deserialize(byte[] data, int typeFlag, int start, int count)
=> CacheUtils.Helper.Deserialize(data, typeFlag, start, count);
/// <summary>
/// Deserializes an object from the array of bytes
/// </summary>
/// <param name="data"></param>
/// <param name="start"></param>
/// <param name="count"></param>
/// <returns></returns>
public static object Deserialize(byte[] data, int start, int count)
=> Helper.Deserialize(data, (int)TypeCode.Object | 0x0100, start, count);
/// <summary>
/// Deserializes an object from the array of bytes
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static object Deserialize(byte[] data)
=> data == null || data.Length < 4
? null
: Helper.Deserialize(data, data.GetFlags().TypeFlag, 4, data.Length - 4);
/// <summary>
/// Deserializes an object from the array of bytes
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static T Deserialize<T>(byte[] data)
{
var value = data != null ? Helper.Deserialize(data) : null;
return value != null && value is T val ? val : default;
}
internal static object DeserializeFromFragments(this byte[] data)
{
var tmp = new byte[4];
Buffer.BlockCopy(data, 8, tmp, 0, 4);
var typeFlag = BitConverter.ToInt32(tmp, 0);
return Helper.Deserialize(data, typeFlag, 12, data.Length - 12);
}
/// <summary>
/// Gets the first fragment with attached information
/// </summary>
/// <param name="fragments"></param>
/// <returns></returns>
public static byte[] GetFirstFragment(this List<byte[]> fragments)
=> CacheUtils.Helper.Concat(new[] { BitConverter.GetBytes(Helper.FlagOfFirstFragmentBlock), BitConverter.GetBytes(fragments.Where(f => f != null).Sum(f => f.Length)), fragments[0] });
/// <summary>
/// Gets information of fragments
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static (int Blocks, int Length) GetFragmentsInfo(this byte[] data)
{
var info = data.GetFlags(true);
if (info.TypeFlag == 0 && info.Length == 0)
return (0, 0);
var blocks = 0;
var offset = 0;
var length = info.Length;
while (offset < length)
{
blocks++;
offset += Helper.FragmentSize;
}
return (blocks, length);
}
/// <summary>
/// Serializes an object to array of bytes using Json.NET BSON Serializer
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static byte[] SerializeBson(object value)
=> CacheUtils.Helper.SerializeByBson(value);
/// <summary>
/// Deserializes an object from an array of bytes using Json.NET BSON Deserializer
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static object DeserializeBson(byte[] value)
=> CacheUtils.Helper.SerializeByBson(value);
/// <summary>
/// Deserializes an object from an array of bytes using Json.NET BSON Deserializer
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static T DeserializeBson<T>(byte[] value)
=> CacheUtils.Helper.DeserializeByBson<T>(value);
#endregion
#region Working with logs
internal static ILogger Logger { get; set; } = Enyim.Caching.Logger.CreateLogger<Cache>();
internal static void WriteLogs(string region, List<string> logs, Exception ex)
{
if (ex != null)
{
logs.ForEach(log => Helper.Logger.LogInformation($"<{region}>: {log}"));
Helper.Logger.LogError(ex, ex.Message);
}
else if (Helper.Logger.IsEnabled(LogLevel.Debug))
logs.ForEach(log => Helper.Logger.LogInformation($"<{region}>: {log}"));
}
internal static void WriteLogs(string region, string log, Exception ex)
{
if (ex != null)
Logger.LogError(ex, $"<{region}>: {log}");
else if (Logger.IsEnabled(LogLevel.Debug))
Logger.LogInformation(ex, $"<{region}>: {log}");
}
#endregion
#region Working with configurations
/// <summary>
/// Gets the configuration for working with Redis
/// </summary>
/// <param name="configSection"></param>
/// <returns></returns>
public static RedisClientConfiguration GetRedisConfiguration(this CacheConfigurationSectionHandler configSection)
{
var configuration = new RedisClientConfiguration();
if (configSection.Section.SelectNodes("servers/add") is XmlNodeList servers)
foreach (XmlNode server in servers)
if ("redis".Equals((server.Attributes["type"]?.Value ?? "Redis").Trim().ToLower()))
{
var address = server.Attributes["address"]?.Value ?? "localhost";
var endpoint = (address.IndexOf(".") > 0 && address.IndexOf(":") > 0) || (address.IndexOf(":") > 0 && address.IndexOf("]:") > 0)
? ConfigurationHelper.ResolveToEndPoint(address)
: ConfigurationHelper.ResolveToEndPoint(address, Int32.TryParse(server.Attributes["port"]?.Value ?? "6379", out var port) ? port : 6379);
configuration.Servers.Add(endpoint as IPEndPoint);
}
if (configSection.Section.SelectSingleNode("options") is XmlNode options)
foreach (XmlAttribute option in options.Attributes)
if (!string.IsNullOrWhiteSpace(option.Value))
configuration.Options += (configuration.Options != "" ? "," : "") + option.Name + "=" + option.Value;
return configuration;
}
/// <summary>
/// Gets the configuration for working with Redis
/// </summary>
/// <param name="cacheConfiguration"></param>
/// <returns></returns>
public static RedisClientConfiguration GetRedisConfiguration(this ICacheConfiguration cacheConfiguration)
=> new RedisClientConfiguration
{
Servers = cacheConfiguration.Servers.Where(s => s.Type.ToLower().Equals("redis")).Select(s => (s.Address.IndexOf(".") > 0 && s.Address.IndexOf(":") > 0) || (s.Address.IndexOf(":") > 0 && s.Address.IndexOf("]:") > 0) ? ConfigurationHelper.ResolveToEndPoint(s.Address) as IPEndPoint : ConfigurationHelper.ResolveToEndPoint(s.Address, s.Port) as IPEndPoint).ToList(),
Options = cacheConfiguration.Options
};
/// <summary>
/// Gets the configuration for working with Memcached
/// </summary>
/// <param name="configSection"></param>
/// <param name="loggerFactory"></param>
/// <returns></returns>
public static MemcachedClientConfiguration GetMemcachedConfiguration(this CacheConfigurationSectionHandler configSection, ILoggerFactory loggerFactory = null)
=> new MemcachedClientConfiguration(loggerFactory, configSection);
/// <summary>
/// Gets the configuration for working with Memcached
/// </summary>
/// <param name="cacheConfiguration"></param>
/// <param name="loggerFactory"></param>
/// <returns></returns>
public static MemcachedClientConfiguration GetMemcachedConfiguration(this ICacheConfiguration cacheConfiguration, ILoggerFactory loggerFactory = null)
{
var configuration = new MemcachedClientConfiguration(loggerFactory)
{
Protocol = cacheConfiguration.Protocol
};
cacheConfiguration.Servers.Where(s => s.Type.ToLower().Equals("memcached"))
.ToList()
.ForEach(s => configuration.Servers.Add((s.Address.IndexOf(".") > 0 && s.Address.IndexOf(":") > 0) || (s.Address.IndexOf(":") > 0 && s.Address.IndexOf("]:") > 0) ? ConfigurationHelper.ResolveToEndPoint(s.Address) : ConfigurationHelper.ResolveToEndPoint(s.Address, s.Port)));
configuration.SocketPool.MaxPoolSize = cacheConfiguration.SocketPool.MaxPoolSize;
configuration.SocketPool.MinPoolSize = cacheConfiguration.SocketPool.MinPoolSize;
configuration.SocketPool.ConnectionTimeout = cacheConfiguration.SocketPool.ConnectionTimeout;
configuration.SocketPool.ReceiveTimeout = cacheConfiguration.SocketPool.ReceiveTimeout;
configuration.SocketPool.QueueTimeout = cacheConfiguration.SocketPool.QueueTimeout;
configuration.SocketPool.DeadTimeout = cacheConfiguration.SocketPool.DeadTimeout;
configuration.SocketPool.FailurePolicyFactory = cacheConfiguration.SocketPool.FailurePolicyFactory;
configuration.Authentication.Type = cacheConfiguration.Authentication.Type;
foreach (var kvp in cacheConfiguration.Authentication.Parameters)
configuration.Authentication.Parameters[kvp.Key] = kvp.Value;
if (!string.IsNullOrWhiteSpace(cacheConfiguration.KeyTransformer))
configuration.KeyTransformer = Enyim.Caching.FastActivator.Create(cacheConfiguration.KeyTransformer) as IKeyTransformer;
if (!string.IsNullOrWhiteSpace(cacheConfiguration.Transcoder))
configuration.Transcoder = Enyim.Caching.FastActivator.Create(cacheConfiguration.Transcoder) as ITranscoder;
if (!string.IsNullOrWhiteSpace(cacheConfiguration.NodeLocator))
configuration.NodeLocator = Type.GetType(cacheConfiguration.NodeLocator);
return configuration;
}
#endregion
}
/// <summary>
/// Presents in-process memory cache
/// </summary>
public class MemoryCache : IDisposable
{
internal readonly List<Microsoft.Extensions.Caching.Memory.MemoryCache> _shards = new List<Microsoft.Extensions.Caching.Memory.MemoryCache>(16);
internal readonly Action<string> _onUpdateCallback;
internal readonly Action<string> _onRemoveCallback;
internal readonly Func<string, string> _getKey;
internal readonly byte _maxSize;
/// <summary>
/// Gets the collection of keys
/// </summary>
public IEnumerable<string> Keys
=> this._shards.Select(shard => shard.Keys).SelectMany(key => key).Select(key => key as string);
/// <summary>
/// Creates new an instance
/// </summary>
/// <param name="onUpdateCallback">The action to callback when an item was updated</param>
/// <param name="onRemoveCallback">The action to callback when an item was removed</param>
/// <param name="getKey">The function to get 'real-key' in the distributed cache</param>
/// <param name="maxSize">Max memory size (giga-bytes)</param>
/// <param name="loggerFactory">The logger factory for working with logs</param>
public MemoryCache(Action<string> onUpdateCallback = null, Action<string> onRemoveCallback = null, Func<string, string> getKey = null, byte maxSize = 0, ILoggerFactory loggerFactory = null)
{
this._onUpdateCallback = onUpdateCallback;
this._onRemoveCallback = onRemoveCallback;
this._getKey = getKey;
this._maxSize = maxSize > 0 ? maxSize : (byte)0;
long maxCacheSize = this._maxSize > 0 ? this._maxSize * 1024 * 1024 * 1024 : 0;
for (var index = 0; index < 16; index++)
this._shards.Add(new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptions { SizeLimit = maxCacheSize > 0 ? maxCacheSize / 16 : (long?)null, ExpirationScanFrequency = TimeSpan.FromMinutes(5) }, loggerFactory));
}
Microsoft.Extensions.Caching.Memory.MemoryCache GetShard(string key)
=> this._shards[(key.GetHashCode() & 0x7fffffff) % this._shards.Count];
bool Set<T>(string key, T value, TimeSpan validFor)
{
var options = new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = validFor + TimeSpan.FromSeconds(Helper.Random.Next(0, 30))
};
if (this._maxSize > 0 && Cache.Sizes.TryRemove(this._getKey?.Invoke(key) ?? key, out var size))
options.SetSize(size);
return this.GetShard(key).Set(key, value, options) != null;
}
/// <summary>
/// Sets a cache item
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="validFor"></param>
/// <param name="fireCallbackHandler"></param>
/// <returns></returns>
public bool Set<T>(string key, T value, TimeSpan validFor, bool fireCallbackHandler)
{
if (!string.IsNullOrWhiteSpace(key) && value != null && this.Set(key, value, validFor))
{
if (fireCallbackHandler)
this._onUpdateCallback?.Invoke(key);
return true;
}
return false;
}
/// <summary>
/// Sets a cache item
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expiresAt"></param>
/// <param name="fireCallbackHandler"></param>
/// <returns></returns>
public bool Set<T>(string key, T value, DateTime expiresAt, bool fireCallbackHandler = true)
=> this.Set(key, value, expiresAt.ToTimeSpan(), fireCallbackHandler);
/// <summary>
/// Sets a collection of cache items
/// </summary>
/// <param name="items"></param>
/// <param name="keyPrefix"></param>
/// <param name="expiresAt"></param>
/// <param name="fireCallbackHandler"></param>
/// <returns></returns>
public bool Set<T>(IDictionary<string, T> items, string keyPrefix, DateTime expiresAt, bool fireCallbackHandler = true)
{
var dictionary = items?.Where(kvp => kvp.Key != null).ToDictionary(kvp => (string.IsNullOrWhiteSpace(keyPrefix) ? "" : keyPrefix) + kvp.Key, kvp => kvp.Value) ?? new Dictionary<string, T>();
foreach (var kvp in dictionary)
this.Set(kvp.Key, kvp.Value, expiresAt, fireCallbackHandler);
return items != null && items.Any();
}
/// <summary>
/// Gets a cache item
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object Get(string key)
=> !string.IsNullOrWhiteSpace(key) && this.GetShard(key).TryGetValue(key, out var value) ? value : null;
/// <summary>
/// Gets a cache item
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T Get<T>(string key)
=> !string.IsNullOrWhiteSpace(key) && this.GetShard(key).TryGetValue(key, out var value) && value is T tvalue ? tvalue : default;
/// <summary>
/// Gets a collection of cache items
/// </summary>
/// <param name="keys"></param>
/// <returns></returns>
public IDictionary<string, object> Get(IEnumerable<string> keys)
{
var dictKeys = keys?.ToList();
var dictionary = dictKeys?.Select(key => new KeyValuePair<string, object>(key, this.Get(key))).Where(kvp => kvp.Key != null && kvp.Value != null).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
return dictionary != null && dictKeys != null && dictionary.Count > 0 && dictionary.Count == dictKeys.Count ? dictionary : null;
}
/// <summary>
/// Gets a collection of cache items
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="keys"></param>
/// <returns></returns>
public IDictionary<string, T> Get<T>(IEnumerable<string> keys)
=> this.Get(keys)?.ToDictionary(kvp => kvp.Key, kvp => kvp.Value is T tvalue ? tvalue : default);
/// <summary>
/// Removes a cache item
/// </summary>
/// <param name="key"></param>
/// <param name="fireCallbackHandler"></param>
/// <returns></returns>
public bool Remove(string key, bool fireCallbackHandler = true)
{
if (!string.IsNullOrWhiteSpace(key))
{
this.GetShard(key).Remove(key);
if (fireCallbackHandler)
this._onRemoveCallback?.Invoke(key);
return true;
}
return false;
}
/// <summary>
/// Removes a collection of cache items
/// </summary>
/// <param name="keys"></param>
/// <param name="keyPrefix"></param>
/// <param name="fireCallbackHandler"></param>
/// <returns></returns>
public bool Remove(IEnumerable<string> keys, string keyPrefix, bool fireCallbackHandler = true)
=> keys != null && !keys.Where(key => !string.IsNullOrWhiteSpace(key)).Select(key => (string.IsNullOrWhiteSpace(keyPrefix) ? "" : keyPrefix) + key).ToList().Select(key => this.Remove(key, fireCallbackHandler)).Any(value => value == false);
/// <summary>
/// Checks existing of a cache item
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Exists(string key)
=> !string.IsNullOrWhiteSpace(key) && this.GetShard(key).TryGetValue(key, out var _);
/// <summary>
/// Clears the cache bag
/// </summary>
public void Clear()
=> this._shards.ForEach(shard => shard.Clear());
public void Dispose()
=> this._shards.ForEach(shard => shard.Dispose());
}
}
namespace Microsoft.Extensions.DependencyInjection
{
public static partial class CachingServiceCollectionExtensions
{
/// <summary>
/// Adds the caching service into the collection of services for using with dependency injection
/// </summary>
/// <param name="services"></param>
/// <param name="setupAction">The action to bind options of 'Cache' section from appsettings.json file</param>
/// <param name="addInstanceOfIDistributedCache">true to add the cache service as an instance of IDistributedCache</param>
/// <returns></returns>
public static IServiceCollection AddCache(this IServiceCollection services, Action<CacheOptions> setupAction, bool addInstanceOfIDistributedCache = true)
{
if (setupAction == null)
throw new ArgumentNullException(nameof(setupAction));
services.AddOptions().Configure(setupAction);
services.Add(ServiceDescriptor.Singleton<ICacheConfiguration, CacheConfiguration>());
services.Add(ServiceDescriptor.Singleton<ICache, Cache>(Cache.GetInstance));
if (addInstanceOfIDistributedCache)
services.Add(ServiceDescriptor.Singleton<IDistributedCache, Cache>(Cache.GetInstance));
return services;
}
}
}
namespace Microsoft.AspNetCore.Builder
{
public static partial class CachingApplicationBuilderExtensions
{
/// <summary>
/// Calls to use the caching service
/// </summary>
/// <param name="appBuilder"></param>
/// <returns></returns>
public static IApplicationBuilder UseCache(this IApplicationBuilder appBuilder)
{
var logger = appBuilder.ApplicationServices.GetService<ILogger<ICache>>();
try
{
var cache = appBuilder.ApplicationServices.GetService<ICache>() as Cache;
logger.LogInformation($"The caching service was {(cache != null ? "" : "not ")}registered with application service providers{(cache != null ? $" - {cache.Provider}: {cache.Name} ({cache.ExpirationTime} minutes) - L1-Cache: {cache.UseL1Cache}/{cache.PrefetchL1Cache}" : "")}");
}
catch (Exception ex)
{
logger.LogError(ex, $"Error occurred while collecting information of caching service => {ex.Message}");
}
return appBuilder;
}
}
}