前言
近日在看 ASP.Net Mvc 的源码,在 OutputCache 上遇到了点问题特地记录一下。
在最后知道如何清理服务器端缓存之后,有些同学可能会发现那一句代码可能在直观上与服务器端缓存在什么位置并没有直接的联系(手动捂脸)。
什么是 OutputCache
在现如今的系统架构设计中,几乎都用到了缓存来提升系统的响应速度。但是缓存又分为多种,根据缓存内容的时机区分,我将缓存分为:
- 数据缓存:需要对缓存的内容进行逻辑上的处理,最后才响应给客户端;
- 页面缓存:直接将缓存的内容响应给客户端;
根据我的理解 ASP.Net Mvc 中的 OutputCache 是属于页面缓存。其主要的作用是根据用户请求的 资源URI
及其 请求参数
来区分是否是同一个页面,并将返回的页面缓存到源服务器、代理服务器或者客户端中。
当缓存在源服务器时,如何手动清理服务器端缓存?
服务器端缓存是缓存在哪里?
要解决这个问题,首先就要知道当 Location = OutputCacheLocation.Server
时,页面是缓存什么位置?IIS?System.Web.Caching?等。
然而查阅完 OutputCache 相关的源码后并没有发现什么端倪。只能借助 google 来搜了,最终在 stackoverflow 找到了一个相关的问题。
By default
OutputCacheLocation.Server
simply means that the data is stored in IISWorker Process
memory space. if you enabledKernel-Mode
caching the data is stored in theHttp.Sys
driver memory space (which is an Operating System process, not IIS process).In the case of Windows Azure Shared Caching, the data is serialized and stored in a special process on the Virtual Machine instance, that process is then responsible for managing and synchronization of the cached data between all of the Role instances.
根据@haim770 的描述,我们知道了 OutputCache 的服务器端缓存是缓存在 IIS 进程内的内存空间
的,当启用 Kernel-Mode 内核模式
后,会将页面数据缓存在 http.Sys driver
的内存空间;
如何清理服务器端缓存呢?
知道了服务器端缓存缓存的位置之后,我们很方便的就可以对缓存进行清理:
HttpResponse.RemoveOutputCacheItem(Url.Action(“Index”));