Skip to content

Instantly share code, notes, and snippets.

@twilly86
Last active March 11, 2021 13:17
Show Gist options
  • Save twilly86/c297b56a556e83c1bcdce19d3a2574c6 to your computer and use it in GitHub Desktop.
Save twilly86/c297b56a556e83c1bcdce19d3a2574c6 to your computer and use it in GitHub Desktop.
EFSecondLevelCache Redis Service Provider
using EFSecondLevelCache;
using EFSecondLevelCache.Contracts;
using Newtonsoft.Json;
using StackExchange.Redis;
using StackExchange.Redis.Extensions.Core;
using StackExchange.Redis.Extensions.Newtonsoft;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Configuration;
/// <summary>
/// Returns a new cached query.
/// </summary>
public static class EFRedisQueryExtension
{
public static EFCachedQueryable<TType> Cacheable<TType>(this IQueryable<TType> query)
{
return new EFCachedQueryable<TType>(
query,
new EFCachePolicy(),
new EFCacheDebugInfo(),
new EFCacheKeyProvider(new EFCacheKeyHashProvider()),
new RedisCache());
}
}
public class RedisCache : IEFCacheServiceProvider
{
private static NewtonsoftSerializer serializer = new NewtonsoftSerializer();
private static StackExchangeRedisCacheClient cacheClient = new StackExchangeRedisCacheClient(serializer);
/// <summary>
/// Returns list of the cached keys.
/// </summary>
public IList<string> AllCachedKeys
{
get
{
return cacheClient.SearchKeys("*").ToList();
}
}
/// <summary>
/// Removes the cached entries added by this library.
/// </summary>
/// <param name="keyHashPrefix">Its default value is EF_.</param>
public void ClearAllCachedEntries(string keyHashPrefix = EFCacheKey.KeyHashPrefix)
{
var keys = GetAllEFCachedKeys(keyHashPrefix);
cacheClient.RemoveAll(keys);
}
/// <summary>
/// Gets all of the cached keys, added by this library.
/// </summary>
/// <param name="keyHashPrefix">Its default value is EF_.</param>
/// <returns>list of the keys</returns>
public IList<string> GetAllEFCachedKeys(string keyHashPrefix = EFCacheKey.KeyHashPrefix)
{
return cacheClient.SearchKeys(keyHashPrefix + "*").ToList();
}
/// <summary>
/// Gets a cached entry by key.
/// </summary>
/// <param name="cacheKey">key to find</param>
/// <returns>cached value</returns>
public object GetValue(string cacheKey)
{
return cacheClient.Get<object>(cacheKey);
}
/// <summary>
/// Adds a new item to the cache.
/// </summary>
/// <param name="cacheKey">key</param>
/// <param name="value">value</param>
/// <param name="rootCacheKeys">cache dependencies</param>
/// <param name="absoluteExpiration">absolute expiration time</param>
/// <param name="priority">its default value is CacheItemPriority.Normal</param>
public void InsertValue(string cacheKey, object value,
string[] rootCacheKeys,
DateTime absoluteExpiration,
CacheItemPriority priority = CacheItemPriority.Normal)
{
cacheClient.Add<object>(cacheKey, value);
}
/// <summary>
/// Invalidates all of the cache entries which are dependent on any of the specified root keys.
/// </summary>
/// <param name="rootCacheKeys">cache dependencies</param>
public void InvalidateCacheDependencies(string[] rootCacheKeys)
{
cacheClient.RemoveAll(rootCacheKeys);
}
/// <summary>
/// The name of the cache keys used to clear the cache. All cached items depend on these keys.
/// </summary>
public void StoreRootCacheKeys(string[] rootCacheKeys)
{
}
}
@aborja79
Copy link

Is compatible with StackExchange.Redis 2.0 package?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment