Add RemoveAsync cache
This commit is contained in:
parent
0d78a3996a
commit
ebeccad542
|
|
@ -5,4 +5,5 @@ public interface ICacheService
|
||||||
Task<T?> GetAsync<T>(string key);
|
Task<T?> GetAsync<T>(string key);
|
||||||
Task<object> GetAsync(string key, Type type);
|
Task<object> GetAsync(string key, Type type);
|
||||||
Task SetAsync<T>(string key, T value, TimeSpan? expiry);
|
Task SetAsync<T>(string key, T value, TimeSpan? expiry);
|
||||||
|
Task RemoveAsync(string key);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,13 @@ public class SharpCacheAttribute : MoAttribute
|
||||||
var value = cache.GetAsync(key, context.TaskReturnType).Result;
|
var value = cache.GetAsync(key, context.TaskReturnType).Result;
|
||||||
if (value != null)
|
if (value != null)
|
||||||
{
|
{
|
||||||
context.ReplaceReturnValue(this, value);
|
// check if the cache is out of date
|
||||||
|
var isOutOfDate = IsOutOfDate(context, value).Result;
|
||||||
|
|
||||||
|
if (!isOutOfDate)
|
||||||
|
{
|
||||||
|
context.ReplaceReturnValue(this, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,6 +64,11 @@ public class SharpCacheAttribute : MoAttribute
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual Task<bool> IsOutOfDate(MethodContext context, object value)
|
||||||
|
{
|
||||||
|
return Task.FromResult(false);
|
||||||
|
}
|
||||||
|
|
||||||
private string GetCacheKey(SharpCacheSettings settings, MethodContext context)
|
private string GetCacheKey(SharpCacheSettings settings, MethodContext context)
|
||||||
{
|
{
|
||||||
var key = settings.Prefix + "-" + context.Method.Name;
|
var key = settings.Prefix + "-" + context.Method.Name;
|
||||||
|
|
|
||||||
|
|
@ -32,4 +32,9 @@ public class MemoryCacheService : ICacheService
|
||||||
AbsoluteExpirationRelativeToNow = expiry
|
AbsoluteExpirationRelativeToNow = expiry
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task RemoveAsync(string key)
|
||||||
|
{
|
||||||
|
_cache.Remove(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -76,4 +76,20 @@ public class RedisCacheService : ICacheService
|
||||||
var db = redis.GetDatabase();
|
var db = redis.GetDatabase();
|
||||||
await db.StringSetAsync(key, JsonConvert.SerializeObject(value), expiry);
|
await db.StringSetAsync(key, JsonConvert.SerializeObject(value), expiry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task RemoveAsync(string key)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(_settings.Redis))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (redis == null)
|
||||||
|
{
|
||||||
|
redis = ConnectionMultiplexer.Connect(_settings.Redis);
|
||||||
|
}
|
||||||
|
|
||||||
|
var db = redis.GetDatabase();
|
||||||
|
await db.KeyDeleteAsync(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue