网站内容方案(2021-05-30用缓存来优化网站性能的方法)

优采云 发布时间: 2022-03-26 11:25

  网站内容方案(2021-05-30用缓存来优化网站性能的方法)

  2021-05-30

  估计没有人知道使用缓存优化网站性能的方法。ASP.NET 提供 HttpRuntime.Cache 对象来缓存数据,并提供 OutputCache 指令来缓存整个页面输出。虽然 OutputCache 指令使用起来更方便,效果也很好,但是,它需要我们在那些页面中添加这样的指令。

  对于设置了OutputCache的页面,浏览器会在收到该页面的响应后缓存页面响应内容。只要在指定的缓存时间内,用户不强制刷新操作,根本不会再次请求服务器,而对于其他浏览器的请求,如果已经生成了缓存页面,则可以直接从缓存。响应中间的请求,加快响应速度。因此,OutputCache 指令对性能优化很有意义(除非所有页面都经常更新)。

  在网站的优化阶段,我们可以使用Fiddler等工具找到一些内容几乎不会发生变化的页面,并为其设置OutputCache。但是,按照传统的开发流程,我们需要对每个页面文件执行以下操作:

  方法一、使用ASP.NET平台提供的OutputCache(运行时内存)机制。实现方法:为列表页配置输出缓存声明;根据页面的不同查询参数缓存不同的页面。eg也是一个列表页,分类1页:和类别二页:分别缓存到不同的缓存对象中优点:配置简单,不侵入原系统,访问速度非常快(因为执行结果缓存在内存中)缺点:它占用大量资源,无法缓存所有页面。还有一个问题:使用时间长了缓存访问速度变慢了;二、方法也使用了ASP.NET平台提供的OutputCache(自定义Provider)机制。实现方法:开发一个自定义提供程序并将其提供给 ASP .NET 调用以访问输出缓存;其他方法相同。优点一:可以独立控制缓存的读写,不侵入原系统,访问速度快。自定义 OutputCacheProvider 实现将输出缓存放入分布式缓存中

  实施原则:

  

  实现方法:

  实现抽象基类OutputCacheProvider的增删改查方法;附上大概代码(简单通过文件读写、JSON序列化和二进制序列化),思路仅供参考:

  输出缓存提供者

   1 using System;

2 using System.Collections.Generic;

3 using System.Diagnostics;

4 using System.IO;

5 using System.Linq;

6 using System.Runtime.Serialization.Formatters.Binary;

7 using System.Security.Cryptography;

8 using System.Text;

9 using System.Web.Caching;

10 using Micua.Utility;

11 namespace Micua.UI.Cache

12 {

13 public class MicuaOutputCacheProvider : OutputCacheProvider, IDisposable

14 {

15 readonly IList _cacheItems;

16 public MicuaOutputCacheProvider()

17 {

18 if (File.Exists("z:\\cache.json"))

19 {

20 var json = File.ReadAllText("z:\\cache.json");

21 _cacheItems = JsonHelper.Deserialize(json) ?? new List();

22 }

23 else

24 {

25 _cacheItems = new List();

26 }

27 }

28 public override object Get(string key)

29 {

30 Debug.WriteLine(string.Format("Cache.Get({0})", key));

31 key = MD5(key);

32 var cacheItem = _cacheItems.FirstOrDefault(c => c.Id == key);

33 if (cacheItem != null)

34 {

35 if (cacheItem.Expiration.ToUniversalTime() c.Id == key);

53 if (item != null)

54 {

55 if (item.Expiration.ToUniversalTime() c.Id == key);

79 if (item != null)

80 {

81 _cacheItems.Remove(item);

82 item.Item = Serialize(entry);

83 item.Expiration = utcExpiry;

84 _cacheItems.Add(item);

85 }

86 else

87 {

88 _cacheItems.Add(new CacheItem

89 {

90 Id = key,

91 Item = Serialize(entry),

92 Expiration = utcExpiry

93 });

94 }

95 SaveChange();

96 }

97 public override void Remove(string key)

98 {

99 Debug.WriteLine("Cache.Remove({0})", key);

100 key = MD5(key);

101 _cacheItems.Remove(_cacheItems.FirstOrDefault(c => c.Id == key));

102 SaveChange();

103 }

104 private static string MD5(string value)

105 {

106 var cryptoServiceProvider = new MD5CryptoServiceProvider();

107 var bytes = Encoding.UTF8.GetBytes(value);

108 var builder = new StringBuilder();

109 bytes = cryptoServiceProvider.ComputeHash(bytes);

110 foreach (var b in bytes)

111 builder.Append(b.ToString("x2").ToLower());

112 return builder.ToString();

113 }

114 private static byte[] Serialize(object entry)

115 {

116 var formatter = new BinaryFormatter();

117 var stream = new MemoryStream();

118 formatter.Serialize(stream, entry);

119 return stream.ToArray();

120 }

121 private static object Deserialize(byte[] serializedEntry)

122 {

123 var formatter = new BinaryFormatter();

124 var stream = new MemoryStream(serializedEntry);

125 return formatter.Deserialize(stream);

126 }

127 private void SaveChange()

128 {

129 var json = JsonHelper.Serialize(_cacheItems);

130 File.WriteAllText("z:\\cache.json", json);

131 }

132 public void Dispose()

133 {

134 var json = JsonHelper.Serialize(_cacheItems);

135 File.WriteAllText("z:\\cache.json", json);

136 }

137 }

138 }

  分类:

  技术要点:

  相关文章:

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线