|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections import deque |
| 4 | +from typing import Any |
| 5 | + |
| 6 | + |
| 7 | +class KeyBufferCache: |
| 8 | + """ |
| 9 | + A cache implementation for a single key with size tracking and eviction support. |
| 10 | +
|
| 11 | + This class manages a buffer for a specific key, keeping track of the current size |
| 12 | + and providing methods to add, remove, and manage cached items. It supports automatic |
| 13 | + eviction tracking and size management. |
| 14 | +
|
| 15 | + Attributes |
| 16 | + ---------- |
| 17 | + cache : deque |
| 18 | + A double-ended queue storing the cached items. |
| 19 | + current_size : int |
| 20 | + The total size of all items currently in the cache. |
| 21 | + has_evicted : bool |
| 22 | + A flag indicating whether any items have been evicted from the cache. |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__(self): |
| 26 | + """ |
| 27 | + Initialize a buffer cache for a specific key. |
| 28 | + """ |
| 29 | + self.cache: deque = deque() |
| 30 | + self.current_size: int = 0 |
| 31 | + self.has_evicted: bool = False |
| 32 | + |
| 33 | + def add(self, item: Any) -> None: |
| 34 | + """ |
| 35 | + Add an item to the cache. |
| 36 | +
|
| 37 | + Parameters |
| 38 | + ---------- |
| 39 | + item : Any |
| 40 | + The item to be stored in the cache. |
| 41 | + """ |
| 42 | + item_size = len(str(item)) |
| 43 | + self.cache.append(item) |
| 44 | + self.current_size += item_size |
| 45 | + |
| 46 | + def remove_oldest(self) -> Any: |
| 47 | + """ |
| 48 | + Remove and return the oldest item from the cache. |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + Any |
| 53 | + The removed item. |
| 54 | + """ |
| 55 | + removed_item = self.cache.popleft() |
| 56 | + self.current_size -= len(str(removed_item)) |
| 57 | + self.has_evicted = True |
| 58 | + return removed_item |
| 59 | + |
| 60 | + def get(self) -> list: |
| 61 | + """ |
| 62 | + Retrieve items for this key. |
| 63 | +
|
| 64 | + Returns |
| 65 | + ------- |
| 66 | + list |
| 67 | + List of items in the cache. |
| 68 | + """ |
| 69 | + return list(self.cache) |
| 70 | + |
| 71 | + def clear(self) -> None: |
| 72 | + """ |
| 73 | + Clear the cache for this key. |
| 74 | + """ |
| 75 | + self.cache.clear() |
| 76 | + self.current_size = 0 |
| 77 | + self.has_evicted = False |
| 78 | + |
| 79 | + |
| 80 | +class LoggerBufferCache: |
| 81 | + """ |
| 82 | + A multi-key buffer cache with size-based eviction and management. |
| 83 | +
|
| 84 | + This class provides a flexible caching mechanism that manages multiple keys, |
| 85 | + with each key having its own buffer cache. The total size of each key's cache |
| 86 | + is limited, and older items are automatically evicted when the size limit is reached. |
| 87 | +
|
| 88 | + Key Features: |
| 89 | + - Multiple key support |
| 90 | + - Size-based eviction |
| 91 | + - Tracking of evicted items |
| 92 | + - Configurable maximum buffer size |
| 93 | +
|
| 94 | + Example |
| 95 | + -------- |
| 96 | + >>> buffer_cache = LoggerBufferCache(max_size_bytes=1000) |
| 97 | + >>> buffer_cache.add("logs", "First log message") |
| 98 | + >>> buffer_cache.add("debug", "Debug information") |
| 99 | + >>> buffer_cache.get("logs") |
| 100 | + ['First log message'] |
| 101 | + >>> buffer_cache.get_current_size("logs") |
| 102 | + 16 |
| 103 | + """ |
| 104 | + |
| 105 | + def __init__(self, max_size_bytes: int): |
| 106 | + """ |
| 107 | + Initialize the LoggerBufferCache. |
| 108 | +
|
| 109 | + Parameters |
| 110 | + ---------- |
| 111 | + max_size_bytes : int |
| 112 | + Maximum size of the cache in bytes for each key. |
| 113 | + """ |
| 114 | + self.max_size_bytes: int = max_size_bytes |
| 115 | + self.cache: dict[str, KeyBufferCache] = {} |
| 116 | + |
| 117 | + def add(self, key: str, item: Any) -> None: |
| 118 | + """ |
| 119 | + Add an item to the cache for a specific key. |
| 120 | +
|
| 121 | + Parameters |
| 122 | + ---------- |
| 123 | + key : str |
| 124 | + The key to store the item under. |
| 125 | + item : Any |
| 126 | + The item to be stored in the cache. |
| 127 | +
|
| 128 | + Returns |
| 129 | + ------- |
| 130 | + bool |
| 131 | + True if item was added, False otherwise. |
| 132 | + """ |
| 133 | + # Check if item is larger than entire buffer |
| 134 | + item_size = len(str(item)) |
| 135 | + if item_size > self.max_size_bytes: |
| 136 | + raise BufferError("Cannot add item to the buffer") |
| 137 | + |
| 138 | + # Create the key's cache if it doesn't exist |
| 139 | + if key not in self.cache: |
| 140 | + self.cache[key] = KeyBufferCache() |
| 141 | + |
| 142 | + # Calculate the size after adding the new item |
| 143 | + new_total_size = self.cache[key].current_size + item_size |
| 144 | + |
| 145 | + # If adding the item would exceed max size, remove oldest items |
| 146 | + while new_total_size > self.max_size_bytes and self.cache[key].cache: |
| 147 | + self.cache[key].remove_oldest() |
| 148 | + new_total_size = self.cache[key].current_size + item_size |
| 149 | + |
| 150 | + self.cache[key].add(item) |
| 151 | + |
| 152 | + def get(self, key: str) -> list: |
| 153 | + """ |
| 154 | + Retrieve items for a specific key. |
| 155 | +
|
| 156 | + Parameters |
| 157 | + ---------- |
| 158 | + key : str |
| 159 | + The key to retrieve items for. |
| 160 | +
|
| 161 | + Returns |
| 162 | + ------- |
| 163 | + list |
| 164 | + List of items for the given key, or an empty list if the key doesn't exist. |
| 165 | + """ |
| 166 | + return [] if key not in self.cache else self.cache[key].get() |
| 167 | + |
| 168 | + def clear(self, key: str | None = None) -> None: |
| 169 | + """ |
| 170 | + Clear the cache, either for a specific key or entirely. |
| 171 | +
|
| 172 | + Parameters |
| 173 | + ---------- |
| 174 | + key : Optional[str], optional |
| 175 | + The key to clear. If None, clears the entire cache. |
| 176 | + """ |
| 177 | + if key: |
| 178 | + if key in self.cache: |
| 179 | + self.cache[key].clear() |
| 180 | + del self.cache[key] |
| 181 | + else: |
| 182 | + self.cache.clear() |
| 183 | + |
| 184 | + def has_items_evicted(self, key: str) -> bool: |
| 185 | + """ |
| 186 | + Check if a specific key's cache has evicted items. |
| 187 | +
|
| 188 | + Parameters |
| 189 | + ---------- |
| 190 | + key : str |
| 191 | + The key to check for evicted items. |
| 192 | +
|
| 193 | + Returns |
| 194 | + ------- |
| 195 | + bool |
| 196 | + True if items have been evicted, False otherwise. |
| 197 | + """ |
| 198 | + return False if key not in self.cache else self.cache[key].has_evicted |
| 199 | + |
| 200 | + def get_current_size(self, key: str) -> int | None: |
| 201 | + """ |
| 202 | + Get the current size of the buffer for a specific key. |
| 203 | +
|
| 204 | + Parameters |
| 205 | + ---------- |
| 206 | + key : str |
| 207 | + The key to get the current size for. |
| 208 | +
|
| 209 | + Returns |
| 210 | + ------- |
| 211 | + int |
| 212 | + The current size of the buffer for the key. |
| 213 | + Returns 0 if the key does not exist. |
| 214 | + """ |
| 215 | + return None if key not in self.cache else self.cache[key].current_size |
0 commit comments