Official FA .NET core library containing multiple utility libraries
- Clone the repo or add it as submodule
- Add it in your project
dotnet add FA.Cache/FA.Cache.csproj
Provides two popular cache:
- Memory Cache
- Redis Cache
Following implementation of above cache are provided:
MemoryCacheProvider
: Use .NET inbuilt memory cache.RedisCacheProvider
: Redis cache provider for both read write combined. Use when only 1 redis server is there.RedisReadWriteCacheProvider
: Provides separate read write connections. Use when master, replicas are different.
Using cache in any project is now super easy
- Update Dependencies.cs
// Cache
ConfigUtils.SetupCache(serviceProvider, configuration);
Example implementation 1
public static void SetupCache(IServiceCollection serviceProvider, IConfiguration configuration)
{
var redisCacheConnectionString = configuration.GetConnectionString("RedisCache");
serviceProvider.AddMemoryCache();
if (redisCacheConnectionString != null)
{
serviceProvider.AddSingleton<ICacheProvider>(s => new RedisCacheProvider(redisCacheConnectionString));
Console.WriteLine("\u2705 Cache: Redis cache setup successful");
}
else
{
serviceProvider.AddSingleton<ICacheProvider, MemoryCacheProvider>();
Console.WriteLine("\u2705 Cache: Memory cache setup successful");
}
serviceProvider.AddSingleton<CacheHelper>();
}
Example Implementation 2
private static void SetupCache(IServiceCollection serviceProvider, IConfiguration configuration)
{
// add for safety
serviceProvider.AddMemoryCache();
var redisReadCacheConnectionString = configuration.GetConnectionString("RedisReadCache");
var redisWriteCacheConnectionString = configuration.GetConnectionString("RedisWriteCache");
if (redisReadCacheConnectionString != null && redisWriteCacheConnectionString != null)
{
serviceProvider.AddSingleton<ICacheProvider>(s => new RedisReadWriteCacheProvider(
redisReadOnlyConnectionString: redisReadCacheConnectionString,
redisWriteOnlyConnectionString: redisWriteCacheConnectionString
));
Console.WriteLine("\u2705 Cache: Redis cache setup successful");
}
else
{
serviceProvider.AddSingleton<ICacheProvider, MemoryCacheProvider>();
Console.WriteLine("\u2705 Cache: Memory cache setup successful");
}
serviceProvider.AddSingleton<CacheHelper>();
}
- Add CacheHelper as dependency as class constructor parameter in respective service.
- Use it directly
API Reference:
public async Task<T> GetResult<T>(string cacheKey, TimeSpan expiresIn, Func<Task<T>> fetchDataFunc)
cacheKey
: Unique corresponding key identifierexpiresIn
: Time to expire the cache valuefetchDataFunc
: callback to get data if cache not found
Example Usage:
var positionsList = await _cacheHelper.GetResult(
CacheKeys.GetPositionDetails(companyId), TimeSpan.FromHours(1),
() => _unifyDbRepository.GetCompanyPositionUserDetails(companyId));
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -m 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
Made with ❤️ by Ayush P Gupta (@apgapg)