2024. 1. 25. 20:24ใCS - Roadmap.sh/3. Common Algorithms
CS - 3. Common Algorithms - 3.4. Cache - 3.4.1 LRU Cache
3.4. Cache
๐กCache algorithms are used to manage the cache memory of a computer. Cache memory is a small amount of memory that is used to store data that is frequently accessed. This allows the computer to access the data faster than if it had to go to the main memory. Cache algorithms are used to determine which data should be stored in the cache memory and which data should be removed from the cache memory.
์บ์ ์๊ณ ๋ฆฌ์ฆ์ ์ปดํจํฐ์ ์บ์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ด๋ฆฌํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์บ์ ๋ฉ๋ชจ๋ฆฌ๋ ์์ฃผ ์ก์ธ์คํ๋ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ ๋ฐ ์ฌ์ฉ๋๋ ์๋์ ๋ฉ๋ชจ๋ฆฌ์ ๋๋ค. ์ด๋ฅผ ํตํด ์ปดํจํฐ๋ ์ฃผ ๋ฉ๋ชจ๋ฆฌ๋ก ์ด๋ํด์ผ ํ๋ ๊ฒฝ์ฐ๋ณด๋ค ๋ ๋น ๋ฅด๊ฒ ๋ฐ์ดํฐ์ ์ก์ธ์คํ ์ ์์ต๋๋ค. ์บ์ ์๊ณ ๋ฆฌ์ฆ์ ์บ์ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅํด์ผ ํ ๋ฐ์ดํฐ์ ์บ์ ๋ฉ๋ชจ๋ฆฌ์์ ์ ๊ฑฐํด์ผ ํ ๋ฐ์ดํฐ๋ฅผ ๊ฒฐ์ ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
3.4. Cache - 3.4.1 LRU Cache
๐กLRU cache is a cache that evicts the least recently used item first. It is a very common cache algorithm. It is used in many places, such as in the browser cache, the database cache, and the cache of the operating system.
LRU ์บ์๋ ์ต๊ทผ์ ๊ฐ์ฅ ์ ๊ฒ ์ฌ์ฉ๋ ํญ๋ชฉ์ ๋จผ์ ๊บผ๋ด๋ ์บ์์ ๋๋ค. ๋งค์ฐ ์ผ๋ฐ์ ์ธ ์บ์ ์๊ณ ๋ฆฌ์ฆ์ ๋๋ค. ๋ธ๋ผ์ฐ์ ์บ์, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์บ์, ์ด์ ์ฒด์ ์ ์บ์ ๋ฑ ์ฌ๋ฌ ๊ณณ์์ ์ฌ์ฉ๋ฉ๋๋ค.
LRU ์ " Least Recently Used"์ ์ฝ์์ ๋๋ค.
LRU Cache๋ "๊ฐ์ฅ ์ต๊ทผ์ ์ฌ์ฉ๋์ง ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฅ ๋จผ์ ์ ๊ฑฐํ๋" ์บ์ ์๊ณ ๋ฆฌ์ฆ ์ ๋๋ค.
์บ์๋ ๋ฐ์ดํฐ๋ฅผ ๋ ๋น ๋ฅด๊ฒ ์ ๊ทผํ ์ ์๋ ์์ ์ ์ฅ์์ด๋ฉฐ, LRU Cache๋ ์บ์์ ๊ณต๊ฐ์ ์ ํ์ ์ผ๋ก ์ฌ์ฉํ๋ฉด์๋ ๊ฐ์ฅ ์์ฃผ ์ฌ์ฉ๋๋ ๋ฐ์ดํฐ์ ๋ํ ๋น ๋ฅธ ์ ๊ทผ์ ๋ณด์ฅํฉ๋๋ค.
LRU Cache์ ์๋ ๋ฐฉ์์ ์ด๋ ์ต๋๋ค.
1. ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ฑฐ๋ ์ธ ๋๋ง๋ค ํด๋น ๋ฐ์ดํฐ๋ฅผ ์บ์์ ์ ์ฅํฉ๋๋ค.
์ด๋ ์บ์๋ ๊ณ ์ ๋ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค.
2. ์บ์๊ฐ ๊ฐ๋ ์ฐฌ์ํ์์ ์๋ก์ด ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ ค๊ณ ํ๋ฉด, ๊ฐ์ฅ ์ค๋ ์ ์ ์ฌ์ฉ๋ ๋ฐ์ดํฐ๋ฅผ ์ ๊ฑฐํฉ๋๋ค.
๋ฐ๋๋ก, ์ด๋ฏธ ์บ์์ ์กด์ฌํ๋ ๋ฐ์ดํฐ์ ์ ๊ทผํ๋ฉด ํด๋น ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ฅ ์ต๊ทผ์ ์ฌ์ฉ๋ ๊ฒ์ผ๋ก ๊ฐฑ์ ํฉ๋๋ค.
์ด๋ฐ ๊ณผ์ ์ ํตํด ๊ฐ์ฅ ์ต๊ทผ์ ์ฌ์ฉ๋์ง ์์ ๋ฐ์ดํฐ๋ ์ ๊ฑฐํ๊ณ , ๊ฐ์ฅ ์ต๊ทผ์ ์ฌ์ฉ๋ ๋ฐ์ดํฐ๋ ์บ์์๋จ๊ฒ ๋ฉ๋๋ค.
In Python
ํ์ด์ฌ์์ LRU Cache ๋ฅผ ๊ตฌํํ๊ธฐ ์ํด์๋ collections ๋ชจ๋์ OrderedDict๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
OrderedDict๋ ํญ๋ชฉ์ด ์ถ๊ฐ๋ ์์๋ฅผ ๊ธฐ์ตํฉ๋๋ค. ์ด ์ถ๊ฐ๋ ์์๋ฅผ ๊ธฐ์ตํ๋๊ฒ์ด LRU ์บ์ฌ์ ๋น์ทํ ํน์ฑ์ ๋๋ค.
from collections import OrderedDict
class 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[key]
def put(self, key, value):
if key in self.cache:
self.cache.move_to_end(key)
else:
if len(self.cache) >= self.capacity:
self.cache.popitem(last=False)
self.cache[key] = value
์ ์ฝ๋์์ get() ํจ์๋ key๋ฅผ ์ฌ์ฉํด ๊ฐ์ ๊ฒ์ํ๊ณ , put() ํจ์๋ ์ ๊ฐ์ ์ ์ฅํฉ๋๋ค.
๋ง์ฝ ์บ์๊ฐ ์ด๋ฏธ ๊ฐ๋ ์ฐผ๋ค๋ฉด, ๊ฐ์ฅ ์ค๋๋ ํญ๋ชฉ์ ์ ๊ฑฐํ๊ณ ์ ํญ๋ชฉ์ ์ ์ฅํฉ๋๋ค.
์์ ํจ์๋ ์ ๋ง LRUCache ์ ๊ตฌํ์ด๊ณ
์ฐธ๊ณ )
OrderedDict์ ํ์ฉ์ ์์๋ณด๊ฒ ์ต๋๋ค.
collections ๋ชจ๋์ OrderedDict๋ ์ผ๋ฐ์ ์ธ ๋์ ๋๋ฆฌ๋ ํค์ ์์๋ฅผ ์ ์ง ํ์ง ์์ง๋ง, OrderedDict๋ ์ด๋ฅผ ๊ฐ๋ฅํ๊ฒ ํด์์ ๋๋ค.
์ ์ฝ๋๋ฅผ ์คํ์ํค๋ฉด ํญ๋ชฉ๋ค์ด ์ถ๊ฐ๋ ์์๋๋ก ์ถ๋ ฅ๋ฉ๋๋ค.
ํ์ฉ
์ฃผ๋ก LRU Cache ๋ ์ฃผ๋ก DB๋ ์นํ์ด์ง ์บ์ฑ์ ํ์ฉ๋ฉ๋๋ค.
๋ฐ์ดํฐ ์ ๊ทผ ์๋๋ฅผ ํฅ์์ํค๊ณ , ๋์คํฌ๋ ๋คํธ์ํฌ I/O์ ์ค์ฌ ์์คํ ์ ์ ๋ฐ์ ์ธ ์ฑ๋ฅ์ ํฅ์์ํค๋๋ฐ ๋์์ด ๋ฉ๋๋ค.
๋, ๋น๋ฒํ๊ฒ ์ ๊ทผํ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ์ ์งํ์ฌ ์์ฉ ํ๋ก๊ทธ๋จ์ ์คํ ์๋๋ฅผ ๋์ด๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
์์๋ก
์น ์์ฒญ์ ๋ํ ์๋ต์ ์บ์ฑํ์ฌ ๋น ๋ฅธ ์๋ต ์๊ฐ์ ์ ๊ณตํ๋ ์น ์๋ฒ๋ฅผ ๊ตฌํํ๋ค๊ณ ํด๋ด ์๋ค.
class WebServer:
def __init__(self):
self.cache = LRUCache(5) # ์ต๋ 5๊ฐ์ ์์ฒญ์ ์ ์ฅ
def handle_request(self, request):
response = self.cache.get(request)
if response == -1: # ์บ์์ ์๋ต์ด ์์ผ๋ฉด ์๋ก ์์ฒญ
response = self.process_request(request)
self.cache.put(request, response) # ์บ์์ ์๋ต์ ์ ์ฅ
return response
def process_request(self, request):
# ์ค์ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ์ฝ๋
# ์ง๊ธ ์ด ์์์์๋ ๋จ์ํ ์์ฒญ์ ๋ฐํํ๋๋ก ํฉ๋๋ค.
return request
ํด๋์ค๋ ์์ฒญ์ ์ฒ๋ฆฌํ๋ handle_request ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค.
handle_request๋ LRUCache์ ์์ฒญ์ ๋ํ ์๋ต์ ๊ฒ์ํฉ๋๋ค.
์บ์์ ์๋ต์ด ์์ผ๋ฉด ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ์บ์์ ์๋ต์ ์ ์ฅํฉ๋๋ค.
๋ฐ๋ผ์ ๋์ผํ ์์ฒญ์ด ์ฌ์ฐจ ๋ค์ด์ฌ ๊ฒฝ์ฐ ์บ์ฑํ ์ํ์ด๊ธฐ ๋๋ฌธ์ ๋น ๋ฅด๊ฒ ์๋ต์ ์ ๊ณตํ ์ ์์ต๋๋ค.
'CS - Roadmap.sh > 3. Common Algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
3.4. Cache - 3.4.3 MFU Cache (0) | 2024.01.27 |
---|---|
3.4. Cache - 3.4.2 LFU Cache (2) | 2024.01.26 |
3.3. Search - 3.3.2 Linear Search (1) | 2024.01.24 |
3.3. Search - 3.3.1 Binary Search (0) | 2024.01.23 |
3.2. Recursion - 3.2.2 Non-Tail Recursion (0) | 2024.01.22 |