Implementing Cache Management in Your Application

This framework provides comprehensive caching capbailities built on the open-source CacheManager library. It offers both global and tenant-specific caching strategies, with the default system cache implementation configurable to support alternaitve backends such as Redis or in-memory storage.

Open-source library: https://github.com/MichaCo/CacheManager

Configuration

The following demonstrates the default cache configuration, typically defined within the Web.config file under the cacheManager element:

<!-- Cache Configuration -->

<cacheManager xmlns="http://tempuri.org/CacheManagerCfg.xsd">

    <managers>

        <cache name="primaryCache" updateMode="Up" enableStatistics="true" enablePerformanceCounters="true">

            <handle ref="sysCache" name="defaultHandle" expirationMode="Absolute" timeout="3600s"/>

        </cache>

    </managers>

    <cacheHandles>

        <handleDef id="sysCache" type="CacheManager.SystemRuntimeCaching.MemoryCacheHandle`1, CacheManager.SystemRuntimeCaching" />

    </cacheHandles>

</cacheManager>

For detailed configuration options, please refer to the official documentation: http://cachemanager.net/Documentation/Index/cachemanager_configuration

Core Methods

The cache manager provides the following primary methods for cache operations:

/// <summary>

/// Retrieves a cached value

/// </summary>

/// <typeparam name="T">Data type of the cached value</typeparam>

/// <param name="key">Cache identifier</param>

/// <returns>The cached value</returns>

public T Retrieve<T>(string key)

/// <summary>

/// Retrieves a cached value for a specific tenant

/// </summary>

/// <typeparam name="T">Data type of the cached value</typeparam>

/// <param name="key">Cache identifier</param>

/// <param name="tenantKey">Tenant identifier. If null, the framework automatically resolves the current tenant ID</param>

/// <returns>The cached value</returns>

public T RetrieveByTenant<T>(string key, string tenantKey = null)

/// <summary>

/// Stores or updates a cache entry

/// </summary>

/// <typeparam name="T">Data type of the value to cache</typeparam>

/// <param name="key">Cache identifier</param>

/// <param name="value">The value to store</param>

public void Store<T>(string key, T value)

/// <summary>

/// Stores or updates a cache entry with custom expiration

/// </summary>

/// <typeparam name="T">Data type of the value to cache</typeparam>

/// <param name="key">Cache identifier</param>

/// <param name="value">The value to store</param>

/// <param name="expiration">Custom expiration interval</param>

public void Store<T>(string key, T value, TimeSpan expiration)

/// <summary>

/// Stores or updates a cache entry for a specific tenant

/// </summary>

/// <typeparam name="T">Data type of the value to cache</typeparam>

/// <param name="key">Cache identifier</param>

/// <param name="value">The value to store</param>

/// <param name="tenantKey">Tenant identifier. If null, the framework automatically resolves the current tenant ID</param>

public void StoreByTenant<T>(string key, T value, string tenantKey = null)

/// <summary>

/// Stores or updates a cache entry for a specific tenant with custom expiration

/// </summary>

/// <typeparam name="T">Data type of the value to cache</typeparam>

/// <param name="key">Cache identifier</param>

/// <param name="value">The value to store</param>

/// <param name="expiration">Custom expiration interval</param>

/// <param name="tenantKey">Tenant identifier. If null, the framework automatically resolves the current tenant ID</param>

public void StoreByTenant<T>(string key, T value, TimeSpan expiration, string tenantKey = null)

/// <summary>

/// Removes a cache entry

/// </summary>

/// <param name="key">Cache identifier</param>

/// <returns>True if removal was successful</returns>

public bool Delete<T>(string key)

/// <summary>

/// Removes a cache entry for a specific tenant

/// </summary>

/// <param name="key">Cache identifier</param>

/// <param name="tenantKey">Tenant identifier. If null, the framework automatically resolves the current tenant ID</param>

/// <returns>True if removal was successful</returns>

public bool DeleteByTenant<T>(string key, string tenantKey = null)

/// <summary>

/// Clears all cache entries of the specified type

/// </summary>

/// <typeparam name="T">Data type of cached values to clear</typeparam>

public void Flush<T>()

/// <summary>

/// Clears all cache entries of the specified type for a specific tenant

/// </summary>

/// <typeparam name="T">Data type of cached values to clear</typeparam>

/// <param name="tenantKey">Tenant identifier. If null, the framework automatically resolves the current tenant ID</param>

public void FlushByTenant<T>(string tenantKey = null)

Usage Examples

Obtaining the cache intsance:

using Magicodes.WeiChat.Infrastructure.Cache;

var cache = CacheManager.Current;

Retrieving values (global scope):

var data = cache.Retrieve<int>("StatisticsCount");

Retrieving values (tenant-specific):

var data = cache.RetrieveByTenant<int>("StatisticsCount");

Storing values (global scope):

cache.Store("StatisticsCount", data);

cache.Store("StatisticsCount", data, TimeSpan.FromHours(2));

Storing values (tenant-specific):

cache.StoreByTenant("StatisticsCount", data);

cache.StoreByTenant("StatisticsCount", data, TimeSpan.FromHours(2));

Tags: cache-management CacheManager Redis ASP.NET Caching

Posted on Thu, 20 Aug 2026 16:14:29 +0000 by bapan