This project implements a Python LRU (Least Recently Used) cache, enhanced with TTL (time to live) expiration and a priority-based eviction strategy.
- TTL:: old items get expired
- Priority-based eviction: removes low-priority items first
- Thread-safe: uses
Locksto handle concurrency
- Returns the value of the item with the given key, if present and Moves the accessed item to the front.
- Returns
Noneif the key is not present.
- Inserts a new item or updates an existing one.
- The item expires after time to live (ttl) number of seconds.
- If the cache is full:
- Expired items are evicted first.
- If no expired items, items with the lowest priority are removed.
- If multiple items share the lowest priority, the least recently used one is removed.
- Clears all items from the cache.
- Returns a string representing the cache contents.
- Returns
Trueif the key is stored in the cache.
- Returns the number of items stored in the cache.
from src.cache import Cache
cache = Cache(capacity=3)
cache.put(1, "apple", time.time()+120, priority=2)
cache.put(2, "banana", time.time()+120, priority=3)
print(cache.get(1))
print(cache)
print(len(cache))