-
Notifications
You must be signed in to change notification settings - Fork 2
Cache
皮卡超人 edited this page Nov 25, 2025
·
1 revision
WodToolKit 提供了基于内存的临时缓存实现,支持 TTL(生存时间)设置和自动清理。
using WodToolkit.src.Cache;
using System;
// 创建缓存实例(每30秒清理一次,默认TTL为300秒)
var cache = new TempCache<string, string>(
cleanupInterval: TimeSpan.FromSeconds(30),
defaultTtl: 300
);
// 使用默认设置(每60秒清理一次,默认TTL为300秒)
var defaultCache = new TempCache<string, string>();// 使用默认 TTL
cache.Set("key1", "value1");
// 自定义 TTL(秒)
cache.Set("key2", "value2", 60); // 60秒后过期
// 设置复杂对象
cache.Set("user:123", new { Name = "John", Age = 25 }, 120);// 方式一:TryGetValue(推荐)
if (cache.TryGetValue("key1", out string value))
{
Console.WriteLine($"缓存值: {value}");
}
else
{
Console.WriteLine("缓存项不存在或已过期");
}// 移除单个缓存项
cache.Remove("key2");
// 清空所有缓存
cache.Clear();if (cache.ContainsKey("key1"))
{
Console.WriteLine("缓存项存在");
}var keys = cache.GetKeys();
foreach (var key in keys)
{
Console.WriteLine($"缓存键: {key}");
}using WodToolkit.src.Cache;
using System;
var cache = new TempCache<string, string>(
cleanupInterval: TimeSpan.FromSeconds(30), // 每30秒清理一次过期项
defaultTtl: 300 // 默认TTL为300秒
);
// 设置缓存
cache.Set("session:user1", "active", 60); // 60秒后过期
cache.Set("session:user2", "active", 120); // 120秒后过期
// 获取缓存
if (cache.TryGetValue("session:user1", out string status))
{
Console.WriteLine($"用户状态: {status}");
}
// 使用完毕后释放资源
cache.Dispose();using WodToolkit.Http;
using WodToolkit.src.Cache;
using System.Net;
var cache = new TempCache<string, string>(TimeSpan.FromSeconds(30), 300);
async Task<string> GetApiData(string endpoint)
{
// 检查缓存
if (cache.TryGetValue(endpoint, out string cachedData))
{
Console.WriteLine("从缓存获取数据");
return cachedData;
}
// 从 API 获取数据
var httpRequest = new HttpRequestClass();
httpRequest.Open(endpoint, HttpMethod.Get).Send();
var response = httpRequest.GetResponse();
if (response.StatusCode == 200)
{
// 缓存数据(5分钟)
cache.Set(endpoint, response.Body, 300);
Console.WriteLine("从 API 获取数据并缓存");
return response.Body;
}
return null;
}using (var cache = new TempCache<string, string>())
{
cache.Set("key", "value");
// 使用缓存...
} // 自动释放资源- 内存使用:缓存数据存储在内存中,注意控制缓存大小
- TTL 单位:TTL 以秒为单位
- 自动清理:过期项会在清理间隔时自动删除
-
线程安全:
TempCache是线程安全的,可以在多线程环境中使用 -
资源释放:使用完毕后应调用
Dispose()释放资源
- HTTP - HTTP 客户端使用指南