3.4. Cache - 3.4.3 MFU Cache

2024. 1. 27. 21:02ใ†CS - Roadmap.sh/3. Common Algorithms

CS - 3. Common Algorithms - 3.4. Cache - 3.4.3 MFU Cache

 

๐Ÿ’กMFU Cache is another cache algorithm. The difference is that instead of deleting the least frequently used entry, the MFU Cache deletes the most frequently used entry.

MFU ์บ์‹œ๋Š” ๋˜ ๋‹ค๋ฅธ ์บ์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค. ์ฐจ์ด์ ์€ ์‚ฌ์šฉ ๋นˆ๋„๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•˜๋Š” ๋Œ€์‹  ๊ฐ€์žฅ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

 

MFU(Most Frequently Used) Cache๋Š” ์ด๋ฆ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด 
๊ฐ€์žฅ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ•ญ๋ชฉ์„ ์บ์‹œ์— ๋ณด๊ด€ํ•˜๋Š” ์บ์‹ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ ํ•ญ๋ชฉ์˜ ์‚ฌ์šฉ ๋นˆ๋„๋ฅผ ์ถ”์ ํ•˜๋ฉฐ, ์บ์‹œ๊ฐ€ ๊ฐ€๋“ ์ฐผ์„ ๋•Œ ๊ฐ€์žฅ ์ž์ฃผ ์‚ฌ์šฉ๋œ ํ•ญ๋ชฉ์„ ์บ์‹œ์— ์œ ์ง€ํ•˜๊ณ  ๊ฐ€์žฅ ์ ๊ฒŒ ์‚ฌ์šฉ๋œ ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
์ด๋Š” ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ•ญ๋ชฉ์ด ๋ฏธ๋ž˜์—๋„ ๊ณ„์† ์‚ฌ์šฉ๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค๋Š” ๊ฐ€์ •์— ๊ธฐ๋ฐ˜์„ ๋‘๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

In Python
from collections import Counter

class MFUCache:
    def __init__(self, capacity: int):
        self.capacity = capacity
        self.cache = {}
        self.counter = Counter()

    def get(self, key: int) -> int:
        if key not in self.cache:
            return -1
        self.counter[key] += 1
        return self.cache[key]

    def put(self, key: int, value: int) -> None:
        if len(self.cache) >= self.capacity and key not in self.cache:
            mfu_key = self.counter.most_common(1)[0][0]
            del self.cache[mfu_key]
            del self.counter[mfu_key]
        self.cache[key] = value
        self.counter[key] += 1

 

MFU ์บ์‰ฌ๋ฅผ ํŒŒ์ด์ฌ์—์„œ ๊ตฌํ˜„ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

get ํ•จ์ˆ˜๋Š” ํŠน์ • ํ‚ค์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์‚ฌ์šฉ ๋นˆ๋„๋ฅผ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค.

put ํ•จ์ˆ˜๋Š” ์ƒˆ๋กœ์šด ๊ฐ’์„ ์บ์‹œ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์บ์‹œ์˜ ์šฉ๋Ÿ‰์ด ์ดˆ๊ณผ๋˜๋ฉด ๊ฐ€์žฅ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ ํ•ญ๋ชฉ์„ ์บ์‹œ์—์„œ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

Counter ํด๋ž˜์Šค๋Š” ํ•ญ๋ชฉ์˜ ์‚ฌ์šฉ ๋นˆ๋„๋ฅผ ์ถ”์ ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 

MFU Cache๋Š” ๋ฐ์ดํ„ฐ์˜ ์‚ฌ์šฉ ํŒจํ„ด์ด ์ผ์ •ํ•˜๊ณ  ์˜ˆ์ธก ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์— ํŠนํžˆ ํšจ๊ณผ์ ์ž…๋‹ˆ๋‹ค. 
์˜ˆ๋ฅผ ๋“ค์–ด, ํŠน์ • ์›น ํŽ˜์ด์ง€๊ฐ€ ์ž์ฃผ ์กฐํšŒ๋˜๊ฑฐ๋‚˜ ํŠน์ • ๋ฐ์ดํ„ฐ์…‹์ด ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

LFU ์™€ MFU ์˜ ์ฐจ์ด

LFU(Least Frequently Used)์™€ MFU(Most Frequently Used)๋Š” ๋‘˜ ๋‹ค ์‚ฌ์šฉ ๋นˆ๋„์— ๊ธฐ๋ฐ˜ํ•œ ์บ์‹œ ๊ต์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด์ง€๋งŒ, ์ด ๋‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์บ์‹œ์—์„œ ์–ด๋–ค ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ• ์ง€ ๊ฒฐ์ •ํ•˜๋Š” ๋ฐฉ์‹์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

LFU (Least Frequently Used) ์บ์‹œ: LFU ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ€์žฅ ์ ๊ฒŒ ์‚ฌ์šฉ๋œ ํ•ญ๋ชฉ์„ ์บ์‹œ์—์„œ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. 

์ฆ‰, ์บ์‹œ์— ์ €์žฅ๋œ ํ•ญ๋ชฉ ์ค‘์—์„œ ์‚ฌ์šฉ ๋นˆ๋„๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. 

์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ•ญ๋ชฉ์ด ๋ฏธ๋ž˜์—๋„ ๊ณ„์† ์‚ฌ์šฉ๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค๋Š” ๊ฐ€์ •์— ๊ธฐ๋ฐ˜์„ ๋‘๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

๊ทธ๋Ÿฌ๋‚˜, ํ•œ ๋ฒˆ ๋งŽ์ด ์‚ฌ์šฉ๋œ ํ›„๋กœ๋Š” ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ํ•ญ๋ชฉ์ด ์บ์‹œ์— ๊ณ„์† ๋‚จ์•„์žˆ์„ ์ˆ˜ ์žˆ๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.


MFU (Most Frequently Used) ์บ์‹œ: MFU ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ€์žฅ ์ž์ฃผ ์‚ฌ์šฉ๋œ ํ•ญ๋ชฉ์„ ์บ์‹œ์— ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. 

์ฆ‰, ์บ์‹œ์— ์ €์žฅ๋œ ํ•ญ๋ชฉ ์ค‘์—์„œ ์‚ฌ์šฉ ๋นˆ๋„๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. 

์ด๋Š” ๋นˆ๋„๊ฐ€ ๋†’์€ ํ•ญ๋ชฉ์ด ๋นˆ๋„๊ฐ€ ๋‚ฎ์€ ํ•ญ๋ชฉ๋ณด๋‹ค ๋œ ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ฐ€์ •์— ๊ธฐ๋ฐ˜์„ ๋‘๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

๊ทธ๋Ÿฌ๋‚˜, ์ผ์‹œ์ ์œผ๋กœ ๋นˆ๋„๊ฐ€ ๋†’์•„์ง„ ํ•ญ๋ชฉ์ด ์บ์‹œ์— ์˜ค๋ž˜ ๋‚จ์•„์žˆ์„ ์ˆ˜ ์žˆ๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.


LFU์™€ MFU๋Š” ๋ชจ๋‘ ์‚ฌ์šฉ ๋นˆ๋„๋ฅผ ๊ณ ๋ คํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค๋งŒ, ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ์บ์‹œ์—์„œ ์ œ๊ฑฐํ• ์ง€์— ๋Œ€ํ•œ ๊ธฐ์ค€์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 

728x90

'CS - Roadmap.sh > 3. Common Algorithms' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

3.5 Graph - 3.5.1 Breadth First Search(BFS)  (0) 2024.01.28
3.5 Graph & 3.6 Tree  (0) 2024.01.27
3.4. Cache - 3.4.2 LFU Cache  (2) 2024.01.26
3.4. Cache - 3.4.1 LRU Cache  (2) 2024.01.25
3.3. Search - 3.3.2 Linear Search  (1) 2024.01.24