Manage Cache Easily
First install package:
If you want Redis then install:
If you want Database then install:
https://www.nuget.org/packages/EasyMultiCacheManager.Database
If you want Api then install:
Then you can use like this:
var easyCacheManager = new CacheBuilder()
.AddApi(new ApiConfig
{
Url = StaticData.Api
})
.AddRedis(new RedisConfig
{
ConnectionString = redisConnectionString
})
.AddDb(new DbConfig
{
ConnectionString = sqlConnectionString,
Query = StaticData.QueryToSelect
})
.AddMemory(new MemoryConfig())
.Build(new LockConfig());
var result = await easyCacheManager.GetAsync<string>("My-Key");
You can create your own provider with these interfaces:
- IBaseCacheSource
- ICacheSourceWithClear
- ICacheSourceWithSet
- ICacheSourceWithSetAndClear
Default providers are these:
- MemoryCacheSource : ICacheSourceWithSetAndClear : Priority => 1
- RedisCacheSource : ICacheSourceWithSetAndClear : Priority => 2
- DbCacheSource : IBaseCacheSource : Priority => 3
- ApiCacheSource : IBaseCacheSource : Priority => 4
On IBaseCacheSource
you have only Get
from cache, for example on ApiCacheSource
you get real data from api.
if item find from one provider and not exist on other provider, it will automatically set to other providers that implemented from ICacheSourceWithSetAndClear
or ICacheSourceWithSet
First try get from Memory because it Priority
is 1, if not exist it try get from redis, after that it try to get from db, and after that it get data from api. if not found it will return null.
also if found on db or api it will set to redis and memory.
This package use AsyncKeyedLock to handle lock on get, set and clear on specific key. so you use this package on multi thread program.
You can use all type like class, object, string to cache, for example EasyCacheManager, EasyCacheManager
Priority should be unique, you can't crate EasyCacheManager with list of providers with same Priority
With ClearCacheAsync
method you can clear specific key on all providers that implement ICacheSourceWithClear
or ICacheSourceWithSetAndClear
With ClearAllCacheAsync
method you can clear all keys on all providers that implement ICacheSourceWithClear
or ICacheSourceWithSetAndClear
With SetAsync
you can manually set value to provides that that implement ICacheSourceWithSet
or ICacheSourceWithSetAndClear
If you want to have ability to clear cache on all solutions you can use these packages.
If you want Redis event install:
https://www.nuget.org/packages/EasyMultiCacheManager.Clear.Redis
If you want Kafka event then install:
https://www.nuget.org/packages/EasyMultiCacheManager.Clear.Kafka
If you want rabbitMQ event then install:
https://www.nuget.org/packages/EasyMultiCacheManager.Clear.Rabbit
Now you can use like this:
using CacheManager;
using CacheManagerClear;
using CacheManagerClear.Kafka;
using Confluent.Kafka;
class Program
{
static async Task Main(string[] args)
{
// Kafka configuration
var producerConfig = new ProducerConfig
{
BootstrapServers = "localhost:9092"
};
var consumerConfig = new ConsumerConfig
{
BootstrapServers = "localhost:9092",
GroupId = "cache-clear-group",
AutoOffsetReset = AutoOffsetReset.Earliest
};
var topic = "cache_clear_topic";
// Cache manager
IEasyCacheManager cacheManager = new EasyCacheManager();
// Initialize producer and consumer
using var producer = new ProducerBuilder<Null, string>(producerConfig).Build();
using var consumer = new ConsumerBuilder<Null, string>(consumerConfig).Build();
// Create CacheManagerClearBuilder
var builder = new CacheManagerClearBuilder();
// Configure Kafka Publisher
builder.UseKafkaPublisher(producer, topic);
// Configure Kafka Subscriber
builder.UseKafkaSubscriber(consumer, topic, cacheManager);
// Alternatively, configure both in one call
// builder.UseKafkaPublisherAndSubscriber(producer, consumer, topic, cacheManager);
// Build and get publisher and subscriber
var (publisher, subscriber) = builder.BuildPublisherAndSubscriber(
new CachePublisher(producer, topic),
new CacheSubscriber(consumer, topic, cacheManager)
);
// Subscribe to cache clear events
await subscriber.SubscribeAsync();
// Publish a cache clear event
await publisher.PublishClearCacheAsync("sample_cache_key");
Console.WriteLine("Cache clear event published and subscriber listening...");
Console.ReadLine();
}
}
The following contributors have made this project better: