Sage.Caching.Memory
1.0.0
dotnet add package Sage.Caching.Memory --version 1.0.0
NuGet\Install-Package Sage.Caching.Memory -Version 1.0.0
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="Sage.Caching.Memory" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sage.Caching.Memory" Version="1.0.0" />
<PackageReference Include="Sage.Caching.Memory" />
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 Sage.Caching.Memory --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Sage.Caching.Memory, 1.0.0"
#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 Sage.Caching.Memory@1.0.0
#: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=Sage.Caching.Memory&version=1.0.0
#tool nuget:?package=Sage.Caching.Memory&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Sage.Caching.Memory
简介
Sage.Caching.Memory 是一个基于 Microsoft.Extensions.Caching.Memory 的内存缓存服务封装,设计之初是为了简化Caching.Memory,并且提供更可靠和安全的功能。提供了更加易用和功能丰富的缓存操作接口。
主要特性
- 线程安全的缓存操作
- 区域(Region)支持,便于管理和清理相关缓存
- 缓存统计功能
- 支持同步和异步操作
- 完全兼容 AOT 编译
使用示例
- 在 ASP.NET Core 应用中注册和使用
// Program.cs
using Sage.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using System;
var builder = WebApplication.CreateBuilder(args);
// 注册缓存服务
builder.Services.AddMemoryCacheService(options =>
{
// 配置默认缓存选项
options.DefaultEntryOptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30);
options.DefaultEntryOptions.SlidingExpiration = TimeSpan.FromMinutes(5);
// 启用统计
options.EnableStats = true;
});
var app = builder.Build();
app.MapControllers();
app.Run();
- 在控制器中使用
// DataController.cs
using Sage.Caching.Memory;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Threading.Tasks;
[ApiController]
[Route("api/[controller]")]
public class DataController : ControllerBase
{
private readonly ICacheService _cacheService;
public DataController(ICacheService cacheService)
{
_cacheService = cacheService;
}
[HttpGet("{id}")]
public async Task<IActionResult> Get(string id)
{
// 使用区域缓存
var data = await _cacheService.GetOrCreateAsync
(
key: $"data:{id}",
createItem: async () =>
{
// 模拟从数据库获取数据
await Task.Delay(500);
return new { Id = id, Name = $"Item
{id}", Timestamp = DateTime.Now };
},
region: "UserData",
options: new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow =
TimeSpan.FromMinutes(15),
Size = 1024 // 估计内存大小
});
return Ok(data);
}
[HttpGet("stats")]
public IActionResult GetStats()
{
return Ok(_cacheService.GetCacheStats());
}
[HttpDelete("clear")]
public IActionResult ClearCache()
{
_cacheService.Clear();
return Ok(new { message = "Cache cleared" });
}
[HttpDelete("region/{region}")]
public IActionResult ClearRegion(string region)
{
_cacheService.ClearRegion(region);
return Ok(new { message = $"Cache region '
{region}' cleared" });
}
}
许可证
版权所有 © 2025 甲壳虫科技 团队。
贡献
欢迎提交问题和功能请求。 QQ Group: 1054304346
作者
甲壳虫科技
Product | Versions 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.
-
net9.0
- Microsoft.Extensions.Caching.Memory (>= 9.0.7)
- Microsoft.Extensions.DependencyInjection (>= 9.0.7)
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 |
---|---|---|
1.0.0 | 123 | 7/16/2025 |