Current Version: 1.2.0
Project Information 2346
Licensed under the Apache License, Version 2.0
var manager = CacheFactory.Build<string>(
p => p.WithSystemRuntimeCacheHandle());
manager.AddOrUpdate("key", "value", _ => "updated value");
var val = manager.Get("key");
manager.Put("key", "another value");
A simple but very efficient implementation. This handle is faster than any other in most scenarios and works cross platform.
Two implementations are available, based on System.Runtime.Caching
and
based on the new Microsoft.Extensions.Caching.Memory package.
Redis can be used as an extremely fast and reliable out of process (or distributed) solution.
The Redis implementation is based on the popular StackExchange.Redis package.
Another great distributed solution is memcached, which can be used as a layer.
Couchbase is primarily a document database but can also be used for simple caching.
ASP.NET comes with its own in-process memory cache which can be used as one layer.
Our Web NuGet package also comes with a custom OutputCacheProvider.
Caching objects or primitives is easy, keep writing your code in a type safe way and you don't have to care about casts or serialization issues.
There are diverse options to configure CacheManager:
The topology can be configured. To, for example, implement a local first level cache in front of a distributed second level, for extremely fast read access.
In distributed scenarios, updating a value must often be handled with care because of concurrency. CacheManager hides the complexity and lets you implement all that in one line of code.
The serialization technology, used to serialize values in distributed scenarios, can be configured.
Choose either Binary-, JSON- or Protocol Buffer serialization for example.
When using multiple layers in conjunction with a distributed cache layer, and you have many instances of your app running, you might want the local instances synchronized. The backplane can automatically handle that.
Logging can be added to the configuration to log some or all the operations.
Install the Microsoft.Extensions.Logging package, to leverage all the additional features.
Using the new .NET Core / ASP.NET Core project structure and libraries, CacheManager now has cross platform support and is tested on Windows, Linux and iOS.
The CacheManager interface also provides events, you can subscribe to, for many operations.
Through configuration, you can set a default expiration mode and time for each layer.
The expiration settings can also individually be changed for each item at any point.
Specify a region for an item to group keys together and, for example, remove all of them at once.
Gather statistical information and track caching operations in Performance Monitor as needed.
CacheManager comes in many different packages, separating the features and dependencies, which gives you the option and flexibility to install only the features you need.
CacheManager is easy to use and start with, but it is also just a few lines of configuration to use a more complex caching strategy without you having to change your code.
Start with a simple in-memory approach and scale out later to Redis or other distributed solutions!
File an issue, suggestion or feature request, or send a PR on GitHub!
You can post any technical questions on Stack Overflow.
Or just leave a message in the comments below!.
Many articles and code samples are available on this site (and more to come...)
The following three examples do actually create the same CacheManager instance. You can decide depending on the situation which method fits your needs.
The first example uses the CacheFactory
.
var manager = CacheFactory.Build<string>(
p => p.WithSystemRuntimeCacheHandle());
Instead you can also create an instance of ConfigurationBuilder
...
var config = new ConfigurationBuilder()
.WithSystemRuntimeCacheHandle()
.Build();
var manager = new BaseCacheManager<string>(config);
// or
var manager = CacheFactory.FromConfiguration<string>(config);
Or you instantiate the CacheManagerConfiguration
and call the Builder
property
to modify the configuration.
var manager = new BaseCacheManager<string>(
new CacheManagerConfiguration()
.Builder
.WithSystemRuntimeCacheHandle()
.Build());
This example uses the Microsoft.Extensions.Configuration and the CacheManager extensions.
var jsonConfiguration =
new Microsoft.Extensions.Configuration.ConfigurationBuilder()
.AddJsonFile("cache.json")
.Build();
var manager = new BaseCacheManager<string>(
jsonConfiguration.GetCacheConfiguration());
This is a more complex configuration using two layers of in-memory and distributed caching. It also uses a cache backplane.
var manager = CacheFactory.Build<int>(settings =>
{
settings
.WithSystemRuntimeCacheHandle()
.And
.WithRedisConfiguration("redis", config =>
{
config.WithAllowAdmin()
.WithDatabase(0)
.WithEndpoint("localhost", 6379);
})
.WithMaxRetries(100)
.WithRetryTimeout(50)
.WithRedisBackplane("redis")
.WithRedisCacheHandle("redis", true);
});
manager.Add("test", 123456);
If the packages are installed, you can use the new Microsoft.Extensions.Logging framework to let CacheManager log common messages and errors to any kind of log targets.
var config = new ConfigurationBuilder()
.WithMicrosoftLogging(l => l.AddConsole(LogLevel.Information))
.WithSystemRuntimeCacheHandle()
.Build();