Skip to content

Pavalisoft.Caching is an open source single caching abstraction layer for both MemoryCache and DistributedCache APIs. Its simple and easy to code CacheManager supports various cache providers with simple configuration.

License

Notifications You must be signed in to change notification settings

vaibhavdw/Caching

 
 

Repository files navigation

Caching

Pavalisoft.Caching is an open source caching extension for .NET Standard written in C#, which provides single unified API for both MemoryCache and DistributedCache implementations.

The main goal of the Pavalisoft.Caching package is to make developer's life easier to handle even very complex caching scenarios and concentrate on functionality. It's additional feature CacheManager supports various cache providers and implements many advanced features which can be used in single project/application.

With Pavalisoft.Caching, it is possible to implement multiple layers of caching with multiple cache providers in one place, e.g. MemoryCache and DistributedCache, in just a few lines of code.

The below diagram explains the Pavalisoft.Caching API and its usage.

Documentation & Samples

Complete Documentation is available at https://pavalisoft.github.io/Caching/ for Pavalisoft.Caching API Refer https://github.com/pavalisoft/Caching/tree/master/Samples for reference implementations

Usage

  1. Define the Cache Stores and Partitions in Caching configuration section in appSettings.json.
{
  "Caching": {
    "Stores": [
      {
        "Name": "InMemory",
        "Type": "Pavalisoft.Caching.InMemory.InMemoryCacheStoreType, Pavalisoft.Caching.InMemory",
        "StoreConfig": "{\"ExpirationScanFrequency\":\"00:05:00\"}"
      },
      {
        "Name": "DistributedInMemory",
        "Type": "Pavalisoft.Caching.InMemory.MemoryDistributedCacheStoreType,Pavalisoft.Caching.InMemory",
        "SerializerType": "Pavalisoft.Caching.Serializers.JsonSerializer,Pavalisoft.Caching",
        "StoreConfig": "{\"ExpirationScanFrequency\":\"00:05:00\"}"
      },
      {
        "Name": "SqlServer",
        "Type": "Pavalisoft.Caching.SqlServer.SqlServerDistributedCacheStoreType,Pavalisoft.Caching.SqlServer",
        "SerializerType": "Pavalisoft.Caching.Serializers.JsonSerializer,Pavalisoft.Caching",
        "StoreConfig": "{\"ExpiredItemsDeletionInterval\":\"00:05:00\", \"ConnectionString\":\"Data Source=localhost;Initial Catalog=DistributedCache;Integrated Security=True\", \"SchemaName\":\"store\", \"TableName\":\"Cache\", \"DefaultSlidingExpiration\":\"00:05:00\"}"
      },
      {
        "Name": "MySql",
        "Type": "Pavalisoft.Caching.MySql.MySqlDistributedCacheStoreType,Pavalisoft.Caching.MySql",
        "SerializerType": "Pavalisoft.Caching.Serializers.JsonSerializer,Pavalisoft.Caching",
        "StoreConfig": "{\"ExpiredItemsDeletionInterval\":\"00:05:00\", \"ConnectionString\":\"Data Source=localhost:9001;Initial Catalog=DistributedCache;Integrated Security=True\", \"SchemaName\":\"store\", \"TableName\":\"Cache\", \"DefaultSlidingExpiration\":\"00:05:00\"}"
      },
      {
        "Name": "Redis",
        "Type": "Pavalisoft.Caching.Redis.RedisDistributedCacheStoreType,Pavalisoft.Caching.Redis",
        "SerializerType": "Pavalisoft.Caching.Serializers.JsonSerializer,Pavalisoft.Caching",
        "StoreConfig": "{\"Configuration\":\"00:05:00\", \"InstanceName\":\"localhost\"}"
      }
    ],
    "Partitions": [
      {
        "Name": "FrequentData",
        "StoreName": "InMemory",
        "SlidingExpiration": "00:05:00"
      },
      {
        "Name": "DistibutedFrequentData",
        "StoreName": "DistributedInMemory",
        "SlidingExpiration": "00:05:00"
      },
      {
        "Name": "MySqlLocalizationData",
        "StoreName": "MySql",
        "Priority": "NeverRemove"
      },
      {
        "Name": "LocalizationData",
        "StoreName": "SqlServer",
        "Priority": "NeverRemove"
      },
      {
        "Name": "MasterData",
        "StoreName": "Redis",
        "SlidingExpiration": "00:05:00"
      }
    ]
  }
}
  1. Add Pavalisoft.Caching to services
...
//Import the below namespace to use InMemory and DitributedInMemory cache store implementations
using Pavalisoft.Caching.InMemory;
//Import the below namespace to use MySql cache store implementation
using Pavalisoft.Caching.MySql;
//Import the below namespace to use Redis Cache Store implementation
using Pavalisoft.Caching.Redis;
//Import the below namespace to use SqlServer Cache Store implementation
using Pavalisoft.Caching.SqlServer;
...

namespace Pavalisoft.Caching.Sample
{
    public class Startup
    {
		...
        public Startup(IConfiguration configuration)
        {
			...
            Configuration = configuration;
			...
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            ...
            // Adds CacheManager servcice to services
            services.AddCaching()
                // Adds InMemory and Distributed InMemory Cache Store implementations to CacheManager
                .AddInMemoryCache()
                // Adds MySql Cache Store implementations to CacheManager
                .AddMySqlCache()
                // Adds Redis Cache Store implementations to CacheManager
                .AddRedisCache()
                // Adds SqlServer Cache Store implementations to CacheManager
                .AddSqlServerCache();
			...
        }
		...
    }
}
  1. Use CacheManager methods to add, get, refresh and remove items in Cache.
// Add required using statements
using Pavalisoft.Caching;
using Pavalisoft.Caching.Interfaces;
using Microsoft.Extensions.Primitives;
using System;

namespace Pavalisoft.Caching.Sample
{
	public class CachingSample
	{
		private const string CachePartitionName = "FrequentData";		
		private readonly ICacheManager _cacheManager;
		public CachingSample(ICacheManager cacheManager)
		{
			_cacheManager = cacheManager;
		}

		public AppUser GetAppUser(HttpContext httpContext)
		{
			var userName = httpContext.User.Identity.Name;
			AppUser appUser;
			
			// Try to get the appUser from cache
			if (!_cacheManager.TryGetValue(CachePartitionName, userName, out appUser))
			{
				// If not available in Cache then create new instance of AppUser
				appUser = new AppUser(userName);

				// Add appUser object to Cache
				_cacheManager.Set(CachePartitionName, userName, appUser);                `
			}
			return appUser;
		}
	}
}

Serializers

Below are the list of serializers options available, which can be used to serialize and deserialize object from and to Distributed Cache Stores.

  • BinaryFormatter - Requires the classes decoarated with SerializableAttribute to store into Distributed Cache Stores.
  • Newtonsoft.Json - Uses Newtonsoft.Json to serialize a class without SerializableAttribute.

Builds

Get latest builds from nuget

Package Version
Pavalisoft.Caching 1.2
Pavalisoft.Caching.TagHelpers 1.0
Pavalisoft.Caching.InMemory 1.0
Pavalisoft.Caching.Redis 1.0
Pavalisoft.Caching.MySql 1.0
Pavalisoft.Caching.SqlServer 1.0

Contributing

Getting started with Git and GitHub

Once you're familiar with Git and GitHub, clone the repository and start contributing.

About

Pavalisoft.Caching is an open source single caching abstraction layer for both MemoryCache and DistributedCache APIs. Its simple and easy to code CacheManager supports various cache providers with simple configuration.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 83.4%
  • C# 9.5%
  • JavaScript 5.7%
  • CSS 1.4%