Skip to content
This repository has been archived by the owner on Jun 25, 2018. It is now read-only.

LykkeCity/Lykke.Rocks.Caching

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rocks.Caching

Rocks.Caching is a library that aimed to provide clean minimal abstraction over caching mechanics for it to be unit test and refactor friendly.

ICacheProvider root interface

The root interface is ICacheProvider which provides minimal method set for caching capabilities:

  • Get item
  • Add item
  • Remove item
  • Clear cache

Built in cache provider implementation

There are couple implementation of the ICacheProvider interface built in package:

  • MemoryCacheProvider - uses System.Runtime.Caching.MemoryCache.
  • DummyCacheProvider - uses dictionary to simply store all items and without any expiration or dependency caching support. Mostly used in unit testing.
  • NullCacheProvider – a Null-object pattern implementation which simply does not store any cache items and always empty. Mostly used in unit testing.

Example of usage

Choose a provider and register it in application root. This is an example to do register cache provider inside web app using SimpleInjector:

public class Global : HttpApplication
{
	protected void Application_Start (object sender, EventArgs e)
	{
		// ...
  		container.RegisterSingle<ICacheProvider> (new MemoryCacheProvider ());
		// ...
	}
}

And now it's easy to use it anywhere. For example, in services:

internal class UsersService : IUsersService
{
	private readonly ICacheProvider cache;


	public UsersService (ICacheProvider cache)
	{
		if (cache == null)
			throw new ArgumentNullException ("cache");
		  
		this.cache = cache;
	}


	public int GetUsersCount ()
	{
		var result = this.cache.Get ("UsersCount",
			                            () => new CachableResult<int> (this.GetUsersCountFromDb (),
			                            		new CachingParameters (TimeSpan.FromMinutes (15))));

		return result;
	}
}

Correct implementation of cache miss handling in multithreaded environment

This issue explained here: Caching in multi thread application – it’s not that simple

For your code to correctly handle cache miss in multithreaded scenarios simply call Get extension method that works with ICacheProvider (and so supports any implementation of it):

public int GetUsersCount ()
{
    var result = this.cache.Get ("UsersCount", () =>
    {
        var users_count = GetUsersCountFromDb ();
        var cp = new CachingParameters (TimeSpan.FromMinutes (15));
 
        return new CachableResult<int> (users_count, cp);
    });
 
    return result;
}

Async is supported:

public async Task<int> GetUsersCountAsync ()
{
    var result = await this.cache
        .GetAsync ("UsersCount", () => Task.Run (async () =>
        {
            var users_count = await GetUsersCountFromDbAsync ();
            var cp = new CachingParameters (TimeSpan.FromMinutes (15));
            return new CachableResult<int> (users_count, cp);
        }));
 
    return result;
}

NuGet package

You can install nuget package for Rocks.Caching here: https://www.nuget.org/packages/Lykke.Rocks.Caching

About

Caching abstraction library.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%