Carter.Cache

A caching library helper for your Carter API


Keywords
carter, cache, netcore
License
MIT
Install
Install-Package Carter.Cache -Version 0.2.0

Documentation

Carter.Cache Mit License

An extensable library to cache your Carter modules.

Builds

Github Branch
.NET Core master

Packages

Package NuGet (Stable) MyGet (Prerelease)
Carter.Cache NuGet MyGet

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

  1. 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();
        }
  1. Define a Configuration usage
    public void Configure(IApplicationBuilder app, AppSettings appSettings)
    {
        app.UseCarterCaching();
        app.UseEndpoints(builder => builder.MapCarter());
    }
  1. 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);
    }