Caching

The ICacheService interface defines a standard set of operations for caching mechanisms. It provides methods for asynchronous and synchronous retrieval and storage of cache items, allowing for both timespan-based and datetime-offset-based expiration. Additionally, it offers a mechanism to verify the existence of a specific cache key. Implement this interface to create consistent caching providers for different storage backends.

/// <summary>
/// Common cache provider interface
/// </summary>
public interface ICacheService
{
    /// <summary>
    /// Gets cache value
    /// </summary>
    /// <typeparam name="T">The type of result object</typeparam>
    /// <param name="cacheKey">Cache key</param>
    /// <returns>Object</returns>
    T Get<T>(string cacheKey);
    
    /// <summary>
    /// Gets cache value asynchronous
    /// </summary>
    /// <typeparam name="T">The type of result object</typeparam>
    /// <param name="cacheKey">Cache key</param>
    /// <returns>Object</returns>
    Task<T> GetAsync<T>(string cacheKey);
    
    /// <summary>
    /// Sets cache value asynchronous
    /// </summary>
    /// <typeparam name="T">The type of source object</typeparam>
    /// <param name="cacheKey">Cache key</param>
    /// <param name="value">Object value</param>
    /// <param name="expiresIn">Expires after given value</param>
    void Set<T>(string cacheKey, T value, TimeSpan? expiresIn = null);
    
    /// <summary>
    /// Sets cache value asynchronous
    /// </summary>
    /// <typeparam name="T">The type of source object</typeparam>
    /// <param name="cacheKey">Cache key</param>
    /// <param name="value">Object value</param>
    /// <param name="expiresIn">Expires after given value</param>
    Task SetAsync<T>(string cacheKey, T value, TimeSpan? expiresIn = null);
    
    /// <summary>
    /// Sets cache value synchronous
    /// </summary>
    /// <typeparam name="T">The type of source object</typeparam>
    /// <param name="cacheKey">Cache key</param>
    /// <param name="value">Object value</param>
    /// <param name="expiresOn">Expires on given date time offset</param>
    void Set<T>(string cacheKey, T value, DateTimeOffset expiresOn);
    
    /// <summary>
    /// Sets cache value asynchronous
    /// </summary>
    /// <typeparam name="T">The type of source object</typeparam>
    /// <param name="cacheKey">Cache key</param>
    /// <param name="value">Object value</param>
    /// <param name="expiresOn">Expires on given date time offset</param>
    Task SetAsync<T>(string cacheKey, T value, DateTimeOffset expiresOn);
    
    /// <summary>
    /// Removes cache key
    /// </summary>
    /// <param name="cacheKey">Cache key</param>
    void Delete(string cacheKey);
    
    /// <summary>
    /// Removes cache key
    /// </summary>
    /// <param name="cacheKey">Cache key</param>
    Task DeleteAsync(string cacheKey);

    /// <summary>
    /// Checks given key is exist in cache or not
    /// </summary>
    /// <param name="cacheKey">Cache key</param>
    /// <returns>true/false</returns>
    bool IsExists(string cacheKey);
}

Last updated