Diatly.Cache
1.0.0.1
dotnet add package Diatly.Cache --version 1.0.0.1
NuGet\Install-Package Diatly.Cache -Version 1.0.0.1
<PackageReference Include="Diatly.Cache" Version="1.0.0.1" />
paket add Diatly.Cache --version 1.0.0.1
#r "nuget: Diatly.Cache, 1.0.0.1"
// Install Diatly.Cache as a Cake Addin
#addin nuget:?package=Diatly.Cache&version=1.0.0.1
// Install Diatly.Cache as a Cake Tool
#tool nuget:?package=Diatly.Cache&version=1.0.0.1
***************** Dialty.Cache Library *****************
There is cache implemention of
- Memory cache and
- Redis cache.
How you use ?
Set configureService in startup.cs file
Step 1:
public void ConfigureServices(IServiceCollection services) { if (Environment.EnvironmentName != "Production") { string redisConnection = Configuration["Redis:ConnectionString"]; services.AddSingleton<IDistributedCache>(factory ⇒ { var cache = new RedisCache(new RedisCacheOptions { Configuration = redisConnection }); return cache; });
services.AddSingleton(typeof(ICacheService<>), typeof(RedisCacheService<>));
}
else
{
services.AddSingleton<IMemoryCache>(factory =>
{
var cache = new MemoryCache(new MemoryCacheOptions());
return cache;
});
services.AddSingleton(typeof(ICacheService<>), typeof(MemoryCacheService<>));
}
services.AddMvc();
}
Step2: Creat business layer sen use cache.
public class ProductManager : IProductManager { private IDataRepository<Product, int> _iRepo; private readonly ICacheService<Product> cache; public ProductManager(IDataRepository<Product, int> repo, ICacheService<Product> cacheService) { _iRepo = repo; cache = cacheService; }
public long Add(Product model)
{
var data = _iRepo.Add(model);
cache.Delete("all");
return data;
}
public long Delete(int id)
{
var data = _iRepo.Delete(id);
cache.Delete(id.ToString());
cache.Delete("all");
return data;
}
public Product Get(int id)
{
Product product = null;
var dataById = cache.GetById(id.ToString());
if (dataById == null)
{
var products = cache.GetAll("all");
if (products == null)
{
product = _iRepo.Get(id);
cache.Save(id.ToString(), product);
}
else
{
product = products.FirstOrDefault(b => b.ProductId == id);
cache.Save(id.ToString(), product);
}
}
else
{
product = dataById;
}
return product;
}
public IEnumerable<Product> GetAll()
{
var data = cache.GetAll("all");
if (data == null)
{
data = _iRepo.GetAll().ToList();
var productsList = JsonConvert.SerializeObject(data);
cache.Save("all", productsList);
}
return data;
}
public long Update(int id, Product model)
{
var data = _iRepo.Update(id, model);
cache.Delete(id.ToString());
cache.Delete("all");
return data;
}
}
Step3: Add Redis key in appsettings.json file, if you want to use redis cache
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 is compatible. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
-
.NETCoreApp 2.0
- Microsoft.AspNetCore.All (>= 2.0.6)
- Newtonsoft.Json (>= 11.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|