配置表分析和缓存的应用
一、配置表分析和缓存的应用
配置表就是对前端中可能经常修改的数据配置在数据库中。其实就是类于配置文件一样,但是此处是将该数据放置在数据库中进行管理。
只需要在数据库中添加一张表,并且对表添加三列字段,分别为,id,key,value列。
二、 缓存的应用,对于需要经常使用的值,需要放置在缓存中,可以提高数据提取的速度。
1. 封装的缓存的方法。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace Common { public class CacheHelper { /// <summary> /// 从缓存中获取数据 /// </summary> /// <param name="key"></param> /// <returns></returns> public static object Get(string key) { System.Web.Caching.Cache cache = HttpRuntime.Cache; return cache[key]; } /// <summary> /// 向缓存中添加数据 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void Set(string key, object value) { System.Web.Caching.Cache cache = HttpRuntime.Cache; cache[key] = value; } //重载,设置过期事件 public static void Set(string key, object value, DateTime time) { System.Web.Caching.Cache cache = HttpRuntime.Cache; //其中第三个值,是缓存依赖,此处不需要,所以填null,第四个是设置过期时间,第五个表示滑动过期时间 cache.Insert(key, value, null, time, TimeSpan.Zero ); } /// <summary> /// 删除缓存,如果需要更新表中某一项的值,切记要把缓存中的值删除掉 /// </summary> /// <param name="key"></param> public static void Rmeove(string key) { System.Web.Caching.Cache cache = HttpRuntime.Cache; cache.Remove(key); } } }
获取缓存中的数据
/// <summary> /// 根据配置项的名称,获取具体的配置的值 /// </summary> /// <param name="key">配置项的名称</param> /// <returns></returns> public string GetValue(string key) { //判断缓存中是否有数据 if (Common.CacheHelper.Get("setting_" + key) == null) { string value = dal.GetModel(key).Value; //GetModel是封装的查询该该数据,并且存放在缓存中 Common.CacheHelper.Set("setting_" + key, value); return value; } else { return Common.CacheHelper.Get("setting_" + key).ToString(); } }
GetModel方法:
/// <summary> ///根据配置的名称 得到一个对象实体 /// </summary> public BookShop.Model.Settings GetModel(string name) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 Id,Name,Value from Settings "); strSql.Append(" where Name=@Name "); SqlParameter[] parameters = { new SqlParameter("@Name", SqlDbType.NVarChar,50)}; parameters[0].Value = name; BookShop.Model.Settings model = new BookShop.Model.Settings(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["Id"].ToString() != "") { model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString()); } model.Name = ds.Tables[0].Rows[0]["Name"].ToString(); model.Value = ds.Tables[0].Rows[0]["Value"].ToString(); return model; } else { return null; } }
调用
bll.GetValue("Key的值")
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构