Skip to content

Commit

Permalink
Merge pull request #5 from Nebula-Software-Systems/add_memcached
Browse files Browse the repository at this point in the history
Add memcached
  • Loading branch information
RafaelJCamara authored Sep 15, 2023
2 parents 1f3bb62 + 71f7abf commit 699716a
Show file tree
Hide file tree
Showing 37 changed files with 448 additions and 525 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
- name: Build
run: dotnet build src/Nebula.Caching.Redis/Nebula.Caching.Redis.csproj --no-restore

tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Test
run: dotnet test tests/ --no-build --verbosity normal
# tests:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v3
# - name: Test
# run: dotnet test tests/ --no-build --verbosity normal

7 changes: 7 additions & 0 deletions Nebula.Caching.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nebula.Caching.Redis", "src
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nebula.Caching.InMemory", "src\Nebula.Caching.InMemory\Nebula.Caching.InMemory.csproj", "{D77EA6CF-7825-458A-B32E-D7481644627D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nebula.Caching.Memcached", "src\Nebula.Caching.Memcached\Nebula.Caching.Memcached.csproj", "{87A9C41A-B5F3-41FB-A5B0-B8A49BA7A2AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -32,10 +34,15 @@ Global
{D77EA6CF-7825-458A-B32E-D7481644627D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D77EA6CF-7825-458A-B32E-D7481644627D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D77EA6CF-7825-458A-B32E-D7481644627D}.Release|Any CPU.Build.0 = Release|Any CPU
{87A9C41A-B5F3-41FB-A5B0-B8A49BA7A2AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{87A9C41A-B5F3-41FB-A5B0-B8A49BA7A2AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87A9C41A-B5F3-41FB-A5B0-B8A49BA7A2AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87A9C41A-B5F3-41FB-A5B0-B8A49BA7A2AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{DDB9C8EA-96AA-4EAE-AF56-7B4897CA4EE7} = {F0265230-4219-46CB-8FCC-A946BC6447CA}
{847F1E55-A4CF-4633-A322-9B08DADE7B30} = {F0265230-4219-46CB-8FCC-A946BC6447CA}
{D77EA6CF-7825-458A-B32E-D7481644627D} = {F0265230-4219-46CB-8FCC-A946BC6447CA}
{87A9C41A-B5F3-41FB-A5B0-B8A49BA7A2AE} = {F0265230-4219-46CB-8FCC-A946BC6447CA}
EndGlobalSection
EndGlobal
6 changes: 0 additions & 6 deletions src/Nebula.Caching.Common/Attributes/BaseAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Nebula.Caching.Common.Constants;

namespace Nebula.Caching.Common.Attributes
{
[AttributeUsage(AttributeTargets.Method, Inherited = true)]
[ExcludeFromCodeCoverage]
public class BaseAttribute : Attribute
{
public int CacheDurationInSeconds { get; set; } = CacheDurationConstants.DefaultCacheDurationInSeconds;
Expand Down
7 changes: 0 additions & 7 deletions src/Nebula.Caching.Common/Constants/CacheDurationConstants.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;

namespace Nebula.Caching.Common.Constants
{
[ExcludeFromCodeCoverage]
public static class CacheDurationConstants
{
public static int DefaultCacheDurationInSeconds = 600;
Expand Down
7 changes: 0 additions & 7 deletions src/Nebula.Caching.Common/Constants/KeyConstants.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;

namespace Nebula.Caching.Common.Constants
{
[ExcludeFromCodeCoverage]
public class KeyConstants
{
public const char MethodFullPathSeparator = '.';
Expand Down
2 changes: 0 additions & 2 deletions src/Nebula.Caching.Common/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using AspectCore.Extensions.Hosting;
using Microsoft.Extensions.Hosting;

namespace Nebula.Caching.Common.Extensions
{
[ExcludeFromCodeCoverage]
public static class Extensions
{
public static IHostBuilder UseNebulaCaching(this IHostBuilder builderHost)
Expand Down
7 changes: 2 additions & 5 deletions src/Nebula.Caching.Common/Settings/BaseOptions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;

namespace Common.Settings
{
[ExcludeFromCodeCoverage]
public abstract class BaseOptions
{
public virtual string ConfigurationRoot { get; set; } = "";
public virtual string CacheServiceUrl { get; set; } = "";
public virtual ConcurrentDictionary<string, TimeSpan> CacheSettings { get; set; } = new();
public virtual ConcurrentDictionary<string, TimeSpan> CacheGroupSettings { get; set; } = new();
public virtual IDictionary<string, TimeSpan> CacheSettings { get; init; } = new ConcurrentDictionary<string, TimeSpan>();
public virtual IDictionary<string, TimeSpan> CacheGroupSettings { get; init; } = new ConcurrentDictionary<string, TimeSpan>();
}
}
6 changes: 2 additions & 4 deletions src/Nebula.Caching.Common/Settings/Configurations.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Diagnostics.CodeAnalysis;
using Nebula.Caching.Common.Constants;

namespace Nebula.Caching.Common.Settings
{
[ExcludeFromCodeCoverage]
public class Configurations
{
public string ConfigurationSection { get; set; } = CacheConfigurationConstants.ConfigurationSection;
public int DefaultCacheDurationInSeconds { get; set; } = CacheDurationConstants.DefaultCacheDurationInSeconds;
public string ConfigurationSection { get; init; } = CacheConfigurationConstants.ConfigurationSection;
public int DefaultCacheDurationInSeconds { get; init; } = CacheDurationConstants.DefaultCacheDurationInSeconds;
}
}
12 changes: 4 additions & 8 deletions src/Nebula.Caching.Common/Utils/ContextUtils.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
using System.Collections.Generic;
using System.Reflection;
using AspectCore.DynamicProxy;
using AspectCore.DynamicProxy.Parameters;
using Common.Settings;
using Microsoft.Extensions.Configuration;
using Nebula.Caching.Common.Attributes;
using Nebula.Caching.Common.KeyManager;
using System.Reflection;

namespace Nebula.Caching.Common.Utils
{
public class ContextUtils : IContextUtils
{

private IKeyManager _keyManager;
private IConfiguration _configuration;
private BaseOptions _baseOptions;

public ContextUtils(IKeyManager keyManager, IConfiguration configuration, BaseOptions baseOptions)
public ContextUtils(IKeyManager keyManager, BaseOptions baseOptions)
{
_keyManager = keyManager;
_configuration = configuration;
_baseOptions = baseOptions;
}

Expand Down Expand Up @@ -77,7 +73,7 @@ public int RetrieveCacheExpirationFromConfig(string key, AspectContext context)
ArgumentNullException.ThrowIfNull(key);

var convertedKey = _keyManager.ConvertCacheKeyToConfigKey(_keyManager.GenerateKey(context.ImplementationMethod, context.ServiceMethod, GenerateParamsFromParamCollection(context.GetParameters())));
var cacheExpiration = _baseOptions.CacheSettings.GetValueOrDefault(convertedKey);
_baseOptions.CacheSettings.TryGetValue(convertedKey, out TimeSpan cacheExpiration);

if (IsCacheExpirationValid(cacheExpiration))
{
Expand Down Expand Up @@ -109,7 +105,7 @@ public bool IsCacheGroupDefined(BaseAttribute attribute)

public int RetrieveCacheExpirationFromCacheGroup(string cacheGroup)
{
var cacheExpiration = _baseOptions.CacheGroupSettings.GetValueOrDefault(cacheGroup);
_baseOptions.CacheGroupSettings.TryGetValue(cacheGroup, out TimeSpan cacheExpiration);

if (IsCacheExpirationValid(cacheExpiration))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Nebula.Caching.InMemory/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Nebula.Caching.InMemory.Extensions
{
public static class Extensions
{
public static IServiceCollection AddInMemoryChache(this IServiceCollection services, InMemoryConfigurations configs = null)
public static IServiceCollection AddInMemoryChache(this IServiceCollection services, InMemoryConfigurations? configs = null)
{
return services
.AddInMemoryInterceptor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Nebula.Caching.InMemory.Extensions.InMemoryExtensions
{
public static class InMemoryExtensions
{
public static IServiceCollection AddInMemoryExtensions(this IServiceCollection services, InMemoryConfigurations inMemoryConfigs)
public static IServiceCollection AddInMemoryExtensions(this IServiceCollection services, InMemoryConfigurations? inMemoryConfigs)
{
// CreateDefaultInMemoryConfigurationsIfNull(inMemoryConfigs);
// SetDefaultValuesBasedOnInMemoryConfigurations(inMemoryConfigs);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Nebula.Caching.Common.Settings;

namespace Nebula.Caching.InMemory.Settings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ public static IServiceCollection AddUtilsExtensions(this IServiceCollection serv
{
services.AddScoped<IContextUtils>(serviceProvider =>
{
var configuration = serviceProvider.GetService<IConfiguration>();
var inMemoryOptions = serviceProvider.GetService<InMemoryOptions>();
return new ContextUtils(new InMemoryKeyManager(), configuration, inMemoryOptions);
return new ContextUtils(new InMemoryKeyManager(), inMemoryOptions);
});

services.AddScoped<GZipCompression>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Nebula.Caching.Common.Attributes;

namespace Nebula.Caching.Memcached.Attributes
{
public class MemCachedCacheAttribute : BaseAttribute
{

}
}
46 changes: 46 additions & 0 deletions src/Nebula.Caching.Memcached/CacheManager/MemCachedCacheManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Enyim.Caching;
using Nebula.Caching.Common.CacheManager;

namespace Nebula.Caching.Memcached.CacheManager
{
public class MemCachedCacheManager : ICacheManager
{
private readonly IMemcachedClient _memCached;

public MemCachedCacheManager(IMemcachedClient memCached)
{
_memCached = memCached;
}

public bool CacheExists(string key)
{
return _memCached.Get<object>(key) is not null;
}

public async Task<bool> CacheExistsAsync(string key)
{
var cache = (await _memCached.GetAsync<string>(key));
return cache.HasValue;
}

public string Get(string key)
{
return _memCached.Get<string>(key);
}

public async Task<string> GetAsync(string key)
{
return (await _memCached.GetAsync<string>(key)).Value;
}

public void Set(string key, string value, TimeSpan expiration)
{
_memCached.Set(key, value, expiration);
}

public async Task SetAsync(string key, string value, TimeSpan expiration)
{
await _memCached.SetAsync(key, value, expiration);
}
}
}
21 changes: 21 additions & 0 deletions src/Nebula.Caching.Memcached/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.DependencyInjection;
using Nebula.Caching.MemCached.Extensions.InterceptorExtensions;
using Nebula.Caching.MemCached.Extensions.ManagerExtensions;
using Nebula.Caching.MemCached.Extensions.RedisExtensions;
using Nebula.Caching.MemCached.Extensions.UtilsExtensions;
using Nebula.Caching.MemCached.Settings;

namespace Nebula.Caching.MemCached.Extensions
{
public static class Extensions
{
public static IServiceCollection AddMemCachedChache(this IServiceCollection services, MemCachedConfigurations configs)
{
return services
.AddMemCachedInterceptor()
.AddMemCachedExtensions(configs)
.AddManagerExtensions()
.AddUtilsExtensions();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using AspectCore.Configuration;
using AspectCore.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Nebula.Caching.Memcached.Interceptors;

namespace Nebula.Caching.MemCached.Extensions.InterceptorExtensions
{
public static class InterceptorExtensions
{
public static IServiceCollection AddMemCachedInterceptor(this IServiceCollection services)
{
services.AddSingleton<MemCachedInterceptor>();

services.ConfigureDynamicProxy(config =>
{
config
.Interceptors
.AddServiced<MemCachedInterceptor>();
});

return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Enyim.Caching;
using Microsoft.Extensions.DependencyInjection;
using Nebula.Caching.Common.CacheManager;
using Nebula.Caching.Common.KeyManager;
using Nebula.Caching.Memcached.CacheManager;
using Nebula.Caching.Memcached.KeyManager;

namespace Nebula.Caching.MemCached.Extensions.ManagerExtensions
{
public static class ManagerExtensions
{
public static IServiceCollection AddManagerExtensions(this IServiceCollection services)
{
var memCachedServer = services.BuildServiceProvider().GetService<IMemcachedClient>();

services.AddScoped<ICacheManager>(serviceProvider =>
{
return new MemCachedCacheManager(memCachedServer);
});

services.AddScoped<IKeyManager>(serviceProvider =>
{
return new MemCachedKeyManager();
});

return services;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Enyim.Caching.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nebula.Caching.Common.Constants;
using Nebula.Caching.MemCached.Settings;

namespace Nebula.Caching.MemCached.Extensions.RedisExtensions
{
public static class MemCachedExtensions
{
public static IServiceCollection AddMemCachedExtensions(this IServiceCollection services, MemCachedConfigurations configs)
{
CacheDurationConstants.DefaultCacheDurationInSeconds = configs.DefaultCacheDurationInSeconds;

CacheConfigurationConstants.ConfigurationSection = configs.ConfigurationSection;

services.AddSingleton<MemCachedOptions>(ctx =>
{
var configuration = ctx.GetService<IConfiguration>();
var memCachedOptions = configuration.GetSection(configs.ConfigurationSection).Get<MemCachedOptions>();
memCachedOptions.ConfigurationRoot = configs.ConfigurationSection;
return memCachedOptions;
});

SetupMemCache(services);

return services;
}

private static void SetupMemCache(IServiceCollection services)
{
var serviceProvider = services.BuildServiceProvider();
var memCachedOptions = serviceProvider.GetRequiredService<MemCachedOptions>();
var memCacheServerSplit = memCachedOptions.CacheServiceUrl.Split(':');
services.AddEnyimMemcached(o => o.Servers = new List<Server> { new Server { Address = memCacheServerSplit[0], Port = Int32.Parse(memCacheServerSplit[1]) } });
}

}
}
Loading

0 comments on commit 699716a

Please sign in to comment.