Tenon.Caching.InMemory 0.0.1-alpha-202502101554

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

// Install Tenon.Caching.InMemory as a Cake Tool
#tool nuget:?package=Tenon.Caching.InMemory&version=0.0.1-alpha-202502101554&prerelease                

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. 
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-202502101554 23 2/10/2025
0.0.1-alpha-202502101448 17 2/10/2025
0.0.1-alpha-202502101434 20 2/10/2025
0.0.1-alpha-202501130258 37 1/13/2025
0.0.1-alpha-202412311524 74 12/31/2024
0.0.1-alpha-202412061617 56 12/6/2024
0.0.1-alpha-202412051527 60 12/5/2024
0.0.1-alpha-202412051431 54 12/5/2024
0.0.1-alpha-202412041445 51 12/4/2024
0.0.1-alpha-202412021409 50 12/2/2024
0.0.1-alpha-202411301019 56 11/30/2024
0.0.1-alpha-202411170525 51 11/17/2024
0.0.1-alpha-202411161308 49 11/16/2024
0.0.1-alpha-202411131604 55 11/13/2024
0.0.1-alpha-202411111439 64 11/11/2024
0.0.1-alpha-202411051434 52 11/5/2024
0.0.1-alpha-202410281339 55 10/28/2024
0.0.1-alpha-202410131500 59 10/13/2024
0.0.1-alpha-202407261457 63 7/26/2024
0.0.1-alpha-202407261325 58 7/26/2024
0.0.1-alpha-202406271301 62 6/27/2024
0.0.1-alpha-202406251508 57 6/25/2024
0.0.1-alpha-202406251310 57 6/25/2024
0.0.1-alpha-202406141611 57 6/14/2024
0.0.1-alpha-202406141550 65 6/14/2024
0.0.1-alpha-202406121515 59 6/12/2024
0.0.1-alpha-202406061553 69 6/6/2024
0.0.1-alpha-202406041519 60 6/4/2024
0.0.1-alpha-202406011613 70 6/1/2024
0.0.1-alpha-202406011238 60 6/1/2024
0.0.1-alpha-202405311458 56 5/31/2024
0.0.1-alpha-202405291213 68 5/29/2024
0.0.1-alpha-202405190457 62 5/19/2024
0.0.1-alpha-202405161229 59 5/16/2024
0.0.1-alpha-202405141510 57 5/14/2024
0.0.1-alpha-202405101323 63 5/10/2024
0.0.1-alpha-202405081356 71 5/8/2024
0.0.1-alpha-202405021337 30 5/2/2024
0.0.1-alpha-202405021336 25 5/2/2024
0.0.1-alpha-202405020452 45 5/2/2024
0.0.1-alpha-202405011443 49 5/1/2024
0.0.1-alpha-202404291541 63 4/29/2024
0.0.1-alpha-202404281218 61 4/28/2024