This is an automated email from the ASF dual-hosted git repository. mmartell pushed a commit to branch AddNonSteeltoeSample in repository https://gitbox.apache.org/repos/asf/geode-dotnet-core-client.git
commit ff7e02e9a58767bcf24a43b77f9c1a873f6c6c82 Author: Mike Martell <mmart...@pivotal.io> AuthorDate: Tue Jul 27 16:52:33 2021 -0700 GEODE-9359: Add netcore-session to geode-native --- .../NetCore.Session.IntegrationTests.csproj | 0 .../Properties/launchSettings.json | 0 .../SessionStateIntegrationTests.cs | 10 ++--- .../NetCore.Session.Tests.csproj | 0 .../SessionStateCacheTests.cs | 0 .../GeodeCacheServiceCollectionExtensions.cs | 28 +++++++++++++ NetCore.Session/GeodeSessionStateCacheOptions.cs | 16 ++++++++ NetCore.Session/NetCoreSessionState.cs | 46 +++++++++++++--------- geode-dotnet-core.sln | 46 +++++++++++++++++----- 9 files changed, 113 insertions(+), 33 deletions(-) diff --git a/Session.IntegrationTests/NetCore.Session.IntegrationTests.csproj b/NetCore.Session.IntegrationTests/NetCore.Session.IntegrationTests.csproj similarity index 100% rename from Session.IntegrationTests/NetCore.Session.IntegrationTests.csproj rename to NetCore.Session.IntegrationTests/NetCore.Session.IntegrationTests.csproj diff --git a/Session.IntegrationTests/Properties/launchSettings.json b/NetCore.Session.IntegrationTests/Properties/launchSettings.json similarity index 100% rename from Session.IntegrationTests/Properties/launchSettings.json rename to NetCore.Session.IntegrationTests/Properties/launchSettings.json diff --git a/Session.IntegrationTests/SessionStateIntegrationTests.cs b/NetCore.Session.IntegrationTests/SessionStateIntegrationTests.cs similarity index 94% rename from Session.IntegrationTests/SessionStateIntegrationTests.cs rename to NetCore.Session.IntegrationTests/SessionStateIntegrationTests.cs index 52538df..eafb590 100644 --- a/Session.IntegrationTests/SessionStateIntegrationTests.cs +++ b/NetCore.Session.IntegrationTests/SessionStateIntegrationTests.cs @@ -24,7 +24,7 @@ namespace Apache.Geode.Session.IntegrationTests using var poolFactory = cache.PoolFactory.AddLocator("localhost", 10334); using var pool = poolFactory.CreatePool("myPool"); - using var ssCache = new SessionStateCache(cache, _regionName); + using var ssCache = new GeodeSessionStateCache(cache, _regionName); var options = new DistributedCacheEntryOptions(); DateTime localTime = DateTime.Now.AddDays(1); @@ -48,7 +48,7 @@ namespace Apache.Geode.Session.IntegrationTests using PoolFactory poolFactory = cache.PoolFactory.AddLocator("localhost", 10334); using var pool = poolFactory.CreatePool("myPool"); - using var ssCache = new SessionStateCache(cache, _regionName); + using var ssCache = new GeodeSessionStateCache(cache, _regionName); var options = new DistributedCacheEntryOptions(); int numSeconds = 20; @@ -81,7 +81,7 @@ namespace Apache.Geode.Session.IntegrationTests using var cache = (Cache)cacheFactory.CreateCache(); PoolFactory poolFactory = cache.PoolFactory; - using var ssCache = new SessionStateCache(cache, _regionName); + using var ssCache = new GeodeSessionStateCache(cache, _regionName); var options = new DistributedCacheEntryOptions(); options.AbsoluteExpiration = DateTime.Now.AddSeconds(5); @@ -102,7 +102,7 @@ namespace Apache.Geode.Session.IntegrationTests using var poolFactory = cache.PoolFactory.AddLocator("localhost", 10334); using var pool = poolFactory.CreatePool("myPool"); - using var ssCache = new SessionStateCache(cache, _regionName); + using var ssCache = new GeodeSessionStateCache(cache, _regionName); var options = new DistributedCacheEntryOptions(); DateTime localTime = DateTime.Now.AddDays(1); @@ -129,7 +129,7 @@ namespace Apache.Geode.Session.IntegrationTests using var poolFactory = cache.PoolFactory.AddLocator("localhost", 10334); using var pool = poolFactory.CreatePool("myPool"); - using var ssCache = new SessionStateCache(cache, _regionName); + using var ssCache = new GeodeSessionStateCache(cache, _regionName); var options = new DistributedCacheEntryOptions(); DateTime localTime = DateTime.Now.AddDays(1); diff --git a/Session.Tests/NetCore.Session.Tests.csproj b/NetCore.Session.Tests/NetCore.Session.Tests.csproj similarity index 100% rename from Session.Tests/NetCore.Session.Tests.csproj rename to NetCore.Session.Tests/NetCore.Session.Tests.csproj diff --git a/Session.Tests/SessionStateCacheTests.cs b/NetCore.Session.Tests/SessionStateCacheTests.cs similarity index 100% rename from Session.Tests/SessionStateCacheTests.cs rename to NetCore.Session.Tests/SessionStateCacheTests.cs diff --git a/NetCore.Session/GeodeCacheServiceCollectionExtensions.cs b/NetCore.Session/GeodeCacheServiceCollectionExtensions.cs new file mode 100644 index 0000000..19f0f54 --- /dev/null +++ b/NetCore.Session/GeodeCacheServiceCollectionExtensions.cs @@ -0,0 +1,28 @@ +using System; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.DependencyInjection; + +namespace Apache.Geode.Session +{ + public static class GeodeCacheServiceCollectionExtensions + { + public static IServiceCollection AddGeodeSessionStateCache(this IServiceCollection services, Action<GeodeSessionStateCacheOptions> setupAction) + { + if (services == null) + { + throw new ArgumentNullException(nameof(services)); + } + + if (setupAction == null) + { + throw new ArgumentNullException(nameof(setupAction)); + } + + services.AddOptions(); + services.Add(ServiceDescriptor.Singleton<IDistributedCache, GeodeSessionStateCache>()); + services.Configure(setupAction); + + return services; + } + } +} diff --git a/NetCore.Session/GeodeSessionStateCacheOptions.cs b/NetCore.Session/GeodeSessionStateCacheOptions.cs new file mode 100644 index 0000000..aecf7e6 --- /dev/null +++ b/NetCore.Session/GeodeSessionStateCacheOptions.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.Options; + +namespace Apache.Geode.Session +{ + public class GeodeSessionStateCacheOptions : IOptions<GeodeSessionStateCacheOptions> + { + public string RegionName { get; set; } + public string Host { get; set; } + public int Port { get; set; } + + GeodeSessionStateCacheOptions IOptions<GeodeSessionStateCacheOptions>.Value + { + get { return this; } + } + } +} diff --git a/NetCore.Session/NetCoreSessionState.cs b/NetCore.Session/NetCoreSessionState.cs index 0f39ae0..df4f8c8 100644 --- a/NetCore.Session/NetCoreSessionState.cs +++ b/NetCore.Session/NetCoreSessionState.cs @@ -1,21 +1,22 @@ using Apache.Geode.NetCore; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using System; using System.Threading; using System.Threading.Tasks; namespace Apache.Geode.Session { - public class SessionStateValue + public class GeodeSessionStateValue { DateTime _lastAccessTimeUtc; DateTime _expirationTimeUtc = DateTime.MinValue; TimeSpan _spanUntilStale = TimeSpan.Zero; private byte[] _value; - public SessionStateValue() { } - public SessionStateValue(byte[] value) + public GeodeSessionStateValue() { } + public GeodeSessionStateValue(byte[] value) { FromByteArray(value); } @@ -88,32 +89,41 @@ namespace Apache.Geode.Session } } - public class SessionStateCache : GeodeNativeObject, IDistributedCache + public class GeodeSessionStateCache : GeodeNativeObject, IDistributedCache { - private readonly Cache _cache; - private ILogger<SessionStateCache> _logger; + private readonly IGeodeCache _cache; + private ILogger<GeodeSessionStateCache> _logger; private static Region _region; private string _regionName; private readonly SemaphoreSlim _connectLock = new SemaphoreSlim(initialCount: 1, maxCount: 1); - public SessionStateCache(Cache cache, string regionName, ILogger<SessionStateCache> logger = null) - { - _regionName = regionName ?? throw new ArgumentNullException(regionName); - _cache = cache ?? throw new ArgumentNullException(nameof(cache)); - _logger = logger; + public GeodeSessionStateCache(IOptions<GeodeSessionStateCacheOptions> optionsAccessor) { + + var host = optionsAccessor.Value.Host; + var port = optionsAccessor.Value.Port; + _regionName = optionsAccessor.Value.RegionName; + + _cache = CacheFactory.Create() + .SetProperty("log-level", "none") + .CreateCache(); + + _cache.PoolManager + .CreatePoolFactory() + .AddLocator(host, port) + .CreatePool("pool"); - _cache.PoolFactory.AddLocator("localhost", 10334); - using var pool = _cache.PoolFactory.CreatePool("pool"); + var regionFactory = _cache.CreateRegionFactory(RegionShortcut.Proxy); + _region = regionFactory.CreateRegion(_regionName); } // Returns the SessionStateValue for key, or null if key doesn't exist - public SessionStateValue GetValueForKey(string key) + public GeodeSessionStateValue GetValueForKey(string key) { byte[] cacheValue = _region.GetByteArray(key); if (cacheValue != null) { - return new SessionStateValue(cacheValue); + return new GeodeSessionStateValue(cacheValue); } else return null; @@ -129,7 +139,7 @@ namespace Apache.Geode.Session Connect(); // Check for nonexistent key - SessionStateValue ssValue = GetValueForKey(key); + GeodeSessionStateValue ssValue = GetValueForKey(key); if (ssValue == null) return null; @@ -178,7 +188,7 @@ namespace Apache.Geode.Session Connect(); // Check for nonexistent key - SessionStateValue ssValue = GetValueForKey(key); + GeodeSessionStateValue ssValue = GetValueForKey(key); if (ssValue == null) return; @@ -263,7 +273,7 @@ namespace Apache.Geode.Session Connect(); - SessionStateValue ssValue = new SessionStateValue(); + GeodeSessionStateValue ssValue = new GeodeSessionStateValue(); ssValue.Value = value; DateTime nowUtc = DateTime.UtcNow; diff --git a/geode-dotnet-core.sln b/geode-dotnet-core.sln index 2f1a398..0ede2ac 100644 --- a/geode-dotnet-core.sln +++ b/geode-dotnet-core.sln @@ -15,64 +15,90 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCore.Session.Tests", "Se EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCore.Session.IntegrationTests", "Session.IntegrationTests\NetCore.Session.IntegrationTests.csproj", "{742B4109-544E-492E-86AC-DC1E0FCCA596}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionSample", "SessionSample\SessionSample.csproj", "{6D0D1D93-917E-48EE-977C-16F65DB982BE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore GeodeSession Sample", "AspNetCore GeodeSession Sample\AspNetCore GeodeSession Sample.csproj", "{0EB901F7-293F-41A3-B1A3-A8C831CCAD85}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU Release|x64 = Release|x64 + Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Debug|Any CPU.Build.0 = Debug|Any CPU {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Debug|x64.ActiveCfg = Debug|x64 {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Debug|x64.Build.0 = Debug|x64 + {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Debug|x86.ActiveCfg = Debug|Any CPU + {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Debug|x86.Build.0 = Debug|Any CPU {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Release|Any CPU.ActiveCfg = Release|Any CPU {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Release|Any CPU.Build.0 = Release|Any CPU {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Release|x64.ActiveCfg = Release|Any CPU {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Release|x64.Build.0 = Release|Any CPU + {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Release|x86.ActiveCfg = Release|Any CPU + {09ABBCE7-B217-43F1-A51B-CC5BDCD8EE98}.Release|x86.Build.0 = Release|Any CPU {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Debug|Any CPU.Build.0 = Debug|Any CPU {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Debug|x64.ActiveCfg = Debug|x64 {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Debug|x64.Build.0 = Debug|x64 + {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Debug|x86.ActiveCfg = Debug|Any CPU + {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Debug|x86.Build.0 = Debug|Any CPU {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Release|Any CPU.ActiveCfg = Release|Any CPU {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Release|Any CPU.Build.0 = Release|Any CPU {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Release|x64.ActiveCfg = Release|Any CPU {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Release|x64.Build.0 = Release|Any CPU + {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Release|x86.ActiveCfg = Release|Any CPU + {501DEA7E-8985-42A8-8BC9-C073E1B6DFE0}.Release|x86.Build.0 = Release|Any CPU {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Debug|Any CPU.Build.0 = Debug|Any CPU {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Debug|x64.ActiveCfg = Debug|x64 {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Debug|x64.Build.0 = Debug|x64 + {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Debug|x86.ActiveCfg = Debug|Any CPU + {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Debug|x86.Build.0 = Debug|Any CPU {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Release|Any CPU.ActiveCfg = Release|Any CPU {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Release|Any CPU.Build.0 = Release|Any CPU {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Release|x64.ActiveCfg = Release|Any CPU {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Release|x64.Build.0 = Release|Any CPU + {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Release|x86.ActiveCfg = Release|Any CPU + {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590}.Release|x86.Build.0 = Release|Any CPU {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Debug|Any CPU.Build.0 = Debug|Any CPU {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Debug|x64.ActiveCfg = Debug|x64 {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Debug|x64.Build.0 = Debug|x64 + {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Debug|x86.ActiveCfg = Debug|Any CPU + {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Debug|x86.Build.0 = Debug|Any CPU {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Release|Any CPU.ActiveCfg = Release|Any CPU {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Release|Any CPU.Build.0 = Release|Any CPU {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Release|x64.ActiveCfg = Release|Any CPU {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Release|x64.Build.0 = Release|Any CPU + {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Release|x86.ActiveCfg = Release|Any CPU + {C107D019-3B4F-403D-9040-ECB6E86C44E9}.Release|x86.Build.0 = Release|Any CPU {742B4109-544E-492E-86AC-DC1E0FCCA596}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {742B4109-544E-492E-86AC-DC1E0FCCA596}.Debug|Any CPU.Build.0 = Debug|Any CPU {742B4109-544E-492E-86AC-DC1E0FCCA596}.Debug|x64.ActiveCfg = Debug|x64 {742B4109-544E-492E-86AC-DC1E0FCCA596}.Debug|x64.Build.0 = Debug|x64 + {742B4109-544E-492E-86AC-DC1E0FCCA596}.Debug|x86.ActiveCfg = Debug|Any CPU + {742B4109-544E-492E-86AC-DC1E0FCCA596}.Debug|x86.Build.0 = Debug|Any CPU {742B4109-544E-492E-86AC-DC1E0FCCA596}.Release|Any CPU.ActiveCfg = Release|Any CPU {742B4109-544E-492E-86AC-DC1E0FCCA596}.Release|Any CPU.Build.0 = Release|Any CPU {742B4109-544E-492E-86AC-DC1E0FCCA596}.Release|x64.ActiveCfg = Release|Any CPU {742B4109-544E-492E-86AC-DC1E0FCCA596}.Release|x64.Build.0 = Release|Any CPU - {6D0D1D93-917E-48EE-977C-16F65DB982BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6D0D1D93-917E-48EE-977C-16F65DB982BE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6D0D1D93-917E-48EE-977C-16F65DB982BE}.Debug|x64.ActiveCfg = Debug|x64 - {6D0D1D93-917E-48EE-977C-16F65DB982BE}.Debug|x64.Build.0 = Debug|x64 - {6D0D1D93-917E-48EE-977C-16F65DB982BE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6D0D1D93-917E-48EE-977C-16F65DB982BE}.Release|Any CPU.Build.0 = Release|Any CPU - {6D0D1D93-917E-48EE-977C-16F65DB982BE}.Release|x64.ActiveCfg = Release|x64 - {6D0D1D93-917E-48EE-977C-16F65DB982BE}.Release|x64.Build.0 = Release|x64 + {742B4109-544E-492E-86AC-DC1E0FCCA596}.Release|x86.ActiveCfg = Release|Any CPU + {742B4109-544E-492E-86AC-DC1E0FCCA596}.Release|x86.Build.0 = Release|Any CPU + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Debug|x64.ActiveCfg = Debug|x64 + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Debug|x64.Build.0 = Debug|x64 + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Debug|x86.ActiveCfg = Debug|x86 + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Debug|x86.Build.0 = Debug|x86 + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Release|Any CPU.Build.0 = Release|Any CPU + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Release|x64.ActiveCfg = Release|x64 + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Release|x64.Build.0 = Release|x64 + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Release|x86.ActiveCfg = Release|x86 + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85}.Release|x86.Build.0 = Release|x86 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -81,7 +107,7 @@ Global {CC1FEC64-9AD6-4A69-ADF2-038CF67A8590} = {38D87C7D-D5FE-43B8-80CC-1CE70167FB69} {C107D019-3B4F-403D-9040-ECB6E86C44E9} = {38D87C7D-D5FE-43B8-80CC-1CE70167FB69} {742B4109-544E-492E-86AC-DC1E0FCCA596} = {38D87C7D-D5FE-43B8-80CC-1CE70167FB69} - {6D0D1D93-917E-48EE-977C-16F65DB982BE} = {38D87C7D-D5FE-43B8-80CC-1CE70167FB69} + {0EB901F7-293F-41A3-B1A3-A8C831CCAD85} = {38D87C7D-D5FE-43B8-80CC-1CE70167FB69} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B30A49F0-1C96-4D6C-A222-0088B1D7FBBE}