Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dvlkv/cacheit
CacheIt makes caching in your application easier
https://github.com/dvlkv/cacheit
asp-net-core caching dotnet dotnet-core json memory-cache message-pack redis
Last synced: about 2 months ago
JSON representation
CacheIt makes caching in your application easier
- Host: GitHub
- URL: https://github.com/dvlkv/cacheit
- Owner: dvlkv
- Created: 2018-08-28T05:48:52.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-08-28T11:27:40.000Z (over 6 years ago)
- Last Synced: 2024-11-13T04:54:42.044Z (2 months ago)
- Topics: asp-net-core, caching, dotnet, dotnet-core, json, memory-cache, message-pack, redis
- Language: C#
- Homepage:
- Size: 18.6 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CacheIt
CacheIt makes caching in your app better - you don't need to write many lines of boilerplate## Usage
### Default caching implementation
```c#
public interface IService
{
string CachedFunction();
Task AsyncCachedFunction();
}public class ServiceImplementation : IService
{
public string CachedFunction()
{
var entry = _cache.Get(GetKey());
if (entry != null)
return entry;
var result = "test";
_cache.Set(result);
return result;
}public async Task AsyncCachedFunction()
{
var entry = _cache.Get(GetKey());
if (entry != null)
return entry;
var result = "async test";
_cache.Set(result);
return result;
}
}
```
### Caching implementation with CacheIt
```c#
public interface IService
{
[Cached]
string CachedFunction();
[Cached]
Task AsyncCachedFunction();
}public class ServiceImplementation : IService
{
public string CachedFunction()
{
return "test";
}public async Task AsyncCachedFunction()
{
return "async test";
}
}
```
## Setup
Now CacheIt supports IDistributedCache and IMemoryCache
To make it work, you should install CacheIt.DistributedCache or CacheIt.MemoryCacheNow you should add this to your Startup.cs:
```c#
services.AddDistributedCachable();
```
or
```c#
services.AddMemoryCachable();
```If you use distributed cache, you need to add entry serializer:
```c#
services.AddJsonEntrySerializer();
```
or
```c#
services.AddMessagePackEntrySerializer();
```
#
Then you should decorate your services to enable caching in them, it's easy:
Use
```c#
services.AddCachable();
```
Instead of
```c#
services.AddScoped();
```
## Configuration
You can configure cache entries like
```c#
services.AddMemoryCachable(opts =>
{
opts.ConfigureAll(entryOptions => { ... });
opts.Configure(entryOptions => { ... });
});
```