Git Product home page Git Product logo

thoughtstuff.caching's Introduction

ThoughtStuff.Caching

Injects look-through cache for configured services

Quick Start

// Cache results returned from MySlowService in Mem Cache
services.AddMethodCaching()
        .AddTransientWithCaching<IMySlowService, MySlowService, MyResult>();

var app = builder.Build();

// Configure method caching policies
var methodCachePolicies = app.Services.GetRequiredService<IMethodCacheOptionsLookup>();
methodCachePolicies.AddRelativeExpiration<IMySlowService>(TimeSpan.FromSeconds(30));

Details

Suppose you have a slow service that fetches information on books by ISBN.

public interface IBookInfoService
{
    BookInfo GetBookInfo(string isbn);
}

class BookInfoService : IBookInfoService
{
    public BookInfo GetBookInfo(string isbn) { ... }
}

The service is slow and book information changes infrequently, so you would like to cache it. Rather than registering BookInfoService with .AddTransient you can use .AddTransientWithCaching.

services.AddMethodCaching()
        .AddTransientWithCaching<IBookInfoService, BookInfoService, BookInfo>();

The above will register a caching proxy for IBookInfoService which will wrap BookInfoService. The first call to get info on a particular book will use BookInfoService, but subsequent calls to get info on the same book will return the cached result from the first call.

void Example([Inject] IBookInfoService bookInfoService)
{
    var info1 = bookInfoService.GetBookInfo("1234");    // Slow. Saves cache entry using key "GetBookInfo(1234)"
    var info2 = bookInfoService.GetBookInfo("1234");    // Fast. Returns cache entry found using key "GetBookInfo(1234)"
    Assert.Equals(info1, info2);
}

Current Limitations

Single Return Type per Interface

Only one return type can be cached per service interface. This works well for services that implement a Single Responsibility, but not for larger classes. Often times simplified interface(s) can be created. The simplified interfaces may only have 1 function each and all be implemented by the large class.

Transient Lifetime

Only the Transient lifetime is implemented.

Cache Key uses Interface Name + Method Name + Arguments

A cache key is automatically generated from the method invocation based on the method name and passed arguments. For example: "IMyBookService.GetBookInfo('The Great Gatsby')". Other environment information is not included.

Be careful in multi-user or multi-tenant environments not to cache user-specific or tenant-specific results. For example, caching a method like GetCurrentUserFavoriteColor() would end up returning the same color for all users. To make it amenable to method caching change it to GetUserFavoriteColor(userId).


The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination. Few media of creation are so flexible, so easy to polish and rework, so readily capable of realizing grand conceptual structures.

— Fred P. Brooks

thoughtstuff.caching's People

Contributors

jfoshee avatar

Stargazers

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

thoughtstuff.caching's Issues

Add simple entry removal

So a more simple implementation than DeleteMatchingEntries(string keyWildcardExpression) is possible.

Cannot dynamically create an instance of type `ThoughtStuff.Caching.Azure.AzureCachingOptions`.

The options pattern seems to require a parameterless constructor for AzureCachingOptions

Message:

System.MissingMethodException : Cannot dynamically create an instance of type ThoughtStuff.Caching.Azure.AzureCachingOptions. Reason: No parameterless constructor defined.

Stack Trace:

ActivatorCache.ctor(RuntimeType rt)
RuntimeType.CreateInstanceOfT()
Activator.CreateInstance[T]()
OptionsFactory`1.CreateInstance(String name)
OptionsFactory`1.Create(String name)
UnnamedOptionsManager`1.get_Value()
BlobStorageService.GetBlobContainerClientBlocking()
BlobStorageService.GetBlobClientBlocking(String blobName)
BlobStorageService.Exists(String blobName)

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.