Skip to content

Repository files navigation

AnyCache

.Net cache framework for connecting to any caches including In Memory cache and Redis from dotnet, dotnet-core and Xamarin forms.

License

AnyCache is library for having common cache API for .Net framework and .Net Core as well as Xamarin Forms projects. As you know, the MemoryCache API in .Net freamwork is not same as MemoryCache in .Net Core and we couldn't easly transfer our code between them. By AnyCache we could have one code base that could be reused in diffrent type of projects without any code change. AnyCache also support Redis cache whith same API as MemoryCache, so we could swap our project caching from MemoryCache to Redis cache and vice versa whithout any major code change. I have plan to add more type of chaching in the future.

Features

  • Same API for .Net Framework, .Net Core projects and Xamarin Forms projects
  • Same API for any type of caching including In Memory cache and Redis cache.

Basic usage

cache.Set("key",123000);varret=cache.Get<int?>("key");
Personobj=newPerson("Tom","Hanks");cache.Set(key,obj);Personret=cache.Get<Person>(key);

Installation

AnyCache is available as a nuget package and supports In Memory and Redis cache at the moment.

In Memory Cache

For creating in memory cache use following nuget command to add library to your project:

PM> Install-Package AnyCache.InMemory

Then use following code to create In Memory cache instance.

varcache=newInMemoryCache();

Redis Cache

For creating Redis cache use following nuget command to add library to your project:

PM> Install-Package AnyCache.Redis
PM> Install-Package AnyCache.Serialization

Then use following code to create Redis cache instance.

ISerializerserializer=newJsonSerializer(null,true);varcache=newRedisCache(connectionString:"localhost",serializer:serializer);

Dependency Injection

AnyCache supports Dependency Injection. Following example shows how to use AnyCache by .Net Core injection mechanism.

In Memory Cache

Remember to register InMemoryCache as singleton to make sure using same cache instance in whole of your project.

service.AddSingleton<IAnyCache>(s =>{returnnewInMemoryCache();;});

Redis Cache

service.AddSingleton<IAnyCache>(s =>{ISerializerserializer=newJsonSerializer(null,true);returnnewRedisCache(connectionString:"localhost",serializer:serializer);});

Now you can inject AnyCache at runtime into your services/controllers:

publicclassEmployeesController{privatereadonlyIAnyCache_cache;publicEmployeesController(IAnyCachecache){_cache=cache;}// use _cache.Set or _cache.Get}

List of available methods

objectthis[stringkey]{get;set;}boolAdd(stringkey,objectvalue,DateTimeOffset?absoluteExpiration=null);boolAdd<T>(stringkey,Tvalue,DateTimeOffset?absoluteExpiration=null);boolAdd(stringkey,objectvalue,TimeSpanslidingExpiration);boolAdd<T>(stringkey,Tvalue,TimeSpanslidingExpiration);voidSet(stringkey,objectvalue,DateTimeOffset?absoluteExpiration=null);voidSet<T>(stringkey,Tvalue,DateTimeOffset?absoluteExpiration=null);voidSet(stringkey,objectvalue,TimeSpanslidingExpiration);voidSet<T>(stringkey,Tvalue,TimeSpanslidingExpiration);boolContains(stringkey);objectGet(stringkey);TGet<T>(stringkey);TGetValueOrDefault<T>(stringkey,Tvalue);Task<object>GetAsync(stringkey);Task<T>GetAsync<T>(stringkey);objectGetValueOrAdd(stringkey,objectvalue,DateTimeOffset?absoluteExpiration=null);TGetValueOrAdd<T>(stringkey,Tvalue,DateTimeOffset?absoluteExpiration=null);objectGetValueOrAdd(stringkey,objectvalue,TimeSpanslidingExpiration);TGetValueOrAdd<T>(stringkey,Tvalue,TimeSpanslidingExpiration);objectGetValueOrAdd(stringkey,Func<object>retriever,DateTimeOffset?absoluteExpiration=null);TGetValueOrAdd<T>(stringkey,Func<T>retriever,DateTimeOffset?absoluteExpiration=null);objectGetValueOrAdd(stringkey,Func<object>retriever,TimeSpanslidingExpiration);TGetValueOrAdd<T>(stringkey,Func<T>retriever,TimeSpanslidingExpiration);IEnumerable<KeyValuePair<string,object>>GetAll(IEnumerable<string>keys);IEnumerable<KeyValuePair<string,T>>GetAll<T>(IEnumerable<string>keys);objectRemove(stringkey);TRemove<T>(stringkey);longGetCount();IEnumerator<KeyValuePair<string,object>>GetEnumerator();voidClearCache();voidCompact();

About

.Net cache framework for connecting to any caches including in memory cache and Redis from dotnet, dotnet-core and Xamarin forms.

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages