Tenon.Caching.InMemory 0.0.1-alpha-202502241449

This is a prerelease version of Tenon.Caching.InMemory.
dotnet add package Tenon.Caching.InMemory --version 0.0.1-alpha-202502241449
                    
NuGet\Install-Package Tenon.Caching.InMemory -Version 0.0.1-alpha-202502241449
                    
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="Tenon.Caching.InMemory" Version="0.0.1-alpha-202502241449" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tenon.Caching.InMemory" Version="0.0.1-alpha-202502241449" />
                    
Directory.Packages.props
<PackageReference Include="Tenon.Caching.InMemory" />
                    
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 Tenon.Caching.InMemory --version 0.0.1-alpha-202502241449
                    
#r "nuget: Tenon.Caching.InMemory, 0.0.1-alpha-202502241449"
                    
#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 Tenon.Caching.InMemory@0.0.1-alpha-202502241449
                    
#: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=Tenon.Caching.InMemory&version=0.0.1-alpha-202502241449&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Tenon.Caching.InMemory&version=0.0.1-alpha-202502241449&prerelease
                    
Install as a Cake Tool

Tenon.Caching.InMemory

NuGet version License: MIT

基于 System.Runtime.Caching.MemoryCache 的高性能内存缓存实现,为 .NET 应用程序提供简单且灵活的缓存操作接口。

✨ 功能特性

  • 🚀 基于 MemoryCache 的高性能实现
  • 🔧 支持自定义缓存配置
  • 💉 集成依赖注入框架
  • 🎯 统一的 ICacheProvider 接口
  • 🔄 自动过期缓存清理
  • 📊 可配置内存使用限制
  • 🛡️ 保证线程安全

📦 安装方式

通过 NuGet 包管理器安装:

dotnet add package Tenon.Caching.InMemory

🚀 快速入门

1. 注册服务

Startup.csProgram.cs 中配置服务:

// 使用默认配置
services.AddInMemoryCache();

// 或使用自定义配置
services.AddInMemoryCache(options =>
{
    // 设置最大内存限制为 1GB
    options.CacheMemoryLimitMegabytes = 1024;    
    // 使用最多 50% 的物理内存
    options.PhysicalMemoryLimitPercentage = 50;  
    // 每 5 分钟清理过期缓存
    options.PollingInterval = TimeSpan.FromMinutes(5); 
});

2. 使用缓存服务

public class WeatherService
{
    private readonly ICacheProvider _cache;

    public WeatherService(ICacheProvider cache)
    {
        _cache = cache;
    }

    public async Task<WeatherForecast> GetForecastAsync(string city)
    {
        var cacheKey = $"weather:{city}";
        
        // 尝试从缓存获取数据
        if (_cache.TryGet(cacheKey, out WeatherForecast? forecast))
            return forecast;

        // 缓存未命中,从数据源获取
        forecast = await GetForecastFromApiAsync(city);
        
        // 存入缓存,设置 30 分钟过期
        _cache.Set(cacheKey, forecast, TimeSpan.FromMinutes(30));
        
        return forecast;
    }
}

📖 高级用法

自定义缓存配置

services.AddInMemoryCache(options =>
{
    // 基础配置
    options.CacheName = "CustomCache";
    options.CacheMemoryLimitMegabytes = 2048;
    
    // 内存限制
    options.PhysicalMemoryLimitPercentage = 75;
    
    // 清理配置
    options.PollingInterval = TimeSpan.FromMinutes(10);
});

缓存操作示例

public class CacheExample
{
    private readonly ICacheProvider _cache;

    public CacheExample(ICacheProvider cache)
    {
        _cache = cache;
    }

    public void CacheOperations()
    {
        // 设置字符串缓存
        _cache.Set("key1", "value1", TimeSpan.FromHours(1));

        // 获取缓存数据
        if (_cache.TryGet("key1", out string? value))
        {
            Console.WriteLine($"缓存命中: {value}");
        }

        // 删除缓存
        _cache.Remove("key1");

        // 缓存复杂对象
        var user = new User { Id = 1, Name = "张三" };
        _cache.Set($"user:{user.Id}", user, TimeSpan.FromMinutes(30));
    }
}

⚙️ 配置选项说明

配置项 说明 默认值
CacheName 缓存实例名称 MemoryCacheProvider
CacheMemoryLimitMegabytes 最大内存限制(MB) 不限制
PhysicalMemoryLimitPercentage 物理内存使用限制百分比 不限制
PollingInterval 过期缓存清理间隔 2分钟

🔨 项目依赖

  • System.Runtime.Caching
  • Tenon.Caching.Abstractions
  • Microsoft.Extensions.DependencyInjection.Abstractions

📁 项目结构

Tenon.Caching.InMemory/
├── Configurations/
│   └── InMemoryCachingOptions.cs    # 缓存配置选项
├── Extensions/
│   ├── CachingOptionsExtension.cs    # 缓存选项扩展
│   └── ServiceCollectionExtension.cs # 服务注册扩展
├── MemoryCacheProvider.cs           # 内存缓存实现
└── Tenon.Caching.InMemory.csproj    # 项目文件

📝 使用注意事项

1. 内存管理

  • 根据应用程序需求合理设置内存限制
  • 为缓存项设置合适的过期时间
  • 定期监控缓存命中率和内存使用情况

2. 性能优化

  • 合理配置缓存清理间隔
  • 避免缓存过大的对象
  • 使用合适的缓存策略

3. 最佳实践

  • 采用统一的缓存键命名规范
  • 实现缓存预热机制
  • 添加必要的缓存监控和日志记录

🤝 参与贡献

欢迎参与项目贡献!请阅读我们的贡献指南了解如何参与项目开发。

📄 开源协议

本项目采用 MIT 开源协议 - 详情请查看 LICENSE 文件。

Product Compatible and additional computed target framework versions.
.NET 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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.1-alpha-202502241449 69 2/24/2025
0.0.1-alpha-202502101554 89 2/10/2025
0.0.1-alpha-202502101448 77 2/10/2025
0.0.1-alpha-202502101434 75 2/10/2025
0.0.1-alpha-202501130258 60 1/13/2025
0.0.1-alpha-202412311524 89 12/31/2024
0.0.1-alpha-202412061617 71 12/6/2024
0.0.1-alpha-202412051527 77 12/5/2024
0.0.1-alpha-202412051431 68 12/5/2024
0.0.1-alpha-202412041445 66 12/4/2024
0.0.1-alpha-202412021409 68 12/2/2024
0.0.1-alpha-202411301019 69 11/30/2024
0.0.1-alpha-202411170525 67 11/17/2024
0.0.1-alpha-202411161308 63 11/16/2024
0.0.1-alpha-202411131604 67 11/13/2024
0.0.1-alpha-202411111439 75 11/11/2024
0.0.1-alpha-202411051434 62 11/5/2024
0.0.1-alpha-202410281339 69 10/28/2024
0.0.1-alpha-202410131500 69 10/13/2024
0.0.1-alpha-202407261457 73 7/26/2024
0.0.1-alpha-202407261325 67 7/26/2024
0.0.1-alpha-202406271301 73 6/27/2024
0.0.1-alpha-202406251508 66 6/25/2024
0.0.1-alpha-202406251310 69 6/25/2024
0.0.1-alpha-202406141611 66 6/14/2024
0.0.1-alpha-202406141550 73 6/14/2024
0.0.1-alpha-202406121515 68 6/12/2024
0.0.1-alpha-202406061553 78 6/6/2024
0.0.1-alpha-202406041519 75 6/4/2024
0.0.1-alpha-202406011613 78 6/1/2024
0.0.1-alpha-202406011238 72 6/1/2024
0.0.1-alpha-202405311458 63 5/31/2024
0.0.1-alpha-202405291213 76 5/29/2024
0.0.1-alpha-202405190457 74 5/19/2024
0.0.1-alpha-202405161229 74 5/16/2024
0.0.1-alpha-202405141510 68 5/14/2024
0.0.1-alpha-202405101323 75 5/10/2024
0.0.1-alpha-202405081356 79 5/8/2024
0.0.1-alpha-202405021337 41 5/2/2024
0.0.1-alpha-202405021336 36 5/2/2024
0.0.1-alpha-202405020452 56 5/2/2024
0.0.1-alpha-202405011443 57 5/1/2024
0.0.1-alpha-202404291541 71 4/29/2024
0.0.1-alpha-202404281218 68 4/28/2024