File size: 19,435 Bytes
50a7bf0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
"""
File caching utilities for improved performance.
This module provides caching strategies for file metadata,
thumbnails, and frequently accessed files.
"""
import asyncio
import hashlib
import json
from datetime import datetime, timedelta
from pathlib import Path
from typing import Optional, Dict, Any, List
import aiofiles
from redis.asyncio import Redis
from ..core.redis import get_redis, redis_json_get, redis_json_set
from ..core.config import get_settings
from ..core.logger import get_logger
settings = get_settings()
logger = get_logger(__name__)
class FileCacheManager:
"""Manager for file caching operations."""
def __init__(self, redis_client: Optional[Redis] = None):
self.redis_client = redis_client
self.cache_ttl = {
'metadata': 3600, # 1 hour
'thumbnail': 86400, # 1 day
'file_content': 1800, # 30 minutes
'access_stats': 300 # 5 minutes
}
async def cache_file_metadata(
self,
file_id: str,
metadata: Dict[str, Any],
ttl: Optional[int] = None
) -> bool:
"""
Cache file metadata in Redis.
Args:
file_id: File identifier
metadata: Metadata to cache
ttl: Time to live in seconds
Returns:
True if cached successfully
"""
try:
if not self.redis_client:
return False
cache_key = f"file_cache:metadata:{file_id}"
cache_ttl = ttl or self.cache_ttl['metadata']
await redis_json_set(
self.redis_client,
cache_key,
metadata,
ex=cache_ttl
)
logger.debug(f"Cached metadata for file {file_id}")
return True
except Exception as e:
logger.error(f"Failed to cache metadata: {e}")
return False
async def get_cached_metadata(self, file_id: str) -> Optional[Dict[str, Any]]:
"""
Get cached file metadata.
Args:
file_id: File identifier
Returns:
Cached metadata or None if not found
"""
try:
if not self.redis_client:
return None
cache_key = f"file_cache:metadata:{file_id}"
return await redis_json_get(self.redis_client, cache_key)
except Exception as e:
logger.error(f"Failed to get cached metadata: {e}")
return None
async def cache_thumbnail_path(
self,
file_id: str,
size: str,
thumbnail_path: str,
ttl: Optional[int] = None
) -> bool:
"""
Cache thumbnail file path.
Args:
file_id: File identifier
size: Thumbnail size
thumbnail_path: Path to thumbnail file
ttl: Time to live in seconds
Returns:
True if cached successfully
"""
try:
if not self.redis_client:
return False
cache_key = f"file_cache:thumbnail:{file_id}:{size}"
cache_ttl = ttl or self.cache_ttl['thumbnail']
await self.redis_client.set(cache_key, thumbnail_path, ex=cache_ttl)
logger.debug(f"Cached thumbnail path for file {file_id}, size {size}")
return True
except Exception as e:
logger.error(f"Failed to cache thumbnail path: {e}")
return False
async def get_cached_thumbnail_path(
self,
file_id: str,
size: str
) -> Optional[str]:
"""
Get cached thumbnail path.
Args:
file_id: File identifier
size: Thumbnail size
Returns:
Cached thumbnail path or None if not found
"""
try:
if not self.redis_client:
return None
cache_key = f"file_cache:thumbnail:{file_id}:{size}"
result = await self.redis_client.get(cache_key)
return result.decode() if isinstance(result, bytes) else result
except Exception as e:
logger.error(f"Failed to get cached thumbnail path: {e}")
return None
async def cache_file_access_stats(
self,
file_id: str,
user_id: str,
access_type: str = "download"
) -> bool:
"""
Cache file access statistics.
Args:
file_id: File identifier
user_id: User who accessed the file
access_type: Type of access (download, view, stream)
Returns:
True if cached successfully
"""
try:
if not self.redis_client:
return False
# Increment access counter
counter_key = f"file_cache:access_count:{file_id}"
await self.redis_client.incr(counter_key)
await self.redis_client.expire(counter_key, 86400) # 1 day
# Store recent access
access_key = f"file_cache:recent_access:{file_id}"
access_data = {
"user_id": user_id,
"access_type": access_type,
"timestamp": datetime.utcnow().isoformat()
}
# Add to list (keep last 100 accesses)
await self.redis_client.lpush(access_key, json.dumps(access_data))
await self.redis_client.ltrim(access_key, 0, 99)
await self.redis_client.expire(access_key, 86400) # 1 day
logger.debug(f"Cached access stats for file {file_id}")
return True
except Exception as e:
logger.error(f"Failed to cache access stats: {e}")
return False
async def get_file_access_stats(self, file_id: str) -> Dict[str, Any]:
"""
Get file access statistics.
Args:
file_id: File identifier
Returns:
Dictionary with access statistics
"""
try:
if not self.redis_client:
return {}
# Get access count
counter_key = f"file_cache:access_count:{file_id}"
access_count = await self.redis_client.get(counter_key)
access_count = int(access_count) if access_count else 0
# Get recent accesses
access_key = f"file_cache:recent_access:{file_id}"
recent_accesses = await self.redis_client.lrange(access_key, 0, 9) # Last 10
recent_access_data = []
for access in recent_accesses:
try:
access_data = json.loads(access.decode() if isinstance(access, bytes) else access)
recent_access_data.append(access_data)
except json.JSONDecodeError:
continue
return {
"total_accesses": access_count,
"recent_accesses": recent_access_data
}
except Exception as e:
logger.error(f"Failed to get access stats: {e}")
return {}
async def cache_file_content_hash(
self,
file_path: str,
content_hash: str,
ttl: Optional[int] = None
) -> bool:
"""
Cache file content hash for integrity checking.
Args:
file_path: Path to file
content_hash: Hash of file content
ttl: Time to live in seconds
Returns:
True if cached successfully
"""
try:
if not self.redis_client:
return False
cache_key = f"file_cache:hash:{hashlib.md5(file_path.encode()).hexdigest()}"
cache_ttl = ttl or self.cache_ttl['file_content']
await self.redis_client.set(cache_key, content_hash, ex=cache_ttl)
logger.debug(f"Cached content hash for file {file_path}")
return True
except Exception as e:
logger.error(f"Failed to cache content hash: {e}")
return False
async def get_cached_content_hash(self, file_path: str) -> Optional[str]:
"""
Get cached file content hash.
Args:
file_path: Path to file
Returns:
Cached content hash or None if not found
"""
try:
if not self.redis_client:
return None
cache_key = f"file_cache:hash:{hashlib.md5(file_path.encode()).hexdigest()}"
result = await self.redis_client.get(cache_key)
return result.decode() if isinstance(result, bytes) else result
except Exception as e:
logger.error(f"Failed to get cached content hash: {e}")
return None
async def invalidate_file_cache(self, file_id: str) -> bool:
"""
Invalidate all cached data for a file.
Args:
file_id: File identifier
Returns:
True if invalidated successfully
"""
try:
if not self.redis_client:
return False
# Get all cache keys for this file
patterns = [
f"file_cache:metadata:{file_id}",
f"file_cache:thumbnail:{file_id}:*",
f"file_cache:access_count:{file_id}",
f"file_cache:recent_access:{file_id}"
]
deleted_count = 0
for pattern in patterns:
if '*' in pattern:
# Handle wildcard patterns
keys = await self.redis_client.keys(pattern)
if keys:
deleted_count += await self.redis_client.delete(*keys)
else:
# Handle exact keys
deleted_count += await self.redis_client.delete(pattern)
logger.info(f"Invalidated {deleted_count} cache entries for file {file_id}")
return True
except Exception as e:
logger.error(f"Failed to invalidate cache: {e}")
return False
async def cleanup_expired_cache(self) -> Dict[str, int]:
"""
Clean up expired cache entries.
Returns:
Dictionary with cleanup statistics
"""
try:
if not self.redis_client:
return {"cleaned": 0}
# Redis automatically handles TTL expiration, but we can clean up
# any orphaned entries or perform maintenance
# Get all file cache keys
cache_patterns = [
"file_cache:metadata:*",
"file_cache:thumbnail:*",
"file_cache:hash:*"
]
total_keys = 0
for pattern in cache_patterns:
keys = await self.redis_client.keys(pattern)
total_keys += len(keys)
logger.info(f"File cache contains {total_keys} entries")
return {"total_entries": total_keys, "cleaned": 0}
except Exception as e:
logger.error(f"Failed to cleanup cache: {e}")
return {"cleaned": 0}
async def get_cache_stats(self) -> Dict[str, Any]:
"""
Get cache statistics.
Returns:
Dictionary with cache statistics
"""
try:
if not self.redis_client:
return {}
stats = {}
# Count entries by type
cache_types = {
"metadata": "file_cache:metadata:*",
"thumbnails": "file_cache:thumbnail:*",
"access_counts": "file_cache:access_count:*",
"content_hashes": "file_cache:hash:*"
}
for cache_type, pattern in cache_types.items():
keys = await self.redis_client.keys(pattern)
stats[f"{cache_type}_count"] = len(keys)
# Get memory usage info if available
try:
info = await self.redis_client.info("memory")
stats["memory_used"] = info.get("used_memory_human", "unknown")
except Exception:
pass
return stats
except Exception as e:
logger.error(f"Failed to get cache stats: {e}")
return {}
async def warm_cache_for_popular_files(self, limit: int = 100) -> Dict[str, int]:
"""
Warm cache for most frequently accessed files.
Args:
limit: Maximum number of files to warm
Returns:
Dictionary with warming statistics
"""
try:
if not self.redis_client:
return {"warmed": 0}
# Get most accessed files
access_pattern = "file_cache:access_count:*"
access_keys = await self.redis_client.keys(access_pattern)
# Get access counts and sort
file_access_counts = []
for key in access_keys:
try:
count = await self.redis_client.get(key)
if count:
file_id = key.decode().split(':')[-1] if isinstance(key, bytes) else key.split(':')[-1]
file_access_counts.append((file_id, int(count)))
except Exception:
continue
# Sort by access count (descending)
file_access_counts.sort(key=lambda x: x[1], reverse=True)
# Warm cache for top files
warmed_count = 0
for file_id, access_count in file_access_counts[:limit]:
try:
# Pre-load metadata if not cached
metadata_key = f"file_cache:metadata:{file_id}"
if not await self.redis_client.exists(metadata_key):
# This would typically load from primary storage
# For now, we'll just mark it as a candidate for warming
logger.debug(f"Cache warming candidate: {file_id} (access count: {access_count})")
warmed_count += 1
except Exception as e:
logger.warning(f"Failed to warm cache for file {file_id}: {e}")
logger.info(f"Cache warming completed: {warmed_count} files processed")
return {
"warmed": warmed_count,
"candidates": len(file_access_counts),
"limit": limit
}
except Exception as e:
logger.error(f"Failed to warm cache: {e}")
return {"warmed": 0}
async def implement_cache_eviction_policy(self) -> Dict[str, int]:
"""
Implement LRU-style cache eviction for memory management.
Returns:
Dictionary with eviction statistics
"""
try:
if not self.redis_client:
return {"evicted": 0}
evicted_count = 0
# Get all metadata cache keys with their TTL
metadata_pattern = "file_cache:metadata:*"
metadata_keys = await self.redis_client.keys(metadata_pattern)
# Check each key's TTL and access patterns
for key in metadata_keys:
try:
ttl = await self.redis_client.ttl(key)
# If TTL is very low (< 300 seconds), consider for eviction
if 0 < ttl < 300:
# Check if file has been accessed recently
file_id = key.decode().split(':')[-1] if isinstance(key, bytes) else key.split(':')[-1]
access_key = f"file_cache:recent_access:{file_id}"
recent_accesses = await self.redis_client.llen(access_key)
# If no recent accesses, evict early
if recent_accesses == 0:
await self.redis_client.delete(key)
evicted_count += 1
logger.debug(f"Evicted cache entry: {file_id}")
except Exception as e:
logger.warning(f"Failed to process cache key {key}: {e}")
logger.info(f"Cache eviction completed: {evicted_count} entries evicted")
return {
"evicted": evicted_count,
"total_checked": len(metadata_keys)
}
except Exception as e:
logger.error(f"Failed to implement cache eviction: {e}")
return {"evicted": 0}
# Global cache manager instance
file_cache_manager = FileCacheManager()
# Utility functions
async def cache_file_metadata(
file_id: str,
metadata: Dict[str, Any],
ttl: Optional[int] = None
) -> bool:
"""Cache file metadata."""
redis_client = await get_redis()
cache_manager = FileCacheManager(redis_client)
return await cache_manager.cache_file_metadata(file_id, metadata, ttl)
async def get_cached_file_metadata(file_id: str) -> Optional[Dict[str, Any]]:
"""Get cached file metadata."""
redis_client = await get_redis()
cache_manager = FileCacheManager(redis_client)
return await cache_manager.get_cached_metadata(file_id)
async def track_file_access(
file_id: str,
user_id: str,
access_type: str = "download"
) -> bool:
"""Track file access for statistics."""
redis_client = await get_redis()
cache_manager = FileCacheManager(redis_client)
return await cache_manager.cache_file_access_stats(file_id, user_id, access_type)
async def get_file_access_statistics(file_id: str) -> Dict[str, Any]:
"""Get file access statistics."""
redis_client = await get_redis()
cache_manager = FileCacheManager(redis_client)
return await cache_manager.get_file_access_stats(file_id) |