Giter VIP home page Giter VIP logo

xcache's Introduction

Archived in favor of dotnet core native implementations.

xCache

xCache is a decoupled caching abstraction for .NET.

xCache comes with support for System.Runtime.Caching.MemoryCache out of the box and is designed to be easy to extend with other caching implementations. Like the xCache.Redis implementation.

Installation

The core library is available on nuget

install-package xCache

Usage

Register your preffered caching service to the ICache interface through your DI container.

	//Register
	Ioc.RegisterType<ICache, MemoryCache>();

Resolve ICache and handle your caching needs

	
	var cache = Ioc.Resolve<ICache>();
	var key = "testKey";
	
	var result = cache.Get<string>(key);
	
	if(result == null)
	{
		result = GetResult();
		
		//Cache for 5 minutes
		cache.Add<string>(key, result, new Timespan(0,5,0));
	}

Aop

Because caching is a cross-cutting concern it can be executed with some flavor of Aspect oriented programming (Aop) to promote single responsiblity.

xCache.Aop.Unity scaffolds the IoC interception pattern and provides a caching attribute to enable caching functionality on a method by method basis.

It comes bundled with a Json.Net cache key generator that leverage generic serialization to ensure unique keys are always generated.

Installation

The Aop Unity package is available on nuget

install-package xCache.Aop.Unity

Usage

//Create an interface for interception
public interface ICachedObject
{
    string GetCurrentDateAsStringFiveSecondCache();
}

//Impliment that interface

public class UnityCachedObject : ICachedObject
{
    [Cache(Seconds = 5)]
    public string GetCurrentDateAsStringFiveSecondCache()
    {
        return DateTime.Now.ToString();
    }
}
public class CacheTests 
{
	[Fact]
	public void FiveSecondTest()
	{
		var container = new UnityContainer();
		
		//Register interception
		container.AddNewExtension<Interception>();
		
		//Register xCache
		container.RegisterType<ICache, xCache.MemoryCache>();
		container.RegisterType<ICacheKeyGenerator,JsonCacheKeyGenerator>();
		
		//Register test interface with interception
		container.RegisterType<ICachedObject, UnityCachedObject>(
		    new InterceptionBehavior<PolicyInjectionBehavior>(),
		    new Interceptor<InterfaceInterceptor>());
		
		//Resolve
		var obj = container.Resolve<ICachedObject>();
		
		//Should return the same DateTime string for 5 second intervals
		var cachedDateTimeString = obj.GetCurrentDateAsStringFiveSecondCache();
	}
}

Durable Cache

As of [email protected] you now have the ability to refresh your cache from a background processes without relying on your method being called. To opt into this new feature you will need to register two additional objects:

		//Register xCache Durable Dependencies
		container.RegisterType<IDurableCacheQueue, TimedDurableCacheQueue>(
                new ContainerControlledLifetimeManager(),
                new InjectionFactory((c) => new TimedDurableCacheQueue(c.Resolve<IDurableCacheRefreshHandler>(), new TimeSpan(0,0,30))));
		container.RegisterType<IDurableCacheRefreshHandler, DurableCacheRefreshHandler>();

And add Absolute expiration values to the Cache attribute like so:

	//Cache for 90 seconds refreshing every 15 from the cache queue
	[Cache(Seconds = 15, AbsoluteSeconds = 90)]

When you add an Absolute[Hours|Minutes|Seconds] value you are setting the maximum lifetime for the item to be cached, and using the Seconds, Minutes, or Hours attribute to indicate how often your item should be refreshed within that lifetime.

It is important to note that in the absence of an Absolute value, the Seconds, Minutes and Hours attribute will continue to act as cache expiration times and will only be updated when a cache miss occurs.

The TimedDurableCacheQueue will schedule a refresh of cached items using .NET timers to ensure that your items are refreshed at the specified intevals. It can be overridden with your own prefered implementation.

The DurableCacheRefreshHandler interogates your code to find the correct method and paramters needed to refresh the cache and executed on a seperate background thread.

Automatic Rescheduling

Every time an item is retrieved from durable cache it is checked for staleness. By default a stale itme is defined as an item that has not been updated for at least two refresh cycles. You can customize the maximum staleness by setting the MaximumStaleness[Hours|Minutes|Seconds] values.

Caching Tiers

As of [email protected] you can now specify multiple caching tiers each with thier own lifetimes.

You will need to overload the registration of ICache with your desired caching implementations:

//Register multiple tiers by name
container.RegisterType<ICache, MemoryCache>();
container.RegisterType<ICache, RedisCache>("Redis");

Then add the appropriate cache attributes:

[Cache(Minutes = 1, Order = 1)]
[Cache(Minutes = 30, CacheName = "Redis", Order = 2)]
public string GetCurrentDateAsString()
{
	return DateTime.Now.ToString();
}

Version

  • xCache 0.2.2
  • xCache.Aop.Unity 0.5.0

License

MIT

xcache's People

Contributors

chriseby avatar donlockhart avatar jarlotee avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  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.