CS中缓存对性能的优化起了非常大的作用,今天做一次深入的研究。经过大致的代码浏览发现CS中的缓存分为2种:一种采用System.Web.Caching,另一种采用HttpContext.Items(由于CS大量的采用服务器端控件没有使用页面级的缓存)。

首先研究一下System.web.Caching.Cache的使用

在CommunityServerComponents项目中发现了CommunityServer.Components.CSCache,查看一下代码

private CSCache(){} //>> Based on Factor = 5 default value public static readonly int DayFactor = 17280; public static readonly int HourFactor = 720; public static readonly int MinuteFactor = 12; private static readonly Cache _cache; private static int Factor = 5; public static void ReSetFactor(int cacheFactor) { Factor = cacheFactor; } /// /// Static initializer should ensure we only have to look up the current cache /// instance once. /// static CSCache() { HttpContext context = HttpContext.Current; if(context != null) { _cache = context.Cache; } else { _cache = HttpRuntime.Cache; } }

发现其实是对System.Web.Caching.Cache作了一个封装(在CS中这样的例子比比皆是如:CSContext),主要实现了一下功能:

1、插入:Insert、MicroInsert(插入生存周期短的缓存项目)、Max(插入生存周期很长的缓存项目,在系统运行期间永久缓存)
2、获取:Get
3、移除:Remove、RemoveByPattern
4、全部清除:Clear
另外大家看一下“private static int Factor = 5;”,如果希望不用缓存则直接设置为Factor=0即可,如果希望延长缓存生存周期则适当调大Factor值,也可以在CommunityServer.config中修改“cacheFactor”的数值。

在解决方案中搜索一下“CSCache”,会发现有63个文件,分析一下缓存内容主要有2种类型:配置文件及从数据库读取的实体。

一个是配置文件信息(例如:CommunityServer.Configuration.CSConfiguration、CommunityServer.Galleries.Components.GalleryConfiguration、CommunityServer.Blogs.Components.WeblogConfiguration等)
例如CommunityServer.Configuration.CSConfiguration

public static CSConfiguration GetConfig() { CSConfiguration config = CSCache.Get(CacheKey) as CSConfiguration; if(config == null) { string path; if(HttpContext.Current != null) path = HttpContext.Current.Server.MapPath("~/communityserver.config"); else path = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "communityserver.config"; XmlDocument doc = new XmlDocument(); doc.Load(path); config = new CSConfiguration(doc); CSCache.Max(CacheKey,config,new CacheDependency(path)); CSCache.ReSetFactor(config.CacheFactor); } return config; }

可以看到对communityserver.config的配置信息在第一次使用时从配置文件中读取出来并加入采用CSCache.Max()永久缓存,其失效策略为communityserver.config文件的更改。回顾一下,System.Web.Caching.Cache的失效策略一般有三种情况:文件的更改、缓存中其他缓存内容的更改、定义失效时间。GalleryConfiguration及WeblogConfiguration内的缓存失效策略是第2种情况的一个示例

public static WeblogConfiguration Instance() { string cacheKey = "WeblogConfiguration"; WeblogConfiguration config = CSCache.Get(cacheKey) as WeblogConfiguration; if(config == null) { XmlNode node = CSConfiguration.GetConfig().GetConfigSection("CommunityServer/Weblog"); config = new WeblogConfiguration();                 ... CacheDependency dep = new CacheDependency(null, new string[]{CSConfiguration.CacheKey}); CSCache.Insert(cacheKey, config, dep); } return config; }

另一种是数据库实体的缓存,以CommunityServer.Discussions.Components.Posts为例

public static PostSet GetPosts(int postID, int pageIndex, int pageSize, int sortBy, int sortOrder) { PostSet postSet; CSContext csContext = CSContext.Current; string key = "Forum-Posts::P:{0}-PI:{1}-PS:{2}-SB:{3}-SO:{4}"; string postCollectionKey = string.Format(key,postID,pageIndex,pageSize, sortBy, sortOrder); // Attempt to retrieve from Cache postSet = CSCache.Get(postCollectionKey) as PostSet; // forumContext.Context.Cache[postCollectionKey]; if (postSet == null) { // Create Instance of the CommonDataProvider ForumDataProvider dp = ForumDataProvider.Instance(); postSet = dp.GetPosts(postID, pageIndex, pageSize, sortBy, sortOrder, CSContext.Current.User.UserID, true); CSCache.Insert(postCollectionKey,postSet,6); } return postSet; }

GetPosts()首先从缓存中读取postSet = CSCache.Get(postCollectionKey) as PostSet;
如果缓存中不存在则从数据库中读取并放入缓存(缓存失效策略为定义的时间段),这是CS减少数据库连接次数最有效的方式。另外其缓存key值的设置规则也是非常值得学习的。

然后研究一下HttpContext.Items的使用

HttpContext.Items被Rob Howard称之为“每请求缓存”(参见编写高性能 Web 应用程序的 10 个技巧),故名思义即缓存只存在于HttpRequest请求期间,请求结束后缓存即失效。
CS中采用CSContext对HttpContext作了封装,搜索一下“CSContext.Items”发现有7个文件。以CommunityServer.Discussions.Components.Forums为例,分析一下代码

private static Hashtable GetForums(CSContext csContext, bool ignorePermissions, bool cacheable, bool flush) { Hashtable unfilteredForums = null; Hashtable forums; int settingsID = CSContext.Current.SiteSettings.SettingsID; string cacheKey = string.Format("Forums-Site:{0}",settingsID); string localKey = string.Format("ForumsForUser:{0}",ignorePermissions); if(flush) { CSCache.Remove(cacheKey); csContext.Items[localKey] = null; } #if DEBUG_NOCACHE cacheable = false; #endif // Have we already fetched for this request? // //If something is in the context with the current cache key, we have already processed and validated this request! unfilteredForums = csContext.Items[localKey] as Hashtable; //We do not need to revalidate this collection on the same request if((unfilteredForums != null)) return unfilteredForums; else if(!cacheable) csContext.Items.Remove(cacheKey); //Is it safe to use the cached version? if ((!cacheable)) CSCache.Remove(cacheKey); //If we find the forums in the cache, we need to revalidate them. DO NOT return the coolection unless //the call specifies ignorepermissions unfilteredForums = CSCache.Get(cacheKey) as Hashtable; // Get the raw forum groups // if ( unfilteredForums == null ) { unfilteredForums = ForumDataProvider.Instance().GetForums(); // Dynamically add the special forum for private messages // unfilteredForums.Add( 0, PrivateForum() ); // Cache if we can // if (cacheable) CSCache.Insert(cacheKey,unfilteredForums,CSCache.MinuteFactor * 15,CacheItemPriority.High); } // Are we ignoring permissions? // if (ignorePermissions) { //Save us the logic look up later csContext[localKey] = unfilteredForums; return unfilteredForums; } // We need to create a new hashtable // forums = new Hashtable(); User user = CSContext.Current.User; // Filter the list of forums to only show forums this user // is allowed to see // foreach (Forum f in unfilteredForums.Values) { // The forum is added if the user can View, Read // if( Permissions.ValidatePermissions(f,Permission.View,user) ) if(f.IsActive || user.IsForumAdministrator) forums.Add(f.SectionID, f); } // Insert into request cache // csContext[localKey] = forums; return forums; }

第一次访问时首先从数据库中读取数据并存储到Cache及HttpContext.Items中,但是请求结束后HttpContext.Items的缓存即可消失,到底能起什么作用呢?搜索GetForums()发现有多个服务器端控件使用这个方法,如果有多个其中的控件出现在一个页面即一个请求中时,则缓存就发生作用了。

转载于:https://www.cnblogs.com/wulixuan/archive/2006/04/06/368633.html

CS研究笔记-缓存 (转)相关推荐

  1. .net erp(办公oa)开发平台架构概要说明之表单设计器

    2019独角兽企业重金招聘Python工程师标准>>> 背景:搭建一个适合公司erp业务的开发平台. 架构概要图: 表单设计开发部署示例图   表单设计开发部署示例说明 1)每个开发 ...

  2. ServiceStack 项目实例 010 ServiceStack.Northwind - 2

    ServiceStack.Northwind这个项目中提供了三表关联操作和缓存方式操作数据的示例. 主要的服务文件 CustomersService.cs :查询客户列表 OrdersService. ...

  3. Istio-PilotDiscovery服务的启动

    上一篇https://blog.csdn.net/a1023934860/article/details/125855242?spm=1001.2014.3001.5502我们讲解了pilot-dis ...

  4. CPU三级缓存技术解析

    CPU三级缓存技术解析 cpu存取数据 cpu存取数据大致可以认为是下图的流程(此处图比较简单) cpu拿到需要的内存地址,之后这个地址会被mmu转换成真正的物理地址,接下来会去查接下来查L1 cac ...

  5. 消除图片在ie中缓存而无法更新的问题

    程序中图片是动态显示的 原先把打算把图片保存在服务器端然后显示 可是由于ie的缓存问题导致图片无法实时更新显示 所以改为把图片存在session中然后再显示 需要保存的时候再保存到本地 //----- ...

  6. Enterprise Library 4 缓存应用程序块的设计

    缓存应用程序为以下目的而设计: 提供一个大小可管理的 API 集合. 允许开发人员添加标准的缓存操作到他们的应用程序中,而不用学习应用程序块的内部工作. 用 Enterprise Library 配置 ...

  7. System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...

  8. 万字详解本地缓存之王 Caffeine

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 来自:r6d.cn/UXR4 概要 Caffeine[1] ...

  9. 关于分布式多级缓存架构,也许你一直考虑的太简单了

    这篇想聊的话题是:「分布式多级缓存架构的终章」,如何解决大流量.高并发这样的业务场景,取决于你能不能成为这个领域金字塔上层的高手? 能不能把这个问题思考清楚决定了你的成长速度. 很多人在一个行业5年. ...

最新文章

  1. java 类隔离_微服务架构中zuul的两种隔离机制实验
  2. 在51aspx收集的农历日期类
  3. Android Studio 3.3 Beta提供了新的Android代码压缩器R8
  4. c#调用c++的dll接口
  5. Builder内部类
  6. 简单易懂源码解析字符串拼接
  7. uni-app调用wifi接口
  8. CRM系统源码PHP开发
  9. 23000字,信息流广告分析基础!
  10. DX11:先定一个小目标,比如:把DX11龙书上的知识点系统的总结下来
  11. 达梦8初始化参数之BLANK_PAD_MODE
  12. 记录第一次使用python模拟鼠标点击
  13. 【bat】 创建一个文件的快捷方式
  14. 44个路由器知识要点
  15. Oracle入门到精通最详细带例子(新手必看)
  16. 【JavaScript】浏览器
  17. 研发效能度量的正确姿势与落地实践(演讲PPT分享版)
  18. ISP算法----基本DPC算法实现代码
  19. DevStream 概览——开源 DevOps 工具链管理工具
  20. 加州大学计算机硕士,加州大学伯克利分校cs专业

热门文章

  1. 您需要了解有关Angular中的ng-template,ng-content,ng-container和* ngTemplateOutlet的所有信息...
  2. java中next的用法_关于java iterator的next()方法的用法
  3. 小说站 章节内容 ajax,第17章 作业分析与异步编程原理——2019年5月14日22:00
  4. Python中的类、模块和包究竟是什么?
  5. Python培训完可以找什么工作
  6. 正规Java培训机构是什么样的
  7. Python未来的发展趋势怎么样
  8. #大学#SQL基础学习笔记(02)
  9. CIR:2020年全球数据中心应用AOC市场达$42亿
  10. c语言语系的命名风格和java系命名风格