Carter.Cache
An extensable library to cache your Carter modules.
Builds
Github | Branch |
---|---|
master |
Packages
Package | NuGet (Stable) | MyGet (Prerelease) |
---|---|---|
Carter.Cache |
Installation
Install via nuget
PM> Install-Package Carter.Cache
This library depends on Carter to properly work, you can install Carter using the following command:
PM> Install-Package Carter
Sample usage
- Add carter caching to your Startup.cs:
using Carter.Cache;
// The rest of your Startup.cs definition...
public void ConfigureServices(IServiceCollection services)
{
services.AddCarterCaching(new CachingOption(2048)); //Recommended to always provide a caching mas size limit
services.AddCarter();
}
- Define a Configuration usage
public void Configure(IApplicationBuilder app, AppSettings appSettings)
{
app.UseCarterCaching();
app.UseEndpoints(builder => builder.MapCarter());
}
- Add the Cacheable clause to your module:
public class HomeModule : CarterModule
{
public HomeModule()
{
Get("/", (req, res) =>
{
req.AsCacheable(10);
res.StatusCode = 200;
return res.WriteAsync("Hello world");
});
}
}
The default configuration does use the Microsoft.Extensions.Caching.Memory library as a default caching mechanism. Note: By default memory caching can put lots of pressure on the memory of your system, please refer to the following microsoft in-memory cache docs for basics on usage.
Customization
You can easily define a custom Store by implementing the ICacheStore interface with the following signature:
public interface ICacheStore
{
bool TryGetValue(string key, out CachedResponse cachedResponse);
void Set(string key, CachedResponse response, TimeSpan expiration);
void Remove(string key);
}
Also a custom Key can be easily defined by implementing the ICacheKey interface:
public interface ICacheKey
{
string Get(HttpRequest request);
}