from memory cache与from disk cache详解

在chrome浏览器中的控制台Network中size栏通常会有三种状态

1.from memory cache

2.from disk cache

3.资源本身的大小(如:1.5k)

那么问题来了

1.三种区别在哪里;2.浏览器采取不同措施的原则是什么;3.其他浏览器的策略

下面分别讲述以上三个问题

1.三种区别在哪

from memory cache:字面理解是从内存中,其实也是字面的含义,这个资源是直接从内存中拿到的,不会请求服务器一般已经加载过该资源且缓存在了内存当中,当关闭该页面时,此资源就被内存释放掉了,再次重新打开相同页面时不会出现from memory cache的情况

from disk cache:同上类似,此资源是从磁盘当中取出的,也是在已经在之前的某个时间加载过该资源,不会请求服务器但是此资源不会随着该页面的关闭而释放掉,因为是存在硬盘当中的,下次打开仍会from disk cache

资源本身大小数值:当http状态为200是实实在在从浏览器获取的资源,当http状态为304时该数字是与服务端通信报文的大小,并不是该资源本身的大小,该资源是从本地获取的

2.chrome采取措施的准则

什么时候是from memory cache 什么时候是from disk cache 呢?

即哪些资源会放在内存当中,哪些资源浏览器会放在磁盘上呢,结果如下下表所示

统计表
状态类型说明
200form memory cache

不请求网络资源,资源在内存当中,一般脚本、字体、图片会存在内存当中

200form disk ceche不请求网络资源,在磁盘当中,一般非脚本会存在内存当中,如css等
200资源大小数值从服务器下载最新资源
304报文大小请求服务端发现资源没有更新,使用本地资源

 

以上是chrome在请求资源是最常见的两种http状态码

由此可见样式表一般在磁盘中,不会缓存到内存中去,因为css样式加载一次即可渲染出网页

但是脚本却可能随时会执行,如果脚本在磁盘当中,在执行该脚本需要从磁盘中取到内存当中来

这样的IO开销是比较大的,有可能会导致浏览器失去响应

3.不同浏览器策略是否一致

以上的数据及统计都是在chrome浏览器下进行的

在Firefox下并没有from memory cache以及from disk cache的状态展现

相同的资源在chrome下是from disk/memory cache,但是Firefox统统是304状态码

即Firefox下会缓存资源,但是每次都会请求服务器对比当前缓存是否更改,chrome不请求服务器,直接拿过来用

这也是为啥chrome比较快的原因之一吧,

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# MemoryCache is a caching mechanism provided by the .NET Framework that allows developers to store data in memory for faster access. It is a key-value store where data is stored as key-value pairs and can be accessed using the key. MemoryCache is designed to be used in applications that require frequent access to data, such as web applications that need to retrieve data from a database repeatedly. By storing frequently accessed data in MemoryCache, you can reduce the number of database calls and improve the performance of your application. MemoryCache provides several features, such as time-based expiration, sliding expiration, and the ability to remove items from the cache manually. It also provides thread-safe operations to prevent race conditions when multiple threads access the cache simultaneously. To use MemoryCache in your C# application, you can create an instance of the MemoryCache class and add items to the cache using the Add method. You can also retrieve items from the cache using the Get method and remove items from the cache using the Remove method. Here's an example of how to use MemoryCache in C#: ``` // Create a new instance of the MemoryCache class MemoryCache cache = MemoryCache.Default; // Add an item to the cache with a key of "myKey" and a value of "myValue" cache.Add("myKey", "myValue", DateTimeOffset.Now.AddMinutes(5)); // Retrieve the value of the item with a key of "myKey" string value = cache.Get("myKey") as string; // Remove the item from the cache with a key of "myKey" cache.Remove("myKey"); ``` Overall, MemoryCache is a powerful tool for improving the performance of your C# applications by reducing the number of database calls and providing faster access to frequently accessed data.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值