-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Nebula-Software-Systems/add_memcached
Add memcached
- Loading branch information
Showing
37 changed files
with
448 additions
and
525 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
src/Nebula.Caching.Common/Constants/CacheDurationConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
src/Nebula.Caching.InMemory/Settings/InMemoryConfigurations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/Nebula.Caching.Memcached/Attributes/MemCachedCacheAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
src/Nebula.Caching.Memcached/CacheManager/MemCachedCacheManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Nebula.Caching.Memcached/Extensions/InterceptorExtensions/InterceptorExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Nebula.Caching.Memcached/Extensions/ManagerExtensions/ManagerExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Nebula.Caching.Memcached/Extensions/MemCachedExtensions/MemCachedExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) } }); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.