From e25666bbf408ff33c09dc8e2c5fe2d052363595f Mon Sep 17 00:00:00 2001 From: vnugent Date: Wed, 29 Nov 2023 00:17:13 -0500 Subject: Integrate FBM core changes for immutable client instances --- .../src/DataModel/EntityCacheExtensions.cs | 77 +++++++++++++++++++++- 1 file changed, 75 insertions(+), 2 deletions(-) (limited to 'lib/VNLib.Plugins.Extensions.VNCache/src/DataModel') diff --git a/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/EntityCacheExtensions.cs b/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/EntityCacheExtensions.cs index bf2fa2a..562c220 100644 --- a/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/EntityCacheExtensions.cs +++ b/lib/VNLib.Plugins.Extensions.VNCache/src/DataModel/EntityCacheExtensions.cs @@ -107,15 +107,88 @@ namespace VNLib.Plugins.Extensions.VNCache.DataModel /// /// /// + /// The default serializer buffer size /// The new wrapper using json serialization /// - public static IEntityCache CreateJsonEntityCache(this IGlobalCacheProvider cache) where T: class + public static IEntityCache CreateJsonEntityCache(this IGlobalCacheProvider cache, int bufferSize) where T: class { _ = cache ?? throw new ArgumentNullException(nameof(cache)); - JsonCacheObjectSerializer json = new(); + JsonCacheObjectSerializer json = new(bufferSize); return CreateEntityCache(cache, json, json); } + /// + /// Attemts to recover an entity from cache if possible, if a miss occurs, the + /// factory function is called to produce a value from a backing store. If the store + /// returns a result it is writen back to the cache before this method returns + /// + /// + /// + /// The id of the entity to get or laod + /// The factory callback function to produce a value when a cache miss occurs + /// A token to cancel the operation + /// A task that completes by returning the entity + /// + public static async Task GetOrLoadAsync(this IEntityCache cache, string id, Func> factory, CancellationToken cancellation = default) where T : class + { + _ = cache ?? throw new ArgumentNullException(nameof(cache)); + _ = id ?? throw new ArgumentNullException(nameof(id)); + _ = factory ?? throw new ArgumentNullException(nameof(factory)); + + //try to load the value from cache + T? record = await cache.GetAsync(id, cancellation); + + //If record was not found in cache, load it from the factory + if (record is null) + { + record = await factory(id); + + //If new record found, write to cache + if (record is not null) + { + await cache.UpsertAsync(id, record, cancellation); + } + } + + return record; + } + + /// + /// Attemts to recover an entity from cache if possible, if a miss occurs, the + /// factory function is called to produce a value from a backing store. If the store + /// returns a result it is writen back to the cache before this method returns + /// + /// + /// + /// The id of the entity to get or laod + /// The factory callback function to produce a value when a cache miss occurs + /// A token to cancel the operation + /// A task that completes by returning the entity + /// + public static async Task GetOrLoadAsync(this IEntityCache cache, string id, Func> factory, CancellationToken cancellation = default) where T : class + { + _ = cache ?? throw new ArgumentNullException(nameof(cache)); + _ = id ?? throw new ArgumentNullException(nameof(id)); + _ = factory ?? throw new ArgumentNullException(nameof(factory)); + + //try to load the value from cache + T? record = await cache.GetAsync(id, cancellation); + + //If record was not found in cache, load it from the factory + if (record is null) + { + record = await factory(id, cancellation); + + //If new record found, write to cache + if(record is not null) + { + await cache.UpsertAsync(id, record, cancellation); + } + } + + return record; + } + private sealed class EntityCacheImpl : IEntityCache where T : class { private readonly IGlobalCacheProvider _cacheProvider; -- cgit