Cache Service¶
cache
¶
Caching system for Weeb CLI.
This module provides a two-tier caching system with both memory and file-based storage. Supports TTL (time-to-live) for automatic cache expiration.
The cache system is used throughout the application to reduce redundant API calls and improve performance.
Classes:
| Name | Description |
|---|---|
CacheManager |
Main cache manager with memory and file storage |
Functions:
| Name | Description |
|---|---|
cached |
Decorator for automatic function result caching |
get_cache |
Get global cache instance |
Example
Using cache manager::
from weeb_cli.services.cache import get_cache
cache = get_cache()
# Store value
cache.set("search:animecix:naruto", results)
# Retrieve value (max 1 hour old)
results = cache.get("search:animecix:naruto", max_age=3600)
# Clear specific pattern
cache.clear_pattern("search:animecix:")
Using decorator::
from weeb_cli.services.cache import cached
@cached(max_age=1800) # 30 minutes
def expensive_operation(param):
# This result will be cached
return compute_result(param)
CacheManager
¶
Two-tier cache manager with memory and file storage.
Provides fast memory cache with persistent file backup. Automatically handles cache expiration based on TTL (time-to-live).
Attributes:
| Name | Type | Description |
|---|---|---|
cache_dir |
Path
|
Directory for cache file storage. |
_memory_cache |
Dict[str, Tuple[Any, float]]
|
In-memory cache dictionary. |
Source code in weeb_cli/services/cache.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
__init__
¶
Initialize cache manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Directory path for storing cache files. |
required |
Source code in weeb_cli/services/cache.py
get
¶
Retrieve cached value if not expired.
Checks memory cache first, then file cache. Automatically removes expired entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
max_age
|
int
|
Maximum age in seconds (default: 1 hour). |
3600
|
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
Cached value if found and not expired, otherwise None. |
Example
cache.get("search:naruto", max_age=1800) [{'id': '1', 'title': 'Naruto'}]
Source code in weeb_cli/services/cache.py
set
¶
Store value in cache.
Stores in both memory and file cache for persistence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
value
|
Any
|
Value to cache (must be JSON-serializable). |
required |
Example
cache.set("search:naruto", results)
Source code in weeb_cli/services/cache.py
delete
¶
Delete cached value.
Removes from both memory and file cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key to delete. |
required |
Source code in weeb_cli/services/cache.py
clear
¶
Clear all cached values.
Removes all entries from memory and deletes all cache files.
Source code in weeb_cli/services/cache.py
clear_pattern
¶
Clear cached values matching a pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
String pattern to match in cache keys. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of entries removed. |
Example
count = cache.clear_pattern("search:animecix:") print(f"Removed {count} entries")
Source code in weeb_cli/services/cache.py
invalidate_provider
¶
Invalidate all cache entries for a provider.
Clears search and details cache for the specified provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name
|
str
|
Provider identifier. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of entries removed. |
Example
cache.invalidate_provider("animecix")
Source code in weeb_cli/services/cache.py
cleanup
¶
Remove expired cache entries.
Removes entries older than max_age from both memory and file cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_age
|
int
|
Maximum age in seconds (default: 24 hours). |
86400
|
Returns:
| Type | Description |
|---|---|
int
|
Number of entries removed. |
Example
Remove entries older than 1 hour¶
count = cache.cleanup(max_age=3600)
Source code in weeb_cli/services/cache.py
get_stats
¶
Get cache statistics.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with cache statistics: - memory_entries: Number of in-memory entries - file_entries: Number of file cache entries - total_size_bytes: Total file cache size in bytes - total_size_mb: Total file cache size in MB |
Example
stats = cache.get_stats() print(f"Cache size: {stats['total_size_mb']} MB")
Source code in weeb_cli/services/cache.py
cached
¶
Decorator for automatic function result caching.
Caches function results based on arguments. Cache key is generated from function name and arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_age
|
int
|
Cache TTL in seconds (default: 1 hour). |
3600
|
cache_manager
|
Optional[CacheManager]
|
Optional custom cache manager instance. |
None
|
Returns:
| Type | Description |
|---|---|
|
Decorator function. |
Example
@cached(max_age=1800) ... def fetch_anime_details(anime_id): ... # Expensive operation ... return api.get_details(anime_id)
First call: executes function¶
details = fetch_anime_details("123")
Second call within 30 min: returns cached result¶
details = fetch_anime_details("123")
Source code in weeb_cli/services/cache.py
get_cache
¶
Get global cache instance.
Returns:
| Type | Description |
|---|---|
CacheManager
|
Global CacheManager instance for application-wide use. |
Example
from weeb_cli.services.cache import get_cache cache = get_cache() cache.set("key", "value")
Source code in weeb_cli/services/cache.py
Overview¶
Two-tier caching system with memory and file-based storage for improved performance.
CacheManager¶
Main cache manager class.
Methods¶
get(): Retrieve cached valueset(): Store value in cachedelete(): Remove cached valueclear(): Clear all cacheclear_pattern(): Clear by patterninvalidate_provider(): Clear provider cachecleanup(): Remove expired entriesget_stats(): Get cache statistics
Usage Examples¶
Basic Caching¶
from weeb_cli.services.cache import get_cache
cache = get_cache()
# Store
cache.set("key", {"data": "value"})
# Retrieve
data = cache.get("key", max_age=3600)
Using Decorator¶
from weeb_cli.services.cache import cached
@cached(max_age=1800)
def expensive_function(param):
# Result cached for 30 minutes
return compute_result(param)
API Reference¶
CacheManager
¶
Two-tier cache manager with memory and file storage.
Provides fast memory cache with persistent file backup. Automatically handles cache expiration based on TTL (time-to-live).
Attributes:
| Name | Type | Description |
|---|---|---|
cache_dir |
Path
|
Directory for cache file storage. |
_memory_cache |
Dict[str, Tuple[Any, float]]
|
In-memory cache dictionary. |
Source code in weeb_cli/services/cache.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
__init__
¶
Initialize cache manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Path
|
Directory path for storing cache files. |
required |
Source code in weeb_cli/services/cache.py
get
¶
Retrieve cached value if not expired.
Checks memory cache first, then file cache. Automatically removes expired entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
max_age
|
int
|
Maximum age in seconds (default: 1 hour). |
3600
|
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
Cached value if found and not expired, otherwise None. |
Example
cache.get("search:naruto", max_age=1800) [{'id': '1', 'title': 'Naruto'}]
Source code in weeb_cli/services/cache.py
set
¶
Store value in cache.
Stores in both memory and file cache for persistence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key. |
required |
value
|
Any
|
Value to cache (must be JSON-serializable). |
required |
Example
cache.set("search:naruto", results)
Source code in weeb_cli/services/cache.py
delete
¶
Delete cached value.
Removes from both memory and file cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Cache key to delete. |
required |
Source code in weeb_cli/services/cache.py
clear
¶
Clear all cached values.
Removes all entries from memory and deletes all cache files.
Source code in weeb_cli/services/cache.py
clear_pattern
¶
Clear cached values matching a pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
String pattern to match in cache keys. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of entries removed. |
Example
count = cache.clear_pattern("search:animecix:") print(f"Removed {count} entries")
Source code in weeb_cli/services/cache.py
invalidate_provider
¶
Invalidate all cache entries for a provider.
Clears search and details cache for the specified provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
provider_name
|
str
|
Provider identifier. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of entries removed. |
Example
cache.invalidate_provider("animecix")
Source code in weeb_cli/services/cache.py
cleanup
¶
Remove expired cache entries.
Removes entries older than max_age from both memory and file cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_age
|
int
|
Maximum age in seconds (default: 24 hours). |
86400
|
Returns:
| Type | Description |
|---|---|
int
|
Number of entries removed. |
Example
Remove entries older than 1 hour¶
count = cache.cleanup(max_age=3600)
Source code in weeb_cli/services/cache.py
get_stats
¶
Get cache statistics.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with cache statistics: - memory_entries: Number of in-memory entries - file_entries: Number of file cache entries - total_size_bytes: Total file cache size in bytes - total_size_mb: Total file cache size in MB |
Example
stats = cache.get_stats() print(f"Cache size: {stats['total_size_mb']} MB")
Source code in weeb_cli/services/cache.py
cached
¶
Decorator for automatic function result caching.
Caches function results based on arguments. Cache key is generated from function name and arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_age
|
int
|
Cache TTL in seconds (default: 1 hour). |
3600
|
cache_manager
|
Optional[CacheManager]
|
Optional custom cache manager instance. |
None
|
Returns:
| Type | Description |
|---|---|
|
Decorator function. |
Example
@cached(max_age=1800) ... def fetch_anime_details(anime_id): ... # Expensive operation ... return api.get_details(anime_id)
First call: executes function¶
details = fetch_anime_details("123")
Second call within 30 min: returns cached result¶
details = fetch_anime_details("123")
Source code in weeb_cli/services/cache.py
get_cache
¶
Get global cache instance.
Returns:
| Type | Description |
|---|---|
CacheManager
|
Global CacheManager instance for application-wide use. |
Example
from weeb_cli.services.cache import get_cache cache = get_cache() cache.set("key", "value")