Git Product home page Git Product logo

cashew's Introduction

Cashew

Cashew is a .NET library for caching responses easily with an HttpClient through an API that is simple and elegant yet powerful. There's support out of the box for the awesome CacheManager via the Cashew.Adapters.CacheManager package. Its aim is to focus on the HTTP part of caching and not worrying about how stuff is stored, meaning no half-arsed cache implementations!

Cashew targets .NET 4.5 and .NET Standard 1.1 (.NET Core, Mono, Xamarin.iOS, Xamarin.Android, UWP and more) meaning it can be used on all sorts of devices.

Build Status NuGet

Cashew-logo

Installation

The latest versions of the packages are available on NuGet. To install, run the following command if you want to roll your own cache:

PM> Install-Package Cashew

or the command below if you want to utilise the power of CacheManager

PM> Install-Package Cashew.Adapters.CacheManager

Features

General features

  • Extremely easy to use, all it takes is one line to configure the whole thing!
  • Simple but powerful API that allows customisation
  • ETag support
  • Vary Header support

Cache stores

Type Out of the box?
Dictionary Yes*
System.Runtime.Caching.MemoryCache Yes*
Microsoft.Extensions.Caching.Memory Yes*
Redis Yes*
Memcached Yes*
Couchbase Yes*
Custom No, but it's super easy to implement your own.

*Provided that you use Cashew.Adapters.CacheManager

HTTP Cache-Control Headers

Header Aka
max-age "I don't want cached responses older than this"
s-maxage "I don't want cached responses older than this"
max-stale "Stale responses are OK for this long"
min-fresh "The response has to still be fresh for at least this long"
no-cache "You must validate the cached response with the server
no-store "DO NOT CACHE THIS OR I WILL MAKE YOUR LIFE MISERABLE!"
only-if-cached "I only want a response if it's cached"
must-revalidate "You MUST revalidate stale responses"
proxy-revalidate "You MUST revalidate stale responses"

Customisation

Cashew provides a lot of customisation opportunities for its users. The most important ones are listed below:

Feature Quickstart In-depth
Use any cache store Link Wiki
Decide how cache keys are created Link Wiki
Decide which status codes are cacheable Link Wiki

Usage

For more in-depth information on how to use Cashew, please refer to our wiki.

Configuring HttpClient

//All it takes is one line to configure the whole thing!
var httpClient = new HttpClient(new HttpCachingHandler(cache, new HttpStandardKeyStrategy(cache)));

Use any cache store

//We feel like caching the HTTP responses in an SQL store (for some reason) and have therefore created our own SqlCache
var sqlCache = new SqlCache();

//We pass our newly created sql cache in the constructor and watch the magic happen
var httpCachingHandler = new HttpCachingHandler(sqlCache, keyStrategy);

Decide how cache keys are created

//We have created our own strategy that creates keys out of request URI:s
var uriKeyStrategy = new RequestUriKeyStrategy();

//We pass our newly created key strategy in the constructor and watch the magic happen!
var httpCachingHandler = new HttpCachingHandler(memoryCache, uriKeyStrategy);

Decide how query strings are handled

//The default implementation of ICacheKeyStrategy is HttpStandardKeyStrategy. You can configure it to handle query strings in two ways.

//Using CacheKeySetting.Standard will result in a different cache key each time the query string changes
var queryStringStrategy = new HttpStandardKeyStrategy(cache, CacheKeySetting.Standard);

//Using CacheKeySetting.IgnoreQueryString will result in the same key even if the query string changes.
var uriStrategy = new HttpStandardKeyStrategy(cache, CacheKeySetting.IgnoreQueryString);

Cacheable status codes

//We only want to cache responses with status 200
httpCachingHandler.CacheableStatusCodes = new[] { HttpStatusCode.OK };

Contributing

Please refer to our guidelines on contributing.

cashew's People

Contributors

jaxelr avatar joakimskoog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cashew's Issues

Status headers

We need to add some documentation about the status header that is set after each request.

Stream is consumed when using ReadAsAsync

Problem: If HttpResponseMessage.Content is read with ReadAsAsync the stream will be consumed. This causes issues when the user then tries to read the content of a cached response resulting in a null stream.

Solution: Serialise the response before adding it to the cache and deserialise it before retrieving it. This should be done with HttpContent.

Add HttpClientHandler-parameter to constructor

We explicitly create an empty instance of HttpClientHandler in our constructor, this makes the API cleaner and easier to use but it's totally unnecessary if the user wants to create its own and use some of its functionality.

Current constructor:

public HttpCachingHandler(IHttpCache cache, ICacheKeyStrategy keyStrategy)
{
    _cache = cache ?? throw new ArgumentNullException(nameof(cache));
    _keyStrategy = keyStrategy ?? throw new ArgumentNullException(nameof(keyStrategy));
    InnerHandler = new HttpClientHandler(); //This should be moved to its own parameter
}

ConfigureAwait

Seems like I forgot to add ConfigureAwait(false) to all async calls, we need to add them just to be sure.

Simplify GetCacheKey in HttpStandardKeyStrategy

The following code can probably be simplified to make it more readable, it's important to have keys in the same format!

 //todo: Simplify this
var varyHeaders = request.Headers.Where(x => response.Headers.Vary.Any(y => y.Equals(x.Key, StringComparison.CurrentCultureIgnoreCase))).SelectMany(o => o.Value);
var formattedVaryheaderString = varyHeaders.Aggregate("", (current, varyHeader) => current + (VaryHeaderDelimiter + varyHeader));

What to do when Request.CacheControl is null?

As the title states, what should we do when the CacheControl header on the request is null? At the moment we assign an empty header if it's null which does not seem entirely correct.

Commit that added functionality: 2467a35

Clean up HandleServerResponse

The code in the HandleServerResponse method is a bit hard too follow, we should probably try to clean it up and make it easier to follow.

Fix logo

We need to fix the logo since its edges are a bit pixelated in the larger formats.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.