当前位置: 首页 > 编程日记 > 正文

CS研究笔记-缓存 (转)

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

相关文章:

阿里云弹性计算-图形工作站(公测)发布

产品介绍: 阿里云图形工作站,基于GPU 实例,采用AMD 专业GPU,集成了高性能远程桌面功能,非线编软件以及数据存储系统在内的一套完整图形图像处理流程,旨在满足一些高端用户在使用阿里云GPU可视计算实例时的极…

软件测试:黑盒白盒与动态静态之间有必然联系吗

区分黑白盒:看有没有查看源码 区分动静态:看有没有运行程序 情况类型运行程序,只看输入输出动态黑盒运行程序,分析代码结构动态白盒不运行程序,只查看界面静态黑盒不运行程序,查看代码静态白盒

最短路径 - dijkstra

dijkstra是单源点最短路算法。 借图: 其基本思想是,设置顶点集合S并不断地作贪心选择来扩充这个集合。一个顶点属于集合S当且仅当从源到该顶点的最短路径长度已知。 初始时,S中仅含有源。设u是G的某一个顶点,把从源到u且中间只经过…

全面解读WEB 2.0

全面解读WEB 2.0文章来源: http://homepage.yesky.com/300/2295800.shtml1.什么是WEB.2.0Web2.0是以 Flickr、Craigslist、Linkedin、Tribes、Ryze、 Friendster、Del.icio.us、43Things.com等网站为代表,以Blog、TAG、SNS、RSS、wiki等应用为核心,依据六…

Confluence 6 数据库表-系统信息(System information)

2019独角兽企业重金招聘Python工程师标准>>> 这些表格有存储数据相关的状态和 Confluence 站点的相关配置信息。 confversion被用来在升级系统的时候确定那个数据库的版本应该使用,这个表格只对数据库升级有影响。plugindata记录系统安装所有的插件的版本…

入链、出链、反向链接、内链、外链的关系

出入链和内外链没有绝对的关系 出链:自己网页到别的网页 入链:别的网页到自己网页 外链:来源于/去往别的网站的别的网页 内链:来源于/去往本网站的别的网页 反向链接入链

Palo Alto 防火墙升级 Software

今天早上豆子需要升级一下Palo Alto 防火墙的软件。上一次升级已经是半年前的事情了,目前使用的版本是8.0.8,而最新的版本是8.1.2。由于中间跨越了多个版本,因此升级需要从8.0.8 ->8.1.0 -> 8.1.2。每次升级之前需要备份,如…

bash shell 合并文件

# 按列合并文件paste file1 file2 file3 > file4# 要先 sort, 再 joinjoin -a 1 file1 file2 paste格式为:paste -d -s -file1 file2选项含义如下:-d 指定不同于空格或tab键的域分隔符。例如用分隔域,使用 -d 。-s 将每个文件合并成行而不是按行粘贴。…

[再读书]私有构造函数

记录下来,给新手看(应该有人用的到)。私有构造函数初看起来没有什么作用,但是在.net中功能相当多。一般用在许多静态方法的类中,这些静态方法用作一个库,而不是对象。添加私有构造函数,将确保类…

图卷积神经网络(GCN)入门

GCN是从CNN来的 CNN成功在欧式数据上:图像,文本,音频,视频 图像分类,对象检测,机器翻译 CNN基本能力:能学到一些局部的、稳定的结构,通过局部化的卷积核,再通过层级堆叠…

vs2008/2010安装无法打开数据文件解决方案

本人在安装VS2008或2010时,在开始的第一个页面(进度条大约加载到75%左右),提示“无法打开数据文件 C:/Documents and Settings/Administrator/Local Settings/Temp/SIT36198.tmp/DefFactory.dat。”(注:SIT36198.tmp文件夹是随机生产的--36198) 我打开了…

Linux的Unicon资料

Linux的Unicon资料 http://www.okpos.com/wiki/pos/Unicon汉化你的RedHat全功略(五)http://www.unlinux.com/doc/xwindow/20051026/1547.htmlLinux下Unicon安装流程http://www.qqread.com/linux/y621925206.html控制台汉化详细步骤个人认为用unicon实现控制台汉化是最好的解决方…

【强化学习篇】--强化学习从初识到应用

一、前述 强化学习是学习一个最优策略(policy),可以让本体(agent)在特定环境(environment)中,根据当前的状态(state),做出行动(action),从而获得最大回报(G or return)。 通俗点说:学习系统没有像很多其它形式的机器学…

BOS常用代码

2019独角兽企业重金招聘Python工程师标准>>> 验证某个用户是否拥有某个权限 BOSUuid userIdSysContext.getSysContext().getCurrentUserInfo().getId(); BOSUuid orgIdSysContext.getSysContext().getCurrentOrgUnit().getId(); ObjectUuidPK userPK new Objec…

20060521

学习中,发现越学习,越觉得基础的知识比较有用.赶紧补... 转载于:https://www.cnblogs.com/tuantuan/archive/2006/05/21/405894.html

Oracle嵌套表实例说明

嵌套表属于oracle复合数据类型中的集合数据类型。 假设有一个关于动物饲养员的表,希望其中具有他们饲养的动物的信息。用一个嵌套表,就可以在同一个表中存储饲养员和其饲养的全部动物的信息。 创建类型animal_ty:此类型中,对于每…

深入浅出开源性能测试工具 Locust (使用篇 1)

在《【LocustPlus序】漫谈服务端性能测试》中,我对服务端性能测试的基础概念和性能测试工具的基本原理进行了介绍,并且重点推荐了Locust这一款开源性能测试工具。然而,当前在网络上针对Locust的教程极少,不管是中文还是英文&#…

Fedora 19下Guacamole的安装使用

由于我要使用RDP实现web远程桌面,因此需要用到了Guacamole这个开源的软件。之前用Ubuntu12.04折腾了一晚上,也没有找到依赖库文件,而Guacamole的官方安装说明却没有介绍这个依赖库如何安装,而是在RDP的配置说明里才一句话简述了这…

创建ASP.NET WEB自定义控件——例程2

本文通过一段完整的代码向读者介绍复合自定义控件的制作,包括:自定义属性、事件处理、控件间数据传递等方面的技术。 作者在http://damao.0538.org有一些控件和代码,并在更新中,有兴趣的读者可以去下载。 以下是一个登陆框的代码&…

Oracle可变数组实例说明

创建类型comm_info CREATE TYPE comm_info AS OBJECT ( /*此类型为通讯方式的集合*/ no number(3), /*通讯类型号*/ comm_type varchar2(20), /*通讯类型*/ comm_no varchar2(30)); /*号码*/ 创建可变数组comm_info_list CREATE TYPE comm_info_list AS VARRAY(50) OF com…

lua创建文件和文件夹

创建文件夹: os.execute(mkdir xx) 创建文件: f assert(io.open(a.tmp,w)) f:write(test) f:close() 转载于:https://www.cnblogs.com/cyberwalker/p/3599199.html

从定制软件到通用软件的转变

最近做了个项目,在不到一周的时间内完成一个大型网站的外壳,这是个很令人振奋的消息~!我却走了许多弯路,本来公司有自己的信息平台,从信息平台衍生出来的成型的系统也有四五个其实都是工具的拼装,而我做的部…

OPENVAS运行

https://www.jianshu.com/p/382546aaaab5

白盒测试的5种逻辑覆盖法

文章目录判定覆盖法 Decision Coverage (DC)条件覆盖 Condition Coverage (CC)判定-条件覆盖 Condition-Decision Coverage条件组合覆盖 Multiple Condition Coverage (MCC)修正的条件/判定覆盖 Modified Condition/Decision Coverage (MC/DC)5种覆盖的关系判定覆盖法 Decision…

[sinatra] Just Do It: Learn Sinatra, Part One Darren Jones

1. Install sinatra gem gem install sinatra --no-ri --no-rdoc2. Basic App #!/usr/bin/ruby require sinatra get / do"Just Do It" endruby低于1.9,需要在文件开头加require rubygems ruby basic.rbOpen up your browser and go to http://localhost:4567. 3. I…

GMTC 大前端时代前端监控的最佳实践

摘要: 今天我分享的内容分成三个部分: 第一部分是“大前端时代前端监控新的变化”, 讲述这些年来,前端监控一些新的视角以及最前沿的一些思考。 第二部分"前端监控的最佳实践", 从使用的角度出发,介绍前端监…

Visual C#访问接口

对接口成员的访问 对接口方法的调用和采用索引指示器访问的规则与类中的情况也是相同的。如果底层成员的命名与继承而来的高层成员一致,那么底层成员将覆盖同名的高层成员。但由于接口支持多继承,在多继承中,如果两个父接口含有同名的成员&am…

powerdesigner类图在子类中显示从父类继承来的方法

首先确保画了子类和父类之间的继承线 然后在子类的选项卡中点击

[UML]UML系列——用例图中的各种关系(include、extend)

[UML]UML系列——用例图中的各种关系(include、extend) 原文:[UML]UML系列——用例图中的各种关系(include、extend)用例图中的各种关系 一、参与者与用例间的关联关系 参与者与用例之间的通信,也成为关联或通信关系。…