Sparkdo.Caching.Abstractions 3.0.0-preview.1

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

Sparkdo.Caching.Abstractions

Sparkdo.Caching.Abstractions 定义 Caching 的稳定公共契约:typed cache 与混合缓存的接口、读取结果语义、键规范化 SPI 与缓存名元数据。零 Sparkdo 依赖、零服务注册,适合被「只定义缓存契约、不关心 provider」的业务库引用;需要默认实现与 DI 接线时引用 Sparkdo.Caching。

Hybrid API 复用 Microsoft.Extensions.Caching.Abstractions 的 HybridCacheEntryOptions,不另造契约类型。

契约一览

契约 用途
ICache<TCacheItem> 字符串 key 的 typed cache 入口(ICache<TCacheItem, string> 别名)
ICache<TCacheItem,TKey> 自定义 key 类型的 typed cache 入口,读写 IDistributedCache 语义的缓存
IHybridCache<TCacheItem> 字符串 key 的混合缓存入口
IHybridCache<TCacheItem,TKey> 自定义 key 类型的混合缓存入口(L1 内存 + L2 分布式)
CacheValue<TValue> 读取结果:区分「不存在」与「存在但不可用」
ICacheKeyNormalizer 缓存名 + 业务 key → provider 键的规范化 SPI
CacheKeyNormalizeArgs 规范化输入(Key + CacheName,构造时校验非空白)
CacheNameAttribute 缓存项类型的稳定缓存名元数据
CachingCapability.Id 稳定 capability 标识 "Sparkdo.Caching"

ICache<TCacheItem, TKey> 方法语义

public interface ICache<TCacheItem, TKey>
    where TCacheItem : notnull
    where TKey : notnull
方法 语义
GetAsync(key, ct) 读取缓存值;不存在返回 null
TryGetAsync(key, ct) 读取并区分 missing 与不可用值(返回 CacheValue<TCacheItem>)
SetAsync(key, value, ct) 写入值;过期时间由实现方从配置解析(默认实现取 SparkdoCachingOptions)
RemoveAsync(key, ct) 移除缓存值
RemoveManyAsync(keys, ct) 批量移除;默认实现的底层移除并发执行
GetOrAddAsync(key, factory, ct) 读取或创建;默认实现将相同规范化 key 的并发创建合并为一次工厂调用

全部返回 ValueTask。key 以 ToString() 进键规范化,null / 空白 / ToString() 为空白的 key 抛 ArgumentException / ArgumentNullException。

IHybridCache<TCacheItem, TKey> 方法语义

public interface IHybridCache<TCacheItem, TKey>
    where TCacheItem : notnull
    where TKey : notnull
方法 语义
GetOrCreateAsync(key, factory, options?, ct) 读取或创建:L1 命中直返;未命中经单飞保护回落 L2 与工厂
SetAsync(key, value, options?, ct) 写入 L1 与 L2
RemoveAsync(key, ct) 移除 L1 与 L2
RemoveManyAsync(keys, ct) 批量移除:L1 两段式清理(即时一次 + L2 删完补一次),L2 并发移除

options 为 HybridCacheEntryOptions(Microsoft.Extensions.Caching.Abstractions):Expiration 约束 L2,LocalCacheExpiration 约束 L1。注意混合缓存没有纯直读方法——GetOrCreateAsync 的工厂就是未命中兜底。

CacheValue<TValue>

readonly record struct,把「缓存不存在」与「存在但反序列化出不可用值」分开:

var result = await cache.TryGetAsync("42");

if (result.HasValue)
{
    var value = result.Value;             // 存在时取值
}

// Missing 状态:
var missing = CacheValue<string>.Missing;
missing.HasValue;                          // false
missing.GetValueOrDefault();               // null,安全
// missing.Value                           // 抛 InvalidOperationException
成员 语义
HasValue 是否存在可用值
Value 可用值;missing 状态读取抛 InvalidOperationException
GetValueOrDefault() missing 时返回 default
Missing missing 状态单例(default)
Found(value) 以可用值构造;null 实参抛 ArgumentNullException

为什么需要它:TCacheItem 约束 notnull,接口层面的 GetAsync 用 null 表达 missing 已经足够;但实现层仍可能从底层字节反序列化出 null(如其他写入方存了 JSON null)。默认实现把这种「存在但不可用」表达为异常而不是 missing,TryGetAsync + CacheValue 让调用方能安全地在两者间分流。

ICacheKeyNormalizer

public interface ICacheKeyNormalizer
{
    string NormalizeKey(CacheKeyNormalizeArgs args);
}

实现契约:确定性纯映射——相同输入在同一配置下得到相同键,结果不随调用次数变化(不能有时间、随机数或计数器状态)。默认实现的输出格式为 c:{缓存名},k:{前缀}{key};实现在构造时捕获 options 快照。

CacheNameAttribute

[CacheName("Users")]
public sealed record UserCacheItem(string Name);

可标注类、接口、结构体。空白名在构造 attribute 时即抛 ArgumentException(fail fast 在标注处,而非运行期键解析处)。运行时解析顺序为 SparkdoCachingOptions.ConfigureCacheName 显式配置 > 本 attribute > 类型命名约定(全名去掉 CacheItem 后缀);attribute 在缓存实例构造时解析一次,无程序集扫描,AOT 安全。

面向实现者的备注

  • 本包不注册任何服务,也不提供实现;ICache<,> / IHybridCache<,> 的默认实现在 Sparkdo.Caching。
  • 实现自定义 provider 时只需实现 ICache<TCacheItem, TKey>(字符串 key 形态由调用方适配),或在更底层实现 IDistributedCache 交给默认 typed cache 接线。
  • 契约全部 AOT / trimming 安全:无反射扫描、无动态代码;[CacheName] 是唯一反射点(GetCustomAttribute,构造期一次)。
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Sparkdo.Caching.Abstractions:

Package Downloads
Sparkdo.Caching

Sparkdo Caching 运行时:基于 IDistributedCache 的 typed cache、L1+L2 混合缓存、默认内存 provider、Options 与 AddSparkdoCaching 一行接线。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0-preview.1 71 10/30/2026
2.0.0-preview.2 117 7/20/2026
2.0.0-preview.1 93 7/18/2026