如何清除正式环境中的缓存
项目已经挂在了正式系统上,不断有用户在上面进行业务处理,现在在数据库中添加了一些新数据,而这些数据由于是基础数据,一般不会做变动,所以在项目中是采用缓存技术将整个表中的内容缓存起来的。现在用户需要马上看到这些更新的数据,那么就必须要清除缓存。
由于项目已经是正式发布了,所以不可能用停IIS的方式来清除缓存。在项目中添加一个页面一个按钮,用程序来清除缓存是比较好的方法。
由于不能停止系统,重新发布项目,那么可以直接登录到服务器上,在项目的文件夹下面建立一个文件clear.aspx。打开这个文件,输入如下内容:
保存并关闭文件,现在在项目的站点上打开该页面http://xxxxxx.com/clear.aspx就可以看到我们新建的这个页面。点击这个按钮执行缓存的清理工作。
我们可以看到,系统中原来有12个缓存对象,现在清理后还有0个缓存对象。进入正式系统,可以看到新加入的内容已经显示在页面上,缓存清理成功!
由于项目已经是正式发布了,所以不可能用停IIS的方式来清除缓存。在项目中添加一个页面一个按钮,用程序来清除缓存是比较好的方法。
由于不能停止系统,重新发布项目,那么可以直接登录到服务器上,在项目的文件夹下面建立一个文件clear.aspx。打开这个文件,输入如下内容:
1
<%@ Page Language="C#" %>
2
<%@ Import Namespace="System.Web.Caching" %>
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5
<script runat="server">
6
7
protected void Button1_Click(object sender, EventArgs e)
8
{
9
Cache cache = HttpRuntime.Cache;
10
int count = cache.Count;
11
IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
12
while (CacheEnum.MoveNext())
13
{
14
string cacheItem = Server.HtmlEncode(CacheEnum.Key.ToString());
15
cache.Remove(cacheItem);
16
}
17
Response.Write("<script>alert('缓存清理完成" +count+"--"+ cache.Count.ToString() + "')</" + "script>");
18
}
19
</script>
20
21
<html xmlns="http://www.w3.org/1999/xhtml" >
22
<head runat="server">
23
<title>未命名頁面</title>
24
</head>
25
<body>
26
<form id="form1" runat="server">
27
<div>
28
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
29
</form>
30
</body>
31
</html>
32

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

保存并关闭文件,现在在项目的站点上打开该页面http://xxxxxx.com/clear.aspx就可以看到我们新建的这个页面。点击这个按钮执行缓存的清理工作。
我们可以看到,系统中原来有12个缓存对象,现在清理后还有0个缓存对象。进入正式系统,可以看到新加入的内容已经显示在页面上,缓存清理成功!

【本文章出自博客园深蓝居,转载请注明作者出处,如果您觉得博主的文章对您有很大帮助,欢迎支付宝(studyzy@163.com)对博主进行打赏。】