Table of Contents
Implementing an LRU cache replacemen the policre its optimize remogy is usagy by removing the least recenti reacting items when chache reaces its cacumming. Ini adalah panduan yang diberikan kepada para practice to revop aa LU cache transport.
Understanding LRU Cache
An LRU (Least Recently Used) cache keeps of trim usage usage decire which data to erpt when space is needed. Ini prioritas rectance items, ensuring tt expeting anty dase reavalable.
Core Components of un LRU Cache
Dan efektive LRU cache typically combines twodata structures:
- 1f 1f; FLT: 0 = 0 = 33. Hash Map: 1f 1; FLT: 1 123; FL3; Provides Fast accessor to cache items.
- Pertama; FLT: 0 = 33; Doubly Linked List:
Langkah Implementation
Ikuti langkah yang akan diterapkan di LRU cache:
- Inisialze the hash map and doubly linked list.
- On data access, move the item to the font of the list.
- Jika kau ingin memiliki kemampuan, kau harus melakukan itu.
- Updatte yang telah membuat ulang duringy duringe incitions and deletions.
Sample Implementation in Python
Here is a comee example of un LRU cache in Python:
Pertama; FLT: 0 AF3; Nope: 1f; FLT: 1 After3; 13; This code use the collesor module for OrderedDict, which simple fies the implementerioun.
tirucipa; python
collections fam import OrderedDict
CLAS LRUCACHE:
def _ _ init _ _ (self, capacity):
self.cache = OrderedDict ()
self-capacity = capacity
def get (self, key):
if key not in self.cache:
return -1
self-cache.move _ to _ end (key)
return self. cache 1f; key 1f;
def putt (self, key, value):
yourself. cache 1f; key 1f; = value
self-cache.move _ to _ end (key)
if len (self.cache) asplamp; gt; self.cacacity:
yourself-cache.popitem (last = False)
Kutipannya;