Making.MemoryCache.Abstractions 1.0.1-preview

This is a prerelease version of Making.MemoryCache.Abstractions.
dotnet add package Making.MemoryCache.Abstractions --version 1.0.1-preview
                    
NuGet\Install-Package Making.MemoryCache.Abstractions -Version 1.0.1-preview
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Making.MemoryCache.Abstractions" Version="1.0.1-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Making.MemoryCache.Abstractions" Version="1.0.1-preview" />
                    
Directory.Packages.props
<PackageReference Include="Making.MemoryCache.Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Making.MemoryCache.Abstractions --version 1.0.1-preview
                    
#r "nuget: Making.MemoryCache.Abstractions, 1.0.1-preview"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Making.MemoryCache.Abstractions@1.0.1-preview
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Making.MemoryCache.Abstractions&version=1.0.1-preview&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Making.MemoryCache.Abstractions&version=1.0.1-preview&prerelease
                    
Install as a Cake Tool

Making.MemoryCache.Abstractions

Memory cache abstractions and interfaces for the Making framework.

Overview

Making.MemoryCache.Abstractions provides the core abstractions and interfaces for memory caching in the Making framework. It defines contracts for cache services, statistics, and models that can be implemented by various caching providers.

Features

  • Cache Service Interface: Core caching service abstraction
  • Cache Statistics: Performance metrics and cache hit/miss tracking
  • Cache Models: Common cache entry and metadata models
  • Provider Abstraction: Abstraction layer for different cache implementations
  • Async Support: Full async/await support for cache operations

Installation

dotnet add package Making.MemoryCache.Abstractions

Usage

Cache Service Interface

public interface IMemoryCacheService
{
    Task<T> GetAsync<T>(string key);
    Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> factory, TimeSpan? expiration = null);
    Task SetAsync<T>(string key, T value, TimeSpan? expiration = null);
    Task RemoveAsync(string key);
    Task RemoveByPatternAsync(string pattern);
    Task ClearAsync();
    Task<CacheStatistics> GetStatisticsAsync();
    Task<bool> ExistsAsync(string key);
}

Implementing Custom Cache Provider

public class CustomCacheService : IMemoryCacheService
{
    public async Task<T> GetAsync<T>(string key)
    {
        // Custom implementation
        throw new NotImplementedException();
    }
    
    public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> factory, TimeSpan? expiration = null)
    {
        var item = await GetAsync<T>(key);
        if (item != null)
        {
            return item;
        }
        
        var value = await factory();
        await SetAsync(key, value, expiration);
        return value;
    }
    
    public async Task SetAsync<T>(string key, T value, TimeSpan? expiration = null)
    {
        // Custom implementation
        throw new NotImplementedException();
    }
    
    public async Task RemoveAsync(string key)
    {
        // Custom implementation
        throw new NotImplementedException();
    }
    
    public async Task RemoveByPatternAsync(string pattern)
    {
        // Custom implementation
        throw new NotImplementedException();
    }
    
    public async Task ClearAsync()
    {
        // Custom implementation
        throw new NotImplementedException();
    }
    
    public async Task<CacheStatistics> GetStatisticsAsync()
    {
        return new CacheStatistics
        {
            HitCount = 100,
            MissCount = 20,
            TotalRequests = 120,
            HitRatio = 0.83,
            ItemCount = 50
        };
    }
    
    public async Task<bool> ExistsAsync(string key)
    {
        var item = await GetAsync<object>(key);
        return item != null;
    }
}

Cache Models

public class CacheStatistics
{
    public long HitCount { get; set; }
    public long MissCount { get; set; }
    public long TotalRequests { get; set; }
    public double HitRatio { get; set; }
    public long ItemCount { get; set; }
    public DateTime LastResetTime { get; set; }
}

public class CacheEntry<T>
{
    public string Key { get; set; }
    public T Value { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime? ExpiresAt { get; set; }
    public TimeSpan? SlidingExpiration { get; set; }
}

Using in Services

public class ProductService
{
    private readonly IMemoryCacheService _cache;
    
    public ProductService(IMemoryCacheService cache)
    {
        _cache = cache;
    }
    
    public async Task<Product> GetProductAsync(int productId)
    {
        var cacheKey = $"product:{productId}";
        
        return await _cache.GetOrSetAsync(cacheKey, async () =>
        {
            // Expensive database operation
            return await _repository.GetProductAsync(productId);
        }, TimeSpan.FromMinutes(30));
    }
    
    public async Task InvalidateProductCache(int productId)
    {
        var cacheKey = $"product:{productId}";
        await _cache.RemoveAsync(cacheKey);
    }
    
    public async Task<CacheStatistics> GetCachePerformance()
    {
        return await _cache.GetStatisticsAsync();
    }
}

Requirements

  • .NET Standard 2.0+

License

This project is part of the Making framework.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Making.MemoryCache.Abstractions:

Package Downloads
Making.MemoryCache.Redis

Redis-based memory cache implementation for the Making framework

Making.MemoryCache

In-memory cache implementation for the Making framework

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1-preview 322 7/25/2025
1.0.0-preview 393 7/25/2025