-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclear_cache.py
More file actions
executable file
·109 lines (90 loc) · 3.4 KB
/
clear_cache.py
File metadata and controls
executable file
·109 lines (90 loc) · 3.4 KB
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
#!/usr/bin/env python3
"""
Cache management script for manual cache clearing and monitoring.
"""
import sys
import os
sys.path.insert(0, '.')
def clear_cache():
"""Clear the application cache."""
try:
from app import _cache, _cache_timeout
cache_count = len(_cache)
_cache.clear()
_cache_timeout.clear()
print(f"✅ Cleared {cache_count} cached entries")
return True
except Exception as e:
print(f"❌ Error clearing cache: {str(e)}")
return False
def show_cache_stats():
"""Show current cache statistics."""
try:
from app import _cache, _cache_timeout
from datetime import datetime
cache_count = len(_cache)
timeout_count = len(_cache_timeout)
print(f"📊 Cache Statistics:")
print(f" Total cached entries: {cache_count}")
print(f" Timeout entries: {timeout_count}")
if cache_count > 0:
# Show cache keys and their approximate sizes
total_size = 0
now = datetime.utcnow().timestamp()
active_count = 0
print(f"\n📋 Cached Routes:")
for key, value in _cache.items():
try:
size = len(str(value))
total_size += size
# Check if still valid
is_valid = key in _cache_timeout and now < _cache_timeout[key]
status = "🟢 Active" if is_valid else "🔴 Expired"
if is_valid:
active_count += 1
print(f" {status} {key[:50]:<50} ({size:,} bytes)")
except:
print(f" ❓ {key[:50]:<50} (unknown size)")
print(f"\n📈 Summary:")
print(f" Active entries: {active_count}/{cache_count}")
print(f" Estimated total size: {total_size:,} bytes ({total_size/1024/1024:.2f} MB)")
return True
except Exception as e:
print(f"❌ Error getting cache stats: {str(e)}")
return False
def main():
if len(sys.argv) < 2:
print("🗄️ Cache Management Tool")
print("Usage:")
print(" python clear_cache.py stats - Show cache statistics")
print(" python clear_cache.py clear - Clear all cache")
print(" python clear_cache.py restart - Clear cache and show stats")
return
command = sys.argv[1].lower()
if command == "stats":
print("🗄️ Cache Statistics")
print("=" * 50)
show_cache_stats()
elif command == "clear":
print("🗄️ Clearing Cache")
print("=" * 50)
if clear_cache():
print("✅ Cache cleared successfully!")
else:
print("❌ Failed to clear cache")
elif command == "restart":
print("🗄️ Cache Restart")
print("=" * 50)
print("Before clearing:")
show_cache_stats()
print("\nClearing cache...")
if clear_cache():
print("\nAfter clearing:")
show_cache_stats()
else:
print("❌ Failed to restart cache")
else:
print(f"❌ Unknown command: {command}")
print("Available commands: stats, clear, restart")
if __name__ == "__main__":
main()