简体   繁体   English

LRU缓存和内存缓存C#有什么区别

[英]Whats the difference between LRU Caching and Memory Caching C#

Please excuse my noob question as I am still a junior coder , whats the difference between LRU Caching using Dictionary and Linked list and Memory Caching C#, how would one implement a LRU list on say memory cache. 请原谅我的菜鸟问题,因为我还是一名初级编码员,使用Dictionary and Linked List和Memory Caching C#进行LRU Caching的区别是什么,比如说如何在内存缓存上实现LRU list。

Thanks in advance. 提前致谢。

LUR is a algorithm of expiring the cache and adding new Item to your cache. LUR是一种使缓存过期并将新项目添加到缓存的算法。 the this algorithm expires the least recently used Item in your cache when the cache is full. 当缓存已满时,此算法会使缓存中最近最少使用的Item失效。 The MemoryCache is a class in .net 4 and after which is a way of implementing caching inside the heap memory. MemoryCache是​​.net 4中的一个类,之后是在堆内存中实现缓存的一种方法。 Caching can be categorise in different ways base on the media that you cache you can cache on hard drive or memory, based on the location of the memory you can categorize it as in-memory (inside the heap memory) and out-memory (a memory out side the heap for example on another server). 可以根据缓存的介质以不同的方式对缓存进行分类,您可以将其缓存在硬盘驱动器或内存中,也可以根据内存的位置将其分类为内存中(堆内存中)和内存外(a内存在堆之外(例如在另一台服务器上)。 MemoryCaching in c# uses in-memory and you have to be careful because it can use all the memory of your application. c#中的MemoryCaching使用内存,因此您必须小心,因为它会占用应用程序的所有内存。 So its better not to use it if you have more than one node. 因此,如果您有多个节点,最好不要使用它。

One another things you have to take into consideration is that you when you cache an object in an out-memory the object should be serializable. 您还必须考虑的另一件事是,当您在外存储器中缓存对象时,该对象应该可序列化。 But the in-memory caching can cache any object without serializing. 但是内存中缓存可以缓存任何对象而无需序列化。

Least-recently-used (LRU) evicts the key-value used-the-least when the cache is full and it needs to add a value. 当高速缓存已满并且需要添加一个值时,最近最少使用(LRU)会淘汰最少使用的键值。 Whereas a MemoryCache evicts the oldest key-values, or those past their 'use-by-date' if they happen to have one. 而MemoryCache会将最旧的键值逐出,如果它们碰巧有一个,则将其移出“按日期使用”。

Say if the first key-value you added is vital and you happened to read all the time, well in a LRU cache it would be kept, but in a memoryCache it would eventually disappear and need to be replaced. 假设您添加的第一个键值至关重要,并且您始终无时无刻不在读取,那么可以将其保留在LRU缓存中,但是在memoryCache中,它最终会消失并且需要替换。 Sometimes though older key-values disappearing is what your after, so up-to-date values get pulled through from your backend (eg database). 有时候,尽管旧键值消失了,您却需要这样做,所以最新值会从后端(例如数据库)中获取。

Consider also if adding a existing key-value should be considered as being a 'used' (so recently updated stuff tends to stay around) or if 'used' is only when you read a key-value, so you just favour the things your reader likes. 还应考虑添加现有键值是否应被视为“已使用”(因此,最近更新的内容往往会存在)或仅当您读取键值时才使用“已使用”,因此您只喜欢自己喜欢的东西读者喜欢。 As always I would consider concurrency if you have more than one task or thread using it. 与往常一样,如果您有多个任务或线程正在使用并发,我会考虑并发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM