|
1 | 1 | Manage Cache Easily |
| 2 | + |
| 3 | +### Usage |
| 4 | + |
| 5 | +First install package: |
| 6 | + |
| 7 | +> https://www.nuget.org/packages/EasyMultiCacheManager |
| 8 | +
|
| 9 | +then you can use like this: |
| 10 | + |
| 11 | +```csharp |
| 12 | +var easyCacheManager = new CacheBuilder<string>() |
| 13 | + .AddApi(new ApiConfig |
| 14 | + { |
| 15 | + Url = StaticData.Api |
| 16 | + }) |
| 17 | + .AddRedis(new RedisConfig |
| 18 | + { |
| 19 | + ConnectionString = redisConnectionString |
| 20 | + }) |
| 21 | + .AddDb(new DbConfig |
| 22 | + { |
| 23 | + ConnectionString = sqlConnectionString, |
| 24 | + Query = StaticData.QueryToSelect |
| 25 | + }) |
| 26 | + .AddMemory(new MemoryConfig()) |
| 27 | + .Build(new LockConfig()); |
| 28 | + |
| 29 | +var result = await easyCacheManager.GetAsync("My-Key"); |
| 30 | +``` |
| 31 | +You can create your own provider with these interfaces: |
| 32 | + |
| 33 | + - IBaseCacheSource |
| 34 | + - ICacheSourceWithClear |
| 35 | + - ICacheSourceWithSet |
| 36 | + - ICacheSourceWithSetAndClear |
| 37 | + |
| 38 | +Default providers are these: |
| 39 | + |
| 40 | + - MemoryCacheSource : ICacheSourceWithSetAndClear : Priority => 1 |
| 41 | + - RedisCacheSource : ICacheSourceWithSetAndClear : Priority => 2 |
| 42 | + - DbCacheSource : IBaseCacheSource : Priority => 3 |
| 43 | + - ApiCacheSource : IBaseCacheSource : Priority => 4 |
| 44 | + |
| 45 | +On `IBaseCacheSource` you have only `Get` from cache, for example on `ApiCacheSource` you get real data from api. |
| 46 | +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` |
| 47 | + |
| 48 | +### Example |
| 49 | +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. |
| 50 | +also if found on db or api it will set to redis and memory. |
| 51 | + |
| 52 | +### Concurrency |
| 53 | +This package use AsyncKeyedLock to handle lock on get, set and clear on specific key. so you use this package on multi thread program. |
| 54 | + |
| 55 | +### Info |
| 56 | +You can use all type like class, object, string to cache, for example EasyCacheManager<string>, EasyCacheManager<MyClass> |
| 57 | +Priority should be unique, you can't crate EasyCacheManager with list of providers with same Priority |
| 58 | + |
| 59 | +### Clear cache |
| 60 | +With `ClearCacheAsync` method you can clear specific key on all providers that implement `ICacheSourceWithClear` or `ICacheSourceWithSetAndClear` |
| 61 | + |
| 62 | +### Set |
| 63 | +With `SetAsync` you can manually set value to provides that that implement `ICacheSourceWithSet` or `ICacheSourceWithSetAndClear` |
| 64 | + |
0 commit comments