当前位置: 首页 > 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.lryc.cn/news/574758.html

相关文章:

  • MyBatis Plus与P6Spy日志配置
  • leetcode230-二叉搜索树中第K小的元素
  • 【计算机网络】期末复习
  • 【教学类-89-08】20250624新年篇05——元宵节灯笼2CM黏贴边(倒置和正立数字 )
  • STM32学习笔记——中断控制
  • (C++)标准模板库(STL)相关介绍(C++教程)
  • C语言专题——关键字详解
  • 前端后端文件下载防抖实现方案
  • 浅谈开源在线客服系统与 APP 集成的技术方案与优劣势
  • 华为云Flexus+DeepSeek征文 | 基于华为云的 Dify-LLM 企业级 AI 开发平台部署指南
  • React Hooks详解
  • 开源跨平台的轻量 C# 编辑器
  • 想考华为HCIA-AI,应该怎么入门?
  • 第五课:大白话教你用K邻近算法做分类和回归
  • (三十六)深度解析领域特定语言(DSL)第七章——语法制导翻译——案例语义模型
  • 10-Python模块详解
  • wpa_supplicant连接到了路由,但是 udhcpc会分配到不同网段的ip,路由器ip为192.168.0网段,板子分配ip为192.168.1的网段
  • 美团小程序闪购 mtgsig1.2
  • Vue的学习内容和目标
  • OSS迁移实战:从自建MinIO到阿里云OSS的完整数据迁移方案
  • Rust 项目实战:多线程 Web 服务器
  • Lombok注解 - 提高Java开发效率
  • order、sort、distribute和cluster by(Spark/Hive)
  • 开篇:4周的时间设计并开发一个个性化具备专业知识的东方命理师AI agent!
  • 【JavaEE】(5) 网络原理的基础认识
  • spring-security-oauth2系列:授权码授权模式
  • F5推出AI网关,赋能企业化解大模型应用风险
  • VSCode源码解析-程序的启动逻辑
  • 深度学习在智能机器人导航中的创新应用与未来趋势
  • 分布式训练中的随机种子策略:深入理解与实践指南