aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/VNLib.Data.Caching.Providers.Redis/src/RedisClientCacheEntry.cs23
-rw-r--r--plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs42
-rw-r--r--plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCache.cs58
-rw-r--r--plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCacheConfig.cs56
-rw-r--r--plugins/VNLib.Data.Caching.Providers.VNCache/src/RemoteBackedMemoryCache.cs38
-rw-r--r--plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheBase.cs66
-rw-r--r--plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheClient.cs14
-rw-r--r--plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs103
-rw-r--r--plugins/VNLib.Data.Caching.Providers.VNCache/src/VnCacheClientConfig.cs21
9 files changed, 230 insertions, 191 deletions
diff --git a/plugins/VNLib.Data.Caching.Providers.Redis/src/RedisClientCacheEntry.cs b/plugins/VNLib.Data.Caching.Providers.Redis/src/RedisClientCacheEntry.cs
index 30d936c..a0a94c9 100644
--- a/plugins/VNLib.Data.Caching.Providers.Redis/src/RedisClientCacheEntry.cs
+++ b/plugins/VNLib.Data.Caching.Providers.Redis/src/RedisClientCacheEntry.cs
@@ -58,8 +58,7 @@ namespace VNLib.Data.Caching.Providers.Redis
public sealed class RedisClientCacheEntry : IGlobalCacheProvider
{
private const int InitialWriterBufferSize = 4096;
-
- private readonly JsonCacheObjectSerializer _fallbackSerializer;
+
private readonly IUnmangedHeap _defaultHeap;
private readonly Task OnLoadTask;
@@ -69,8 +68,9 @@ namespace VNLib.Data.Caching.Providers.Redis
public RedisClientCacheEntry(PluginBase plugin, IConfigScope config)
{
- _fallbackSerializer = new();
_defaultHeap = MemoryUtil.Shared;
+ DefaultDeserializer = new JsonCacheObjectSerializer();
+ DefaultSerializer = new JsonCacheObjectSerializer();
ILogProvider redisLog = plugin.Log.CreateScope("REDIS");
@@ -203,7 +203,10 @@ namespace VNLib.Data.Caching.Providers.Redis
public Task InitAsync() => OnLoadTask;
///<inheritdoc/>
- public Task AddOrUpdateAsync<T>(string key, string? newKey, T value, CancellationToken cancellation) => AddOrUpdateAsync(key, newKey, value, _fallbackSerializer, cancellation);
+ public ICacheObjectDeserializer DefaultDeserializer { get; }
+
+ ///<inheritdoc/>
+ public ICacheObjectSerializer DefaultSerializer { get; }
///<inheritdoc/>
public async Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation)
@@ -256,13 +259,10 @@ namespace VNLib.Data.Caching.Providers.Redis
ReadOnlySpan<byte> data = callback(state);
length = data.Length;
- //Alloc the buffer on the desired heap
- MemoryManager<byte> buffer = heap.DirectAlloc<byte>(length, false);
-
- //Copy object data to the buffer
- data.CopyTo(buffer.GetSpan());
+ //Alloc the buffer on the desired heap and copy data into it
+ IMemoryHandle<byte> buffer = heap.AllocAndCopy(data);
- return buffer;
+ return buffer.ToMemoryManager(true);
}
}
@@ -274,9 +274,6 @@ namespace VNLib.Data.Caching.Providers.Redis
}
///<inheritdoc/>
- public Task<T?> GetAsync<T>(string key, CancellationToken cancellation) => GetAsync<T>(key, _fallbackSerializer, cancellation);
-
- ///<inheritdoc/>
public async Task<T?> GetAsync<T>(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation)
{
_ = deserializer ?? throw new ArgumentNullException(nameof(deserializer));
diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs
index f952bcb..7132212 100644
--- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs
+++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/FBMCacheClient.cs
@@ -51,7 +51,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
/// A base class that manages
/// </summary>
[ConfigurationName(VNCacheClient.CACHE_CONFIG_KEY)]
- internal class FBMCacheClient : IGlobalCacheProvider, IAsyncBackgroundWork
+ internal class FBMCacheClient : VNCacheBase, IAsyncBackgroundWork
{
private const string LOG_NAME = "CLIENT";
private static readonly TimeSpan InitialDelay = TimeSpan.FromSeconds(10);
@@ -60,6 +60,8 @@ namespace VNLib.Data.Caching.Providers.VNCache
private readonly VnCacheClientConfig _config;
private readonly ClusterNodeIndex _index;
+ private bool _isConnected;
+
/// <summary>
/// The internal client
/// </summary>
@@ -68,7 +70,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
/// <summary>
/// Gets a value that determines if the client is currently connected to a server
/// </summary>
- public bool IsConnected { get; private set; }
+ public override bool IsConnected => _isConnected;
public FBMCacheClient(PluginBase plugin, IConfigScope config)
: this(
@@ -95,7 +97,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
}
- public FBMCacheClient(VnCacheClientConfig config, ILogProvider? debugLog)
+ public FBMCacheClient(VnCacheClientConfig config, ILogProvider? debugLog):base(config)
{
//Validate config
(config as IOnConfigValidation).Validate();
@@ -103,7 +105,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
_config = config;
//Init the client with default settings
- FBMClientConfig conf = FBMDataCacheExtensions.GetDefaultConfig(MemoryUtil.Shared, config.MaxMessageSize!.Value, config.RequestTimeout, debugLog);
+ FBMClientConfig conf = FBMDataCacheExtensions.GetDefaultConfig(MemoryUtil.Shared, (int)config.MaxBlobSize, config.RequestTimeout, debugLog);
Client = new(conf);
@@ -183,7 +185,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
//Set connection status flag
- IsConnected = true;
+ _isConnected = true;
//Wait for disconnect
await Client.WaitForExitAsync(exitToken);
@@ -213,7 +215,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
finally
{
- IsConnected = false;
+ _isConnected = false;
}
//Loop again
@@ -241,15 +243,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
///<inheritdoc/>
- public virtual Task AddOrUpdateAsync<T>(string key, string? newKey, T value, CancellationToken cancellation)
- {
- return !IsConnected
- ? throw new InvalidOperationException("The underlying client is not connected to a cache node")
- : Client!.AddOrUpdateObjectAsync(key, newKey, value, cancellation);
- }
-
- ///<inheritdoc/>
- public virtual Task<bool> DeleteAsync(string key, CancellationToken cancellation)
+ public override Task<bool> DeleteAsync(string key, CancellationToken cancellation)
{
return !IsConnected
? throw new InvalidOperationException("The underlying client is not connected to a cache node")
@@ -257,15 +251,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public virtual Task<T?> GetAsync<T>(string key, CancellationToken cancellation)
- {
- return !IsConnected
- ? throw new InvalidOperationException("The underlying client is not connected to a cache node")
- : Client!.GetObjectAsync<T>(key, cancellation);
- }
-
- ///<inheritdoc/>
- public virtual Task<T?> GetAsync<T>(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation)
+ public override Task<T> GetAsync<T>(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation)
{
return !IsConnected
? throw new InvalidOperationException("The underlying client is not connected to a cache node")
@@ -273,7 +259,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public virtual Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation)
+ public override Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation)
{
return !IsConnected
? throw new InvalidOperationException("The underlying client is not connected to a cache node")
@@ -281,7 +267,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public virtual Task GetAsync<T>(string key, ObjectDataSet<T> callback, T state, CancellationToken cancellation)
+ public override Task GetAsync<T>(string key, ObjectDataSet<T> callback, T state, CancellationToken cancellation)
{
return !IsConnected
? throw new InvalidOperationException("The underlying client is not connected to a cache node")
@@ -289,7 +275,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public virtual Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataReader<T> callback, T state, CancellationToken cancellation)
+ public override Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataReader<T> callback, T state, CancellationToken cancellation)
{
return !IsConnected
? throw new InvalidOperationException("The underlying client is not connected to a cache node")
@@ -297,7 +283,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public object GetUnderlyingStore() => Client; //Client is the underlying "store"
+ public override object GetUnderlyingStore() => Client; //Client is the underlying "store"
private sealed class AuthManager : ICacheAuthManager
{
diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCache.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCache.cs
index 7d03918..79348f0 100644
--- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCache.cs
+++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCache.cs
@@ -26,8 +26,6 @@ using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
-
-using VNLib.Utils;
using VNLib.Utils.Memory;
using VNLib.Utils.Logging;
using VNLib.Utils.Memory.Diagnostics;
@@ -38,8 +36,9 @@ using VNLib.Plugins.Extensions.Loading;
namespace VNLib.Data.Caching.Providers.VNCache
{
+
[ConfigurationName(VNCacheClient.CACHE_CONFIG_KEY)]
- internal sealed class MemoryCache : VnDisposeable, IGlobalCacheProvider
+ internal sealed class MemoryCache : VNCacheBase, IDisposable
{
const int MB_DIVISOR = 1000 * 1024;
@@ -56,8 +55,6 @@ namespace VNLib.Data.Caching.Providers.VNCache
| -----------------------------
";
- private readonly ICacheObjectSerializer _serialzer;
- private readonly ICacheObjectDeserializer _deserialzer;
private readonly IBlobCacheTable _memCache;
private readonly IUnmangedHeap _bufferHeap;
private readonly BucketLocalManagerFactory? _blobCacheMemManager;
@@ -74,7 +71,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
public MemoryCache(MemoryCacheConfig config) : this(config, false, null, null)
{ }
- private MemoryCache(MemoryCacheConfig config, bool isDebug, ILogProvider? log, BucketLocalManagerFactory? factory)
+ private MemoryCache(MemoryCacheConfig config, bool isDebug, ILogProvider? log, BucketLocalManagerFactory? factory) : base(config)
{
//Validate config
config.Validate();
@@ -99,15 +96,6 @@ namespace VNLib.Data.Caching.Providers.VNCache
//Setup cache table
_memCache = new BlobCacheTable(config.TableSize, config.BucketSize, factory, null);
- /*
- * Default to json serialization by using the default
- * serializer and JSON options
- */
-
- JsonCacheObjectSerializer defaultSerializer = new();
- _serialzer = defaultSerializer;
- _deserialzer = defaultSerializer;
-
PrintDebug(log, config);
}
@@ -122,10 +110,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
log?.Debug(DEBUG_TEMPLATE, config.TableSize, config.BucketSize, maxObjects, size4kMb, size8kMb, size16kMb);
}
- ///<inheritdoc/>
- public bool IsConnected { get; } = true;
-
- protected override void Free()
+ public void Dispose()
{
_memCache.Dispose();
_bufferHeap.Dispose();
@@ -133,13 +118,14 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public Task AddOrUpdateAsync<T>(string key, string? newKey, T value, CancellationToken cancellation) => AddOrUpdateAsync(key, newKey, value, _serialzer, cancellation);
+ public override object GetUnderlyingStore() => _memCache;
///<inheritdoc/>
- public async Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation)
- {
- Check();
+ public override bool IsConnected { get; } = true;
+ ///<inheritdoc/>
+ public override async Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation)
+ {
//Alloc serialzation buffer
using AddOrUpdateBuffer buffer = new (_bufferHeap);
@@ -151,20 +137,14 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public Task<bool> DeleteAsync(string key, CancellationToken cancellation)
+ public override Task<bool> DeleteAsync(string key, CancellationToken cancellation)
{
- Check();
return _memCache.DeleteObjectAsync(key, cancellation).AsTask();
}
///<inheritdoc/>
- public Task<T?> GetAsync<T>(string key, CancellationToken cancellation) => GetAsync<T>(key, _deserialzer, cancellation);
-
- ///<inheritdoc/>
- public async Task<T?> GetAsync<T>(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation)
+ public override async Task<T> GetAsync<T>(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation)
{
- Check();
-
IBlobCacheBucket bucket = _memCache.GetBucket(key);
//Obtain lock
@@ -175,10 +155,10 @@ namespace VNLib.Data.Caching.Providers.VNCache
//Try to read the value
if (cache.TryGetValue(key, out CacheEntry entry))
{
- return deserializer.Deserialize<T>(entry.GetDataSegment());
+ return deserializer.Deserialize<T>(entry.GetDataSegment())!;
}
- return default;
+ return default!;
}
finally
{
@@ -187,10 +167,8 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public async Task GetAsync<T>(string key, ObjectDataSet<T> callback, T state, CancellationToken cancellation)
+ public override async Task GetAsync<T>(string key, ObjectDataSet<T> callback, T state, CancellationToken cancellation)
{
- Check();
-
//Get the bucket from the desired key
IBlobCacheBucket bucket = _memCache.GetBucket(key);
@@ -213,16 +191,10 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataReader<T> callback, T state, CancellationToken cancellation)
+ public override Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataReader<T> callback, T state, CancellationToken cancellation)
{
- Check();
-
//Update object data
return _memCache.AddOrUpdateObjectAsync(key, newKey, callback, state, default, cancellation).AsTask();
}
-
- ///<inheritdoc/>
- public object GetUnderlyingStore() => _memCache;
-
}
} \ No newline at end of file
diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCacheConfig.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCacheConfig.cs
index 176333f..5e53af7 100644
--- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCacheConfig.cs
+++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/MemoryCacheConfig.cs
@@ -25,14 +25,12 @@
using System;
using System.Text.Json.Serialization;
-using VNLib.Plugins.Extensions.Loading;
-
namespace VNLib.Data.Caching.Providers.VNCache
{
/// <summary>
- /// Memorycache configuration object. Json-(de)serializable
+ /// Memorycache configuration object
/// </summary>
- public sealed class MemoryCacheConfig : ICacheRefreshPolicy, IOnConfigValidation
+ public sealed class MemoryCacheConfig : VNCacheConfig
{
/// <summary>
/// The number of buckets within the cache table
@@ -46,50 +44,11 @@ namespace VNLib.Data.Caching.Providers.VNCache
[JsonPropertyName("bucket_size")]
public uint BucketSize { get; set; } = 5000;
- /// <summary>
- /// The maxium size (in bytes) of each cache entry within any bucket
- /// </summary>
- [JsonPropertyName("max_object_size")]
- public uint MaxBlobSize { get; set; } = 16 * 1024;
-
- [JsonIgnore]
- public TimeSpan MaxCacheAge { get; set; } = TimeSpan.FromMinutes(1);
-
- /// <summary>
- /// When refresh intervals are configured, The maxium cache entry age in seconds.
- /// </summary>
- [JsonPropertyName("max_age_sec")]
- public uint MaxAgeSeconds
- {
- get => (uint)MaxCacheAge.TotalSeconds;
- set => MaxCacheAge = TimeSpan.FromSeconds(value);
- }
- /*
- * Default disable cache
- */
- [JsonIgnore]
- public TimeSpan RefreshInterval { get; set; } = TimeSpan.Zero;
-
- /// <summary>
- /// The time (in seconds) a cache entry refresh interval will occur
- /// if scheduled on a plugin
- /// </summary>
- [JsonPropertyName("refresh_interval_sec")]
- public uint RefreshIntervalSeconds
- {
- get => (uint)RefreshInterval.TotalSeconds;
- set => RefreshInterval = TimeSpan.FromSeconds(value);
- }
-
- /// <summary>
- /// Zeros all cache entry memory allocations before they are used
- /// </summary>
- [JsonPropertyName("zero_all")]
- public bool ZeroAllAllocations { get; set; }
-
///<inheritdoc/>
- public void Validate()
+ public override void Validate()
{
+ base.Validate();
+
if (TableSize == 0)
{
throw new ArgumentException("You must specify a cache bucket table size", "buckets");
@@ -99,11 +58,6 @@ namespace VNLib.Data.Caching.Providers.VNCache
{
throw new ArgumentException("You must specify the maxium number of entires allowed in each bucket ", "bucket_size");
}
-
- if (MaxBlobSize < 16)
- {
- throw new ArgumentException("You must configure a maximum object size", "max_object_size");
- }
}
}
} \ No newline at end of file
diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/RemoteBackedMemoryCache.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/RemoteBackedMemoryCache.cs
index c14ddb9..2068805 100644
--- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/RemoteBackedMemoryCache.cs
+++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/RemoteBackedMemoryCache.cs
@@ -53,13 +53,10 @@ namespace VNLib.Data.Caching.Providers.VNCache
* lost or is exiting
*/
-
[ConfigurationName(VNCacheClient.CACHE_CONFIG_KEY)]
- internal sealed class RemoteBackedMemoryCache : IDisposable, IGlobalCacheProvider, IIntervalScheduleable
+ internal sealed class RemoteBackedMemoryCache : VNCacheBase, IDisposable, IIntervalScheduleable
{
private readonly MemoryCacheConfig _cacheConfig;
- private readonly ICacheObjectSerializer _fallbackSerializer;
- private readonly ICacheObjectDeserializer _fallbackDeserializer;
private readonly IBlobCacheTable _memCache;
private readonly IGlobalCacheProvider _backing;
private readonly IUnmangedHeap _bufferHeap;
@@ -84,7 +81,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
public RemoteBackedMemoryCache(MemoryCacheConfig memCache, IGlobalCacheProvider backingStore) : this(memCache, backingStore, null)
{ }
- public RemoteBackedMemoryCache(MemoryCacheConfig memCache, IGlobalCacheProvider backingStore, BucketLocalManagerFactory? factory)
+ public RemoteBackedMemoryCache(MemoryCacheConfig memCache, IGlobalCacheProvider backingStore, BucketLocalManagerFactory? factory):base(memCache)
{
_ = memCache ?? throw new ArgumentNullException(nameof(memCache));
_ = backingStore ?? throw new ArgumentNullException(nameof(backingStore));
@@ -105,18 +102,8 @@ namespace VNLib.Data.Caching.Providers.VNCache
//If backing store is a VnCacheClient, steal it's buffer heap
_bufferHeap = backingStore is FBMCacheClient client && client.Client.Config.MemoryManager.TryGetHeap(out IUnmangedHeap? heap) ? heap : MemoryUtil.Shared;
-
_cacheConfig = memCache;
_backing = backingStore;
-
- /*
- * Default to json serialization by using the default
- * serializer and JSON options
- */
-
- JsonCacheObjectSerializer defaultSerializer = new();
- _fallbackSerializer = defaultSerializer;
- _fallbackDeserializer = defaultSerializer;
}
void IDisposable.Dispose()
@@ -132,7 +119,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- object IGlobalCacheProvider.GetUnderlyingStore() => _backing.GetUnderlyingStore();
+ public override object GetUnderlyingStore() => _backing.GetUnderlyingStore();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CheckConnected()
@@ -144,14 +131,10 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public bool IsConnected => _backing.IsConnected;
-
- ///<inheritdoc/>
- public Task AddOrUpdateAsync<T>(string key, string? newKey, T value, CancellationToken cancellation)
- => AddOrUpdateAsync(key, newKey, value, _fallbackSerializer, cancellation);
+ public override bool IsConnected => _backing.IsConnected;
///<inheritdoc/>
- public Task<bool> DeleteAsync(string key, CancellationToken cancellation)
+ public override Task<bool> DeleteAsync(string key, CancellationToken cancellation)
{
CheckConnected();
@@ -164,10 +147,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public Task<T?> GetAsync<T>(string key, CancellationToken cancellation) => GetAsync<T>(key, _fallbackDeserializer, cancellation);
-
- ///<inheritdoc/>
- public async Task<T?> GetAsync<T>(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation)
+ public override async Task<T> GetAsync<T>(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation)
{
_ = deserializer ?? throw new ArgumentNullException(nameof(deserializer));
@@ -184,7 +164,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public async Task GetAsync<T>(string key, ObjectDataSet<T> setter, T state, CancellationToken cancellation)
+ public override async Task GetAsync<T>(string key, ObjectDataSet<T> setter, T state, CancellationToken cancellation)
{
_ = key ?? throw new ArgumentNullException(nameof(key));
_ = setter ?? throw new ArgumentNullException(nameof(setter));
@@ -226,7 +206,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public async Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation)
+ public override async Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation)
{
CheckConnected();
@@ -240,7 +220,7 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public async Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataReader<T> callback, T state, CancellationToken cancellation)
+ public override async Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataReader<T> callback, T state, CancellationToken cancellation)
{
CheckConnected();
diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheBase.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheBase.cs
new file mode 100644
index 0000000..fde2e44
--- /dev/null
+++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheBase.cs
@@ -0,0 +1,66 @@
+/*
+* Copyright (c) 2023 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Data.Caching.Providers.VNCache
+* File: VNCacheBase.cs
+*
+* VNCacheBase.cs is part of VNLib.Data.Caching.Providers.VNCache
+* which is part of the larger VNLib collection of libraries and utilities.
+*
+* VNLib.Data.Caching.Providers.VNCache is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Data.Caching.Providers.VNCache is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace VNLib.Data.Caching.Providers.VNCache
+{
+ internal abstract class VNCacheBase : IGlobalCacheProvider
+ {
+ ///<inheritdoc/>
+ public abstract bool IsConnected { get; }
+
+ ///<inheritdoc/>
+ public virtual ICacheObjectDeserializer DefaultDeserializer { get; }
+
+ ///<inheritdoc/>
+ public virtual ICacheObjectSerializer DefaultSerializer { get; }
+
+ protected VNCacheBase(VNCacheConfig config)
+ {
+ //Set default serializers
+ DefaultDeserializer = config.CacheObjectDeserializer ?? new JsonCacheObjectSerializer();
+ DefaultSerializer = config.CacheObjectSerializer ?? new JsonCacheObjectSerializer();
+ }
+
+ ///<inheritdoc/>
+ public abstract Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation);
+
+ ///<inheritdoc/>
+ public abstract Task AddOrUpdateAsync<T>(string key, string? newKey, ObjectDataReader<T> callback, T state, CancellationToken cancellation);
+
+ ///<inheritdoc/>
+ public abstract Task<bool> DeleteAsync(string key, CancellationToken cancellation);
+
+ ///<inheritdoc/>
+ public abstract Task<T?> GetAsync<T>(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation);
+
+ ///<inheritdoc/>
+ public abstract Task GetAsync<T>(string key, ObjectDataSet<T> callback, T state, CancellationToken cancellation);
+
+ ///<inheritdoc/>
+ public abstract object GetUnderlyingStore();
+ }
+} \ No newline at end of file
diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheClient.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheClient.cs
index a832b41..3d66c9c 100644
--- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheClient.cs
+++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheClient.cs
@@ -161,10 +161,10 @@ namespace VNLib.Data.Caching.Providers.VNCache
public bool IsConnected => _client.IsConnected;
///<inheritdoc/>
- public Task AddOrUpdateAsync<T>(string key, string? newKey, T value, CancellationToken cancellation)
- {
- return _client.AddOrUpdateAsync(key, newKey, value, cancellation);
- }
+ public ICacheObjectDeserializer DefaultDeserializer => _client.DefaultDeserializer;
+
+ ///<inheritdoc/>
+ public ICacheObjectSerializer DefaultSerializer => _client.DefaultSerializer;
///<inheritdoc/>
public Task AddOrUpdateAsync<T>(string key, string? newKey, T value, ICacheObjectSerializer serialzer, CancellationToken cancellation)
@@ -185,12 +185,6 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
///<inheritdoc/>
- public Task<T?> GetAsync<T>(string key, CancellationToken cancellation)
- {
- return _client.GetAsync<T>(key, cancellation);
- }
-
- ///<inheritdoc/>
public Task<T?> GetAsync<T>(string key, ICacheObjectDeserializer deserializer, CancellationToken cancellation)
{
return _client.GetAsync<T>(key, deserializer, cancellation);
diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs
new file mode 100644
index 0000000..8311519
--- /dev/null
+++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VNCacheConfig.cs
@@ -0,0 +1,103 @@
+/*
+* Copyright (c) 2023 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Data.Caching.Providers.VNCache
+* File: VNCacheConfig.cs
+*
+* VNCacheConfig.cs is part of VNLib.Data.Caching.Providers.VNCache
+* which is part of the larger VNLib collection of libraries and utilities.
+*
+* VNLib.Data.Caching.Providers.VNCache is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Data.Caching.Providers.VNCache is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Text.Json.Serialization;
+
+using VNLib.Plugins.Extensions.Loading;
+
+namespace VNLib.Data.Caching.Providers.VNCache
+{
+
+ /// <summary>
+ /// VNCache configuration for all vncache type providers
+ /// </summary>
+ public abstract class VNCacheConfig : ICacheRefreshPolicy, IOnConfigValidation
+ {
+ /*
+ * Default disable refreshing
+ */
+ ///<inheritdoc/>
+ [JsonIgnore]
+ public virtual TimeSpan RefreshInterval { get; set; } = TimeSpan.Zero;
+
+ /// <summary>
+ /// The time (in seconds) a cache entry refresh interval will occur
+ /// if scheduled on a plugin
+ /// </summary>
+ [JsonPropertyName("refresh_interval_sec")]
+ public uint RefreshIntervalSeconds
+ {
+ get => (uint)RefreshInterval.TotalSeconds;
+ set => RefreshInterval = TimeSpan.FromSeconds(value);
+ }
+
+ ///<inheritdoc/>
+ [JsonIgnore]
+ public virtual TimeSpan MaxCacheAge { get; set; } = TimeSpan.FromMinutes(1);
+
+ /// <summary>
+ /// When refresh intervals are configured, The maxium cache entry age in seconds.
+ /// </summary>
+ [JsonPropertyName("max_age_sec")]
+ public uint MaxAgeSeconds
+ {
+ get => (uint)MaxCacheAge.TotalSeconds;
+ set => MaxCacheAge = TimeSpan.FromSeconds(value);
+ }
+
+ /// <summary>
+ /// The cache object deserializer to use
+ /// </summary>
+ [JsonIgnore]
+ public ICacheObjectDeserializer? CacheObjectDeserializer { get; set; }
+
+ /// <summary>
+ /// The cache object serializer to use
+ /// </summary>
+ [JsonIgnore]
+ public ICacheObjectSerializer? CacheObjectSerializer { get; set; }
+
+ /// <summary>
+ /// Zeros all cache entry memory allocations before they are used
+ /// </summary>
+ [JsonPropertyName("zero_all")]
+ public bool ZeroAllAllocations { get; set; }
+
+ /// <summary>
+ /// The maxium size (in bytes) of each cache entry within any bucket
+ /// </summary>
+ [JsonPropertyName("max_object_size")]
+ public virtual uint MaxBlobSize { get; set; } = 16 * 1024;
+
+ public virtual void Validate()
+ {
+ if (MaxBlobSize < 16)
+ {
+ throw new ArgumentException("You must configure a maximum object size", "max_object_size");
+ }
+ }
+
+ }
+} \ No newline at end of file
diff --git a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VnCacheClientConfig.cs b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VnCacheClientConfig.cs
index f84fe55..9a21c79 100644
--- a/plugins/VNLib.Data.Caching.Providers.VNCache/src/VnCacheClientConfig.cs
+++ b/plugins/VNLib.Data.Caching.Providers.VNCache/src/VnCacheClientConfig.cs
@@ -26,24 +26,14 @@ using System;
using System.Linq;
using System.Text.Json.Serialization;
-using VNLib.Plugins.Extensions.Loading;
-
namespace VNLib.Data.Caching.Providers.VNCache
{
/// <summary>
/// Represents a remote VNCache client configuration
/// </summary>
- public class VnCacheClientConfig : IOnConfigValidation
+ public class VnCacheClientConfig : VNCacheConfig
{
/// <summary>
- /// The maximum size (in bytes) of messages sent to the
- /// cache server. This value will be negotiated with the server
- /// during a connection upgrade
- /// </summary>
- [JsonPropertyName("max_object_size")]
- public int? MaxMessageSize { get; set; }
-
- /// <summary>
/// The broker server address
/// </summary>
[JsonPropertyName("use_tls")]
@@ -95,12 +85,10 @@ namespace VNLib.Data.Caching.Providers.VNCache
return InitialNodes.Select(static x => new Uri(x, UriKind.Absolute)).ToArray();
}
- void IOnConfigValidation.Validate()
+ ///<inheritdoc/>
+ public override void Validate()
{
- if (!MaxMessageSize.HasValue || MaxMessageSize.Value < 1)
- {
- throw new ArgumentException("Your maxium message size should be a reasonable value greater than 0", "max_message_size");
- }
+ base.Validate();
if (!DiscoveryIntervalSeconds.HasValue || DiscoveryIntervalSeconds.Value < 1)
{
@@ -134,6 +122,5 @@ namespace VNLib.Data.Caching.Providers.VNCache
}
}
}
-
}
} \ No newline at end of file