From eb06ba691b9a51b830061b87d9a4352d945c0a87 Mon Sep 17 00:00:00 2001 From: Jason Webb Date: Wed, 31 Jul 2024 00:03:40 -0600 Subject: [PATCH] WIP --- Examples/Examples.sln | 7 +++++ .../CachingBuilderExtensions.cs | 29 +++++++++++++++---- Src/RCommon.Caching/ICachingService.cs | 12 -------- .../IDistributedCachingBuilder.cs | 14 +++++++++ ...ingBuilder.cs => IMemoryCachingBuilder.cs} | 2 +- Src/RCommon.Caching/RCommon.Caching.csproj | 1 + .../RCommon.DistibutedMemoryCache.csproj | 13 +++++++++ .../DistributedMemoryCacheBuilder.cs | 25 ++++++++++++++++ ...cs => IDistributedMemoryCachingBuilder.cs} | 5 ++-- ...stributedMemoryCachingBuilderExtensions.cs | 20 +++++++++++++ ...gBuilder.cs => IInMemoryCachingBuilder.cs} | 2 +- .../IInMemoryCachingBuilderExtensions.cs | 19 ++++++++++++ Src/RCommon.MemoryCache/MemoryCacheService.cs | 20 ------------- .../MemoryCachingBuilder.cs | 16 ++++++++-- .../IRedisCachingBuilder.cs | 2 +- .../IRedisCachingBuilderExtensions.cs | 9 +++++- Src/RCommon.RedisCache/RedisCacheService.cs | 17 ----------- Src/RCommon.RedisCache/RedisCachingBuilder.cs | 18 ++++++++++-- 18 files changed, 166 insertions(+), 65 deletions(-) delete mode 100644 Src/RCommon.Caching/ICachingService.cs create mode 100644 Src/RCommon.Caching/IDistributedCachingBuilder.cs rename Src/RCommon.Caching/{ICachingBuilder.cs => IMemoryCachingBuilder.cs} (85%) create mode 100644 Src/RCommon.DistibutedMemoryCache/RCommon.DistibutedMemoryCache.csproj create mode 100644 Src/RCommon.MemoryCache/DistributedMemoryCacheBuilder.cs rename Src/RCommon.MemoryCache/{IMemoryCachingBuilderExtensions.cs => IDistributedMemoryCachingBuilder.cs} (54%) create mode 100644 Src/RCommon.MemoryCache/IDistributedMemoryCachingBuilderExtensions.cs rename Src/RCommon.MemoryCache/{IMemoryCachingBuilder.cs => IInMemoryCachingBuilder.cs} (73%) create mode 100644 Src/RCommon.MemoryCache/IInMemoryCachingBuilderExtensions.cs delete mode 100644 Src/RCommon.MemoryCache/MemoryCacheService.cs delete mode 100644 Src/RCommon.RedisCache/RedisCacheService.cs diff --git a/Examples/Examples.sln b/Examples/Examples.sln index d76f21e5..d0bbba48 100644 --- a/Examples/Examples.sln +++ b/Examples/Examples.sln @@ -145,6 +145,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RCommon.SystemTextJson", ". EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RCommon.MemoryCache", "..\Src\RCommon.MemoryCache\RCommon.MemoryCache.csproj", "{F5277287-1776-494B-92EE-7237D1B8949B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RCommon.DistibutedMemoryCache", "..\Src\RCommon.DistibutedMemoryCache\RCommon.DistibutedMemoryCache.csproj", "{0BCA99B2-86A4-40DF-B42D-7AD9F8BC4C49}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -375,6 +377,10 @@ Global {F5277287-1776-494B-92EE-7237D1B8949B}.Debug|Any CPU.Build.0 = Debug|Any CPU {F5277287-1776-494B-92EE-7237D1B8949B}.Release|Any CPU.ActiveCfg = Release|Any CPU {F5277287-1776-494B-92EE-7237D1B8949B}.Release|Any CPU.Build.0 = Release|Any CPU + {0BCA99B2-86A4-40DF-B42D-7AD9F8BC4C49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0BCA99B2-86A4-40DF-B42D-7AD9F8BC4C49}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0BCA99B2-86A4-40DF-B42D-7AD9F8BC4C49}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0BCA99B2-86A4-40DF-B42D-7AD9F8BC4C49}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -435,6 +441,7 @@ Global {8F5C5AD3-8310-47A7-BB30-42C054D032E2} = {A53F8891-29D2-449D-B9BA-062D19EC7834} {6C07FC1A-4339-42AA-AF5F-83570F81A5A4} = {3199F749-0082-41D0-91D3-ECED117F8B08} {F5277287-1776-494B-92EE-7237D1B8949B} = {3199F749-0082-41D0-91D3-ECED117F8B08} + {0BCA99B2-86A4-40DF-B42D-7AD9F8BC4C49} = {3199F749-0082-41D0-91D3-ECED117F8B08} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0B0CD26D-8067-4667-863E-6B0EE7EDAA42} diff --git a/Src/RCommon.Caching/CachingBuilderExtensions.cs b/Src/RCommon.Caching/CachingBuilderExtensions.cs index 3360068a..4d37cc1a 100644 --- a/Src/RCommon.Caching/CachingBuilderExtensions.cs +++ b/Src/RCommon.Caching/CachingBuilderExtensions.cs @@ -1,4 +1,6 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using RCommon.Json; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,14 +10,29 @@ namespace RCommon.Caching { public static class CachingBuilderExtensions { - public static IRCommonBuilder WithCaching(this IRCommonBuilder builder) - where T : ICachingBuilder + public static IRCommonBuilder WithMemoryCaching(this IRCommonBuilder builder) + where T : IMemoryCachingBuilder { - return WithCaching(builder, x => { }); + return WithMemoryCaching(builder, x => { }); } - public static IRCommonBuilder WithCaching(this IRCommonBuilder builder, Action actions) - where T : ICachingBuilder + public static IRCommonBuilder WithMemoryCaching(this IRCommonBuilder builder, Action actions) + where T : IMemoryCachingBuilder + { + Guard.IsNotNull(actions, nameof(actions)); + var cachingConfig = (T)Activator.CreateInstance(typeof(T), new object[] { builder }); + actions(cachingConfig); + return builder; + } + + public static IRCommonBuilder WithDistributedCaching(this IRCommonBuilder builder) + where T : IDistributedCachingBuilder + { + return WithDistributedCaching(builder, x => { }); + } + + public static IRCommonBuilder WithDistributedCaching(this IRCommonBuilder builder, Action actions) + where T : IDistributedCachingBuilder { Guard.IsNotNull(actions, nameof(actions)); var cachingConfig = (T)Activator.CreateInstance(typeof(T), new object[] { builder }); diff --git a/Src/RCommon.Caching/ICachingService.cs b/Src/RCommon.Caching/ICachingService.cs deleted file mode 100644 index 6f7ee968..00000000 --- a/Src/RCommon.Caching/ICachingService.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.Caching -{ - public interface ICachingService - { - } -} diff --git a/Src/RCommon.Caching/IDistributedCachingBuilder.cs b/Src/RCommon.Caching/IDistributedCachingBuilder.cs new file mode 100644 index 00000000..ee5df102 --- /dev/null +++ b/Src/RCommon.Caching/IDistributedCachingBuilder.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCommon.Caching +{ + public interface IDistributedCachingBuilder + { + IServiceCollection Services { get; } + } +} diff --git a/Src/RCommon.Caching/ICachingBuilder.cs b/Src/RCommon.Caching/IMemoryCachingBuilder.cs similarity index 85% rename from Src/RCommon.Caching/ICachingBuilder.cs rename to Src/RCommon.Caching/IMemoryCachingBuilder.cs index fd0a0a2a..cacbefac 100644 --- a/Src/RCommon.Caching/ICachingBuilder.cs +++ b/Src/RCommon.Caching/IMemoryCachingBuilder.cs @@ -7,7 +7,7 @@ namespace RCommon.Caching { - public interface ICachingBuilder + public interface IMemoryCachingBuilder { IServiceCollection Services { get; } } diff --git a/Src/RCommon.Caching/RCommon.Caching.csproj b/Src/RCommon.Caching/RCommon.Caching.csproj index d7a528dd..f8cbcad8 100644 --- a/Src/RCommon.Caching/RCommon.Caching.csproj +++ b/Src/RCommon.Caching/RCommon.Caching.csproj @@ -12,6 +12,7 @@ + diff --git a/Src/RCommon.DistibutedMemoryCache/RCommon.DistibutedMemoryCache.csproj b/Src/RCommon.DistibutedMemoryCache/RCommon.DistibutedMemoryCache.csproj new file mode 100644 index 00000000..cce9b195 --- /dev/null +++ b/Src/RCommon.DistibutedMemoryCache/RCommon.DistibutedMemoryCache.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/Src/RCommon.MemoryCache/DistributedMemoryCacheBuilder.cs b/Src/RCommon.MemoryCache/DistributedMemoryCacheBuilder.cs new file mode 100644 index 00000000..d691c874 --- /dev/null +++ b/Src/RCommon.MemoryCache/DistributedMemoryCacheBuilder.cs @@ -0,0 +1,25 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCommon.MemoryCache +{ + public class DistributedMemoryCacheBuilder : IDistributedMemoryCachingBuilder + { + public DistributedMemoryCacheBuilder(IRCommonBuilder builder) + { + Services = builder.Services; + this.RegisterServices(Services); + } + + protected void RegisterServices(IServiceCollection services) + { + + } + + public IServiceCollection Services { get; } + } +} diff --git a/Src/RCommon.MemoryCache/IMemoryCachingBuilderExtensions.cs b/Src/RCommon.MemoryCache/IDistributedMemoryCachingBuilder.cs similarity index 54% rename from Src/RCommon.MemoryCache/IMemoryCachingBuilderExtensions.cs rename to Src/RCommon.MemoryCache/IDistributedMemoryCachingBuilder.cs index 35f88626..a500c5a1 100644 --- a/Src/RCommon.MemoryCache/IMemoryCachingBuilderExtensions.cs +++ b/Src/RCommon.MemoryCache/IDistributedMemoryCachingBuilder.cs @@ -1,4 +1,5 @@ -using System; +using RCommon.Caching; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +7,7 @@ namespace RCommon.MemoryCache { - public static class IMemoryCachingBuilderExtensions + public interface IDistributedMemoryCachingBuilder : IDistributedCachingBuilder { } } diff --git a/Src/RCommon.MemoryCache/IDistributedMemoryCachingBuilderExtensions.cs b/Src/RCommon.MemoryCache/IDistributedMemoryCachingBuilderExtensions.cs new file mode 100644 index 00000000..2e1ef588 --- /dev/null +++ b/Src/RCommon.MemoryCache/IDistributedMemoryCachingBuilderExtensions.cs @@ -0,0 +1,20 @@ +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; +using RCommon.Caching; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCommon.MemoryCache +{ + public static class IDistributedMemoryCachingBuilderExtensions + { + public static IDistributedMemoryCachingBuilder Configure(this IDistributedMemoryCachingBuilder builder, Action actions) + { + builder.Services.AddDistributedMemoryCache(actions); + return builder; + } + } +} diff --git a/Src/RCommon.MemoryCache/IMemoryCachingBuilder.cs b/Src/RCommon.MemoryCache/IInMemoryCachingBuilder.cs similarity index 73% rename from Src/RCommon.MemoryCache/IMemoryCachingBuilder.cs rename to Src/RCommon.MemoryCache/IInMemoryCachingBuilder.cs index d02361b4..50aea9fd 100644 --- a/Src/RCommon.MemoryCache/IMemoryCachingBuilder.cs +++ b/Src/RCommon.MemoryCache/IInMemoryCachingBuilder.cs @@ -7,7 +7,7 @@ namespace RCommon.MemoryCache { - public interface IMemoryCachingBuilder : ICachingBuilder + public interface IInMemoryCachingBuilder : IMemoryCachingBuilder { } } diff --git a/Src/RCommon.MemoryCache/IInMemoryCachingBuilderExtensions.cs b/Src/RCommon.MemoryCache/IInMemoryCachingBuilderExtensions.cs new file mode 100644 index 00000000..cfd3e3eb --- /dev/null +++ b/Src/RCommon.MemoryCache/IInMemoryCachingBuilderExtensions.cs @@ -0,0 +1,19 @@ +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCommon.MemoryCache +{ + public static class IInMemoryCachingBuilderExtensions + { + public static IInMemoryCachingBuilder Configure(this IInMemoryCachingBuilder builder, Action actions) + { + builder.Services.AddMemoryCache(actions); + return builder; + } + } +} diff --git a/Src/RCommon.MemoryCache/MemoryCacheService.cs b/Src/RCommon.MemoryCache/MemoryCacheService.cs deleted file mode 100644 index 67a6a786..00000000 --- a/Src/RCommon.MemoryCache/MemoryCacheService.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Microsoft.Extensions.Caching.Memory; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.MemoryCache -{ - public class MemoryCacheService - { - private readonly IMemoryCache _memoryCache; - - public MemoryCacheService(IMemoryCache memoryCache) - { - _memoryCache = memoryCache; - - } - } -} diff --git a/Src/RCommon.MemoryCache/MemoryCachingBuilder.cs b/Src/RCommon.MemoryCache/MemoryCachingBuilder.cs index 0853e8cb..66b285fa 100644 --- a/Src/RCommon.MemoryCache/MemoryCachingBuilder.cs +++ b/Src/RCommon.MemoryCache/MemoryCachingBuilder.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using RCommon.Caching; using System; using System.Collections.Generic; using System.Linq; @@ -7,8 +8,19 @@ namespace RCommon.MemoryCache { - public class MemoryCachingBuilder : IMemoryCachingBuilder + public class MemoryCachingBuilder : IInMemoryCachingBuilder { - public IServiceCollection Services => throw new NotImplementedException(); + public MemoryCachingBuilder(IRCommonBuilder builder) + { + Services = builder.Services; + this.RegisterServices(Services); + } + + protected void RegisterServices(IServiceCollection services) + { + + } + + public IServiceCollection Services { get; } } } diff --git a/Src/RCommon.RedisCache/IRedisCachingBuilder.cs b/Src/RCommon.RedisCache/IRedisCachingBuilder.cs index 55fafbcd..963e861a 100644 --- a/Src/RCommon.RedisCache/IRedisCachingBuilder.cs +++ b/Src/RCommon.RedisCache/IRedisCachingBuilder.cs @@ -7,7 +7,7 @@ namespace RCommon.RedisCache { - public interface IRedisCachingBuilder : ICachingBuilder + public interface IRedisCachingBuilder : IDistributedCachingBuilder { } } diff --git a/Src/RCommon.RedisCache/IRedisCachingBuilderExtensions.cs b/Src/RCommon.RedisCache/IRedisCachingBuilderExtensions.cs index a2b93dc2..63f1d223 100644 --- a/Src/RCommon.RedisCache/IRedisCachingBuilderExtensions.cs +++ b/Src/RCommon.RedisCache/IRedisCachingBuilderExtensions.cs @@ -1,4 +1,6 @@ -using System; +using Microsoft.Extensions.Caching.StackExchangeRedis; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,5 +10,10 @@ namespace RCommon.RedisCache { public static class IRedisCachingBuilderExtensions { + public static IRedisCachingBuilder Configure(this IRedisCachingBuilder builder, Action actions) + { + builder.Services.AddStackExchangeRedisCache(actions); + return builder; + } } } diff --git a/Src/RCommon.RedisCache/RedisCacheService.cs b/Src/RCommon.RedisCache/RedisCacheService.cs deleted file mode 100644 index f6976e36..00000000 --- a/Src/RCommon.RedisCache/RedisCacheService.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.Extensions.Caching.Distributed; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace RCommon.RedisCache -{ - public class RedisCacheService - { - public RedisCacheService(IDistributedCache cache) - { - - } - } -} diff --git a/Src/RCommon.RedisCache/RedisCachingBuilder.cs b/Src/RCommon.RedisCache/RedisCachingBuilder.cs index 39a31100..b3bb8af6 100644 --- a/Src/RCommon.RedisCache/RedisCachingBuilder.cs +++ b/Src/RCommon.RedisCache/RedisCachingBuilder.cs @@ -1,4 +1,6 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using RCommon.Caching; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +8,19 @@ namespace RCommon.RedisCache { - public static class RedisCachingBuilder + public class RedisCachingBuilder : IDistributedCachingBuilder { + public RedisCachingBuilder(IRCommonBuilder builder) + { + Services = builder.Services; + this.RegisterServices(Services); + } + + protected void RegisterServices(IServiceCollection services) + { + + } + + public IServiceCollection Services { get; } } }