当前位置: 首页 > news >正文

【WCF】单例模式的线程安全缓存管理器实现,给你的WebApi加入缓存吧

WCF 系列文章目录

链接: 【WCF】基于WCF在WinForms搭建RESTful服务指南


文章目录

  • WCF 系列文章目录
  • 前言
  • 一、缓存项结构搭建
  • 二、核心方法
  • 三、引用示例
  • 四、完整代码
  • 总结


前言

缓存是我们日常优化程序的老面孔。尤其是那种需要复杂计算的服务,又或是涉及和数据库交互更具大量数据生成报表内容的场景。我们都可以通过调用缓存,来使数据库查询次数减少,可降低 IO 压力。

本文将为大家介绍如何在WCF构建的RESTful服务里添加缓存的功能。通过一个泛型静态类实现缓存管理器,用于实现基于内存的键值对缓存功能。采用了单例模式,并提供了线程安全的缓存操作。该存管理器不依赖于WCF。

一、缓存项结构搭建

  • 内部类CacheItem,用于封装缓存数据及其时间戳。
	/// <summary>/// 缓存项结构,包含数据和时间戳/// </summary>private class CacheItem{public T Data { get; set; }public DateTime LastUpdateTime { get; set; }}
  • _cacheDict:线程安全的字典,ConcurrentDictionary类,存储所有缓存项。
  • _keyLocks:为每个缓存键提供独立的锁对象,实现细粒度锁控制。
  • _defaultCacheDuration:默认缓存过期时间(60 秒)。
	private static readonly ConcurrentDictionary<string, CacheItem> _cacheDict= new ConcurrentDictionary<string, CacheItem>();private static readonly ConcurrentDictionary<string, object> _keyLocks= new ConcurrentDictionary<string, object>();private static readonly TimeSpan _defaultCacheDuration = TimeSpan.FromSeconds(60);

二、核心方法

GetCache是该缓存管理器的核心方法,用于获取缓存项,如果不存在或已过期则重新加载。
它接收缓存键,数据获取委托两个必须参数,和自定义缓存时间,当数据获取失败时是否使用旧数据两个可选项。

执行步骤
执行GetCache方法,首先检查缓存键是否存在,无效则使用类型全名。然后通过TryGetValidCacheItem尝试获取有效缓存项。
如果缓存无效,获取键专用锁,并进行双重检查机制,避免多线程重复加载。最后调用数据获取委托dataRetriever,更新缓存并返回新数据。
如果缓存有效,则直接放回缓存数据。

	/// <summary>/// 获取或创建缓存/// </summary>/// <param name="cacheKey">缓存键(为空时使用类型全名)</param>/// <param name="dataRetriever">数据获取委托</param>/// <param name="cacheDuration">自定义缓存时间(默认60秒)</param>/// <param name="useStaleData">当数据获取失败时是否使用旧数据</param>public static T GetCache(string cacheKey,Func<T> dataRetriever,TimeSpan? cacheDuration = null,bool useStaleData = true){cacheKey = string.IsNullOrEmpty(cacheKey) ? typeof(T).FullName : cacheKey;var duration = cacheDuration ?? _defaultCacheDuration;//尝试获取有效缓存if (TryGetValidCacheItem(cacheKey, duration, out var cachedItem)){return cachedItem.Data;}//获取键级专用锁var keyLock = _keyLocks.GetOrAdd(cacheKey, (_) => new object());lock (keyLock){// 双重检查if (TryGetValidCacheItem(cacheKey, duration, out cachedItem)){return cachedItem.Data;}try{//尝试获取新数据var newData = dataRetriever();UpdateCache(cacheKey, newData);return newData;}catch{//降级策略:如果允许返回过期数据if (useStaleData && _cacheDict.TryGetValue(cacheKey, out cachedItem)){return cachedItem.Data;}throw;}}}/// <summary>/// 更新缓存/// </summary>public static void UpdateCache(string cacheKey, T data){_cacheDict[cacheKey] = new CacheItem{Data = data,LastUpdateTime = DateTime.Now};}/// <summary>/// 清除指定缓存/// </summary>public static void ClearCache(string cacheKey){_cacheDict.TryRemove(cacheKey, out _);_keyLocks.TryRemove(cacheKey, out _); // 同时清理锁}/// <summary>/// 清除所有缓存/// </summary>public static void ClearAll(){_cacheDict.Clear();_keyLocks.Clear();}/// <summary>/// 检查缓存项是否有效/// </summary>/// <param name="key"></param>/// <param name="duration"></param>/// <param name="item"></param>/// <returns></returns>private static bool TryGetValidCacheItem(string key, TimeSpan duration, out CacheItem item){//用于判断_cacheDict字典里是否有【key】对应的缓存项,并且赋值给item。返回值是bool类型if (_cacheDict.TryGetValue(key, out item)){//进一步判断缓存项是否在指定时间段内return (DateTime.Now - item.LastUpdateTime) <= duration;}return false;}

三、引用示例

因为CacheManager是静态类,我们将泛型指定为需要的返回结果类型。然后通过一个委托参数执行获取数据的方法。如果想设置自定义缓存时间,当数据获取失败时是否使用旧数据这填入相应参数。
这里通过Thread.Sleep(5000)模拟耗时方法执行。

/// <summary>
/// 获取所有学生
/// </summary>
/// <returns></returns>
public JsonResult<List<Student>> GetStudents()
{try{var cacheData = CacheManager<List<Student>>.GetCache("GetStudents",() => {Thread.Sleep(5000);return new List<Student>{new Student { Id = 1, Name = "张三" },new Student { Id = 2, Name = "李四" }};});return new JsonResult<List<Student>>{code = "200",msg = "success",data = cacheData};}catch (Exception ex){return new JsonResult<List<Student>>{code = "500",msg = $"获取学生列表失败: {ex.Message}",data = null};}
}/// <summary>
/// 获取指定学生
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public JsonResult<Student> GetStudent(string id)
{try{if (!int.TryParse(id, out int studentId)){return new JsonResult<Student>{code = "400",msg = "无效的学生ID",data = null};}var cacheData = CacheManager<Student>.GetCache($"GetStudent_{studentId}", () => {Thread.Sleep(5000);return _students.FirstOrDefault(s => s.Id == studentId);});if (cacheData == null){return new JsonResult<Student>{code = "404",msg = "未找到指定学生",data = null};}return new JsonResult<Student>{code = "200",msg = "success",data = cacheData};}catch (Exception ex){return new JsonResult<Student>{code = "500",msg = $"获取学生失败: {ex.Message}",data = null};}
}

执行耗时方法
执行耗时方法
调用缓存

在这里插入图片描述

四、完整代码

 public static class CacheManager<T>
{/// <summary>/// 缓存项结构,包含数据和时间戳/// </summary>private class CacheItem{public T Data { get; set; }public DateTime LastUpdateTime { get; set; }}/// <summary>/// 缓存字典/// </summary>private static readonly ConcurrentDictionary<string, CacheItem> _cacheDict= new ConcurrentDictionary<string, CacheItem>();/// <summary>/// 缓存锁,只会根据键锁定指定的缓存项/// </summary>private static readonly ConcurrentDictionary<string, object> _keyLocks= new ConcurrentDictionary<string, object>();private static readonly TimeSpan _defaultCacheDuration = TimeSpan.FromSeconds(55);/// <summary>/// 获取或创建缓存/// </summary>/// <param name="cacheKey">缓存键(为空时使用类型全名)</param>/// <param name="dataRetriever">数据获取委托</param>/// <param name="cacheDuration">自定义缓存时间(默认55秒)</param>/// <param name="useStaleData">当数据获取失败时是否使用旧数据</param>public static T GetCache(string cacheKey,Func<T> dataRetriever,TimeSpan? cacheDuration = null,bool useStaleData = true){cacheKey = string.IsNullOrEmpty(cacheKey) ? typeof(T).FullName : cacheKey;var duration = cacheDuration ?? _defaultCacheDuration;//尝试获取有效缓存if (TryGetValidCacheItem(cacheKey, duration, out var cachedItem)){return cachedItem.Data;}//获取键级专用锁var keyLock = _keyLocks.GetOrAdd(cacheKey, (_) => new object());lock (keyLock){// 双重检查if (TryGetValidCacheItem(cacheKey, duration, out cachedItem)){return cachedItem.Data;}try{//尝试获取新数据var newData = dataRetriever();UpdateCache(cacheKey, newData);return newData;}catch{//降级策略:如果允许返回过期数据if (useStaleData && _cacheDict.TryGetValue(cacheKey, out cachedItem)){return cachedItem.Data;}throw;}}}/// <summary>/// 更新缓存/// </summary>public static void UpdateCache(string cacheKey, T data){_cacheDict[cacheKey] = new CacheItem{Data = data,LastUpdateTime = DateTime.Now};}/// <summary>/// 清除指定缓存/// </summary>public static void ClearCache(string cacheKey){_cacheDict.TryRemove(cacheKey, out _);_keyLocks.TryRemove(cacheKey, out _); // 同时清理锁}/// <summary>/// 清除所有缓存/// </summary>public static void ClearAll(){_cacheDict.Clear();_keyLocks.Clear();}/// <summary>/// 检查缓存项是否有效/// </summary>/// <param name="key"></param>/// <param name="duration"></param>/// <param name="item"></param>/// <returns></returns>private static bool TryGetValidCacheItem(string key, TimeSpan duration, out CacheItem item){//用于判断_cacheDict字典里是否有【key】对应的缓存项,并且赋值给item。返回值是bool类型if (_cacheDict.TryGetValue(key, out item)){//进一步判断缓存项是否在指定时间段内return (DateTime.Now - item.LastUpdateTime) <= duration;}return false;}
}

总结

这就是一个用于WCF RESTful服务的缓存管理器实现,它采用了泛型静态类和单例模式,提供了线程安全的缓存操作。希望能帮助到大家。

http://www.lqws.cn/news/504559.html

相关文章:

  • 【DeepSeek实战】3、Ollama实战指南:LobeChat+多网关架构打造高可用大模型集群
  • 数据赋能(319)——安全与合规——数据安全可控
  • 吉林大学软件工程期末复习整理
  • 基于大模型预测的化脓性阑尾炎诊疗方案研究报告
  • MSTP技术解析:提升网络负载均衡
  • 解决移动端播放MP4黑屏问题,PC端正常的问题
  • 华为云对象存储OBS 支持安卓/iOS/鸿蒙UTS组件
  • Android15启动icon界面的背景图颜色
  • contOS7安装docker命令及yum源更换为国内源
  • 使用 .NET Core+GcExcel,生成 Excel 文件
  • AWS S3 可观测性最佳实践
  • Sentinel(三):Sentinel熔断降级
  • python的轻院网购商城管理系统
  • 【Bugku】简单取证1
  • 深度洞察丨2025零信任应对挑战,拥抱变革,开启智能安全新时代
  • 技术伦理之争:OpenAI陷抄袭风波,法院强制下架宣传视频
  • 入门k8s-Pod
  • k8s强制删除podpvpvc和nsnamespace
  • 《陈欣与链接器的黄昏》
  • 华为云Flexus+DeepSeek征文|体验华为云ModelArts快速搭建Dify-LLM应用开发平台并创建小红书爆款文案大模型
  • 软件工程:从理论到实践,构建可靠软件的艺术与科学
  • python有哪些常用的GUI(图形用户界面)库及选择指南
  • 通义灵码编程智能体深度评测(Qwen3模型+终端操作+MCP工具调用实战)
  • STM32 环境监测与控制系统的设计与实现
  • 认识Scikit-learn/PyTorch/TensorFlow这几个AI框架
  • 从代码学习深度学习 - 情感分析:使用循环神经网络 PyTorch版
  • 国产安路FPGA纯verilog视频图像去雾,基于暗通道先验算法实现,提供5套TD工程源码和技术支持
  • 帮助装修公司拓展客户资源的微信装修小程序怎么做?
  • 开篇-认识Gin——Go语言Web框架的性能王者
  • 接口自动化测试之 pytest 接口关联框架封装