Giter Site home page Giter Site logo

mbcache's Introduction

MbCache, or Method based Cache, is a memoization library for .net. It is used to define certain methods to be cached in a centralized way, independent on the underlying caching framework.

How often have you written c# similar to this?

public class Calculator : ICalculator
{
 public SomeType Calculate()
 {
   var cachedValue = tryGetFromCache(yourKey);
   if(cachedValue != null)
     return cachedValue;
   var value = someExpensiveOperation();
   putInCache(yourKey, value);
   return value;
 }
 [...]
}

It sure works. There are some drawback though:

  • A snippet like this tends to repeat itself the next time you want to cache some return value in some other method. Not really DRY-ish.
  • You (might) have a strong dependency to your cache framework. With code like this it's not easy to reuse code amoung different sort of clients. You might, for example, want to use asp.net cache when code is run in web enviroment but something completely different in a desktop application.
  • What if you suddenly want to turn off caching for a method? Or some of them? All of them?
  • SRP is broken. It can eg be solved using decorator pattern and split calculation and caching part, but it will lead to even more handwritten code.
  • Can be hard to test.

There's an alternative that solves this for you - MbCache!

  1. Write your business logic. No need to mess this up with cache logic.

    public SomeType Calculate()
    {
      return someExpensiveOperation();  
    }
    
  2. Tell MbCache at start up what methods to cache (preferably together with your favorite ioc container).

    var builder = new CacheBuilder(new LinFuProxyFactory());
    builder.For<Calculator>()
      .CacheMethod(c => c.Calculate())
      .As<ICalculator>();
    _factory = builder.BuildFactory();
    
  3. Use your objects. ICalculate.Calculate() will now use caching.

    var yourService =_factory.Create<ICalculator>()
    var value = yourService.Calculate();
    

mbcache's People

Contributors

rogerkratz avatar mattes75 avatar blandine avatar

Watchers

Phil Tremblay avatar James Cloos avatar

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.