Caching
Coravel provides you with an easy to use API for caching in your .NET Core applications.
By default, it uses an in-memory cache.
Coravel also provides some database drivers for more robust scenarios.
Config
In Startup.ConfigureServices()
:
services.AddCache();
This will enable in-memory (RAM) caching.
To use caching, inject Coravel.Cache.Interfaces.ICache
via dependency injection.
private ICache _cache;
public CacheController(ICache cache)
{
this._cache = cache;
}
Methods
Remember
Remember
will remember your cached item for the duration you specify.
Remember
requires that you specify:
- A cache key for this specific item
- A method that returns the data you want cached (of any type)
- A
TimeSpan
to indicate how long the item will be cached
For example:
string BigDataLocalFunction()
{
return "Some Big Data";
};
this._cache.Remember("BigDataCacheKey", BigDataLocalFunction, TimeSpan.FromMinutes(10));
TIP
Multiple Calls Does Not Invalidate Cache
Calling Remember
a second (or more times) will not invalidate the cache. The cached items will last as long as your code specified when using this method. This enables the usage of the method to be simple and easy.
RememberAsync
It's Remember
, but async:
async SomeType BigDataLocalFunctionAsync()
{
// ... Doing some stuff ...
return await SomeCostlyDbCall();
};
await this._cache.RememberAsync("BigDataCacheKey", BigDataLocalFunctionAsync, TimeSpan.FromMinutes(10));
Note about cache invalidation from Remember
applies.
Forever
Similar to Remember
, but your item will be cached indefinitely.
this._cache.Forever("BigDataCacheKey", BigDataLocalFunction);
Note about cache invalidation from Remember
applies.
ForeverAsync
It's Forever
, but async:
await this._cache.ForeverAsync("BigDataCacheKey", BigDataLocalFunctionAsync);
Note about cache invalidation from Remember
applies.
HasAsync
Will tell you if a non-expired key exists in the cache.
bool hasKey = await this._cache.HasAsync("BigDataCacheKey");
GetAsync
Get a specific value from the cache.
WARNING
Will throw an exception if the key has expired. This is usually used in conjunction with HasAsync
.
string bigDataValue = await this._cache.GetAsync<string>("BigDataCacheKey");
Flush
Flush
will clear your entire cache.
this._cache.Flush();
Forget
Forget
will clear a specific cache entry by key.
this._cache.Forget("BigDataCacheKey");
Database Drivers
By default, an in-memory caching driver is used. Coravel also offers drivers that will cache your data to a database!
TIP
This allows multiple servers or application instances (e.g. load-balancing, etc.) to share the same cache. It can also alleviate the extra memory required by your application instance(s) when using the in-memory driver.
SQL Server
Install the NuGet package Coravel.Cache.SQLServer
.
Next, in Startup.cs
in the ConfigureServices()
method:
services.AddSQLServerCache(connectionString);
The driver will automatically create the table dbo.CoravelCacheStore
to be used for caching.
PostgreSQL
Install the NuGet package Coravel.Cache.PostgreSQL
.
Next, in Startup.cs
in the ConfigureServices()
method:
services.AddPostgreSQLCache(connectionString);
The driver will automatically create the table public.CoravelCacheStore
to be used for caching.
Custom Drivers
If you wish, you can create your own cache driver that extends Coravel.Cache.Interfaces.ICache
. Maybe you want to use start using a Redis store?
First, implement a class that extends the ICache
interface.
Next, to register your driver to be used, just pass it into the AddCache
method:
services.AddCache(new RedisCache());
// Or, if you need the service provider to create your object:
services.AddCache(provider => new RedisCache(provider.GetService<ISomeRegisteredInterface>()));