Git Product home page Git Product logo

cache's Introduction

cache

Basic Cache Facade for generic caching purposes with StackExchange.Redis implementation

What is this

This library includes very simple form of basic caching operations such as get, set, delete.

By using this library you can add return value of a function to any implemented caching service.

For the sake of simplicity I only implemented StackExchange.Redis library as a Cache Service. But any other caching infrastructure can be used by just implementing ICacheService interface.

How to use

I simply created some test cases for usage. You can find usage in test files. I explain simple usage below also.

Initial Registrations

First you can create an application cache library like so:

var binarySerializer = new ProtobufBinarySerializer();
var cacheProvider = new BasicCacheProvider(new Dictionary<int, ICacheService>()
{
	{1, new StackExchangeRedisService("localhost:6379") },
	{2, new StackExchangeRedisService("localhost:6380") }
}, binarySerializer);

First dictionary defines services to be concerned. Their first parameters are identifiers of services. And second parameter is given binary serializer for internal serialize/deserialize operations. cacheProvider object most of the time is singleton per application lifestyle.

Execute Function / Set&Get

Then for caching a function return value, you can call ExecuteCached method. By doing this, you both calling function and caching it with given key:

var result = cacheProvider.ExecuteCached(new CacheKey("SomeCacheKey"), 
	() => Guid.NewGuid(), TimeSpan.FromSeconds(10));

Get Only

For Get purpose there is also Get function with type declaration:

var result = cacheProvider.Get<Guid>(new CacheKey("SomeCacheKey"));

Delete

For deleting something from cache you can use ExecuteCached with CacheRule parameter:

cacheProvider.ExecuteCached(new CacheKey("SomeCacheKey"), 
	() => Guid.NewGuid(), CacheRule.Delete());

Bypass and Re-Execute Function Before Timeout Happens

Sometimes you need to re-execute same function and re-set cache value for same key. In this case we can use ExecuteCached like this:

var result = cacheProvider.ExecuteCached(new CacheKey("SomeCacheKey"), 
	() => Guid.NewGuid(), TimeSpan.FromSeconds(10), true);  

Fourth parameter is bypassing the cache and executing given function, then adding it to the cache again.

Explicitly using services

Since this is a basic library, I sometimes need to use the service under the hood. So I can call these services like this:

var cacheServices = cacheProvider.GetServices();

This operation returns IEnumerable<ICacheService> typed object.

Bulk Delete

You can bulk delete keys from each service like this:

foreach(var svc in cacheProvider.GetServices())
{
  svc.ClearCache(new CacheClearQuery(pattern: "SomeRepository::*"));
}

In StackExchangeRedisService implementation, I use Scan and Delete each key.

Using Real Client in code

If you want more functions than this facade library, then you can use real implementation that works under the hood. For example, StackExchange.Redis library ConnectionMultiplexer is working under the StackExchangeRedisService implementation. So when you need ConnectionMultiplexer actually, you can use it like this:

var seRedisSvc = cacheProvider.GetServices()
    .First(svc => 
		svc is CacheServiceBase<ConnectionMultiplexer>) as CacheServiceBase<ConnectionMultiplexer>;
seRedisSvc.CacheEndpoint.GetDatabase().Ping();

Here, ICacheService implementation StackExchangeRedisService is also concrete class of CacheServiceBase. This base class contains CacheEndpoint property as generic variable (in this case ConnectionMultiplexer).

References

cache's People

Contributors

yusufuzun avatar

Watchers

 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.