Skip to content

Feature/cache fallback #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AopCaching/AopCaching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Core\Core.csproj" />
<ProjectReference Include="..\RedisCaching\RedisCaching.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="&quot;$(SolutionDir).NuGetPack\NuGetPack.exe&quot; &quot;$(ProjectPath)&quot; &quot;$(TargetPath)&quot; $(ConfigurationName)" />
Expand Down
54 changes: 50 additions & 4 deletions AopCaching/CacheAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PostSharp.Serialization;
using PubComp.Caching.Core;
using PubComp.Caching.Core.Attributes;
using PubComp.Caching.RedisCaching;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -22,6 +23,7 @@ public class CacheAttribute : MethodInterceptionAspect
private int[] indexesNotToCache;
private bool isClassGeneric;
private bool isMethodGeneric;
private bool _isRedisActive = true;

public CacheAttribute()
{
Expand Down Expand Up @@ -66,10 +68,28 @@ public sealed override void OnInvoke(MethodInterceptionArgs args)
{
if (this.cache == null)
{
this.cache = CacheManager.GetCache(this.cacheName);
this.cache = CacheManager.GetCache(cacheName);
if (this.cache == null)
{
LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!");
LogManager.GetCurrentClassLogger().Warn("AOP cache [" + cacheName + "] is not initialized, define NoCache if needed!");
}
}
else if (_isRedisActive && cacheName.Contains("BackUp"))
{
cacheName = cacheName.Replace("BackUp", "");
this.cache = CacheManager.GetCache(cacheName);
}

if (this.cache is RedisCache)
{
_isRedisActive = (this.cache as RedisCache).IsActive;
if (!(this.cache as RedisCache).IsRedisConnectionStateHandlerRegistered)
(this.cache as RedisCache).OnRedisConnectionStateChanged += CacheAttribute_OnRedisConnectionStateChanged;

if (!_isRedisActive)
{
cacheName = string.Format("{0}{1}", cacheName, "BackUp");
this.cache = CacheManager.GetCache(cacheName);
}
}

Expand All @@ -91,10 +111,28 @@ public sealed override async Task OnInvokeAsync(MethodInterceptionArgs args)
{
if (this.cache == null)
{
this.cache = CacheManager.GetCache(this.cacheName);
this.cache = CacheManager.GetCache(cacheName);
if (this.cache == null)
{
LogManager.GetCurrentClassLogger().Warn($"AOP cache [{this.cacheName}] is not initialized, define NoCache if needed!");
LogManager.GetCurrentClassLogger().Warn("AOP cache [" + cacheName + "] is not initialized, define NoCache if needed!");
}
}
else if (_isRedisActive && cacheName.Contains("BackUp"))
{
cacheName = cacheName.Replace("BackUp", "");
this.cache = CacheManager.GetCache(cacheName);
}

if (this.cache is RedisCache)
{
_isRedisActive = (this.cache as RedisCache).IsActive;
if (!(this.cache as RedisCache).IsRedisConnectionStateHandlerRegistered)
(this.cache as RedisCache).OnRedisConnectionStateChanged += CacheAttribute_OnRedisConnectionStateChanged;

if (!_isRedisActive)
{
cacheName = string.Format("{0}{1}", cacheName, "BackUp");
this.cache = CacheManager.GetCache(cacheName);
}
}

Expand All @@ -117,6 +155,14 @@ public sealed override async Task OnInvokeAsync(MethodInterceptionArgs args)
args.ReturnValue = SafeCasting.CastTo(returnType, result);
}

private void CacheAttribute_OnRedisConnectionStateChanged(object sender, bool connectionRestored)
{
if (connectionRestored)
{
_isRedisActive = true;
}
}

private string GetCacheKey(MethodInterceptionArgs args)
{
var classNameNonGeneric = !this.isClassGeneric
Expand Down
8 changes: 7 additions & 1 deletion RedisCaching/CacheContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class CacheContext : IDisposable
{
private readonly RedisClient client;
private readonly IRedisConverter convert;
public event EventHandler<bool> OnRedisConnectionStateChanged;

public bool IsActive { get; private set; }

Expand All @@ -29,7 +30,12 @@ public CacheContext(String connectionName, String converterType)

private void RegisterToRedisConnectionStateChangeEvent()
{
this.client.OnRedisConnectionStateChanged += (sender, args) => this.IsActive = args.IsAvailable;
this.client.OnRedisConnectionStateChanged += (sender, args) =>
{
this.IsActive = args.IsAvailable;
this.OnRedisConnectionStateChanged(sender, IsActive);
};

this.IsActive = this.client.IsConnected;
}

Expand Down
17 changes: 17 additions & 0 deletions RedisCaching/RedisCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ public class RedisCache : ICacheV2
private readonly NLog.ILogger log;

private readonly RedisCachePolicy Policy;
public event EventHandler<bool> OnRedisConnectionStateChanged;

public string Name { get { return this.name; } }
public bool IsActive => InnerCache.IsActive;

public bool IsRedisConnectionStateHandlerRegistered
{
get
{
return OnRedisConnectionStateChanged != null;
}
}

private CacheContext InnerCache
{
get { return innerCache; }
Expand Down Expand Up @@ -106,11 +115,19 @@ public RedisCache(String name, RedisCachePolicy policy)
this.innerCache = new CacheContext(policy.ConnectionName, this.converterType);
}

this.innerCache.OnRedisConnectionStateChanged += InnerCache_OnRedisConnectionStateChanged;

this.notiferName = policy.SyncProvider;

this.synchronizer = CacheSynchronizer.CreateCacheSynchronizer(this, this.notiferName);
}

private void InnerCache_OnRedisConnectionStateChanged(object sender, bool e)
{
if (IsRedisConnectionStateHandlerRegistered)
OnRedisConnectionStateChanged(sender, e);
}

private TValue GetOrAdd<TValue>(
CacheContext context, string key, TValue newValue, bool doForceOverride = false)
{
Expand Down