L

Caché LRU

LRU

Una caché LRU es un sistema de gestión de memoria que prioriza la recencia de uso, eliminando primero los elementos menos recientemente utilizados.

Caché LRU

A Least Recently Used (LRU) Cache is a type of data structure that stores a limited number of items while efficiently managing their retrieval and storage based on usage history. The primary goal of an LRU Cache is to ensure that the most recently accessed items are kept readily available, while the least recently accessed items are removed when the cache reaches its capacity.

The LRU caching algorithm works on the principle that data which has been accessed recently is more likely to be accessed again in the near future. Thus, when the cache is full and a new item needs to be added, the cache will evict the item that has not been used for the longest period of time. This eviction strategy helps optimize performance, particularly in applications that require frequent access to a limited subset of data, such as navegadores web, databases, and applications with limited memory resources.

Implementación

Por lo general, una Caché LRU se implementa utilizando una combinación de un hash map and a doubly linked list. The hash map allows for O(1) average time complexity for both retrieval and insertion of cache items, while the doubly linked list maintains the order of usage. When an item is accessed, it is moved to the front of the list, indicating that it is the most recently used. Conversely, when an eviction occurs, the item at the back of the list—the least recently used item—is removed.

Casos de uso

Las cachés LRU se utilizan ampliamente en varias aplicaciones, como:

  • Caché web para almacenar páginas web accedidas recientemente.
  • Caché de consultas a bases de datos para acelerar recuperación de datos.
  • Memoria management en sistemas operativos para optimizar el uso de recursos.

Al implementar una Caché LRU, los sistemas pueden reducir significativamente latency y mejorar la eficiencia al tratar con patrones de acceso de alta frecuencia.

oEmbed (JSON) + /