-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
137 lines (114 loc) · 4.76 KB
/
Copy pathcache.py
File metadata and controls
137 lines (114 loc) · 4.76 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
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
from flask import render_template, request, jsonify
from auth import login_required
def register_cache_routes(app):
"""Register cache management routes with the Flask app"""
@app.route('/cache-management')
@login_required
def cache_management():
"""Cache management and testing page"""
return render_template('cache_management.html')
@app.route('/api/cache/stats')
@login_required
def cache_stats():
"""Get cache statistics"""
try:
from events import day_events_cache, calendar_events_cache
day_stats = {
'maxsize': day_events_cache.maxsize,
'ttl': day_events_cache.ttl,
'size': len(day_events_cache),
'keys': list(day_events_cache.keys())[:20] # Show first 20 keys
}
calendar_stats = {
'maxsize': calendar_events_cache.maxsize,
'ttl': calendar_events_cache.ttl,
'size': len(calendar_events_cache),
'keys': list(calendar_events_cache.keys())[:20] # Show first 20 keys
}
return jsonify({
'day_events_cache': day_stats,
'calendar_events_cache': calendar_stats,
'total_cached_items': len(day_events_cache) + len(calendar_events_cache)
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/cache/test', methods=['POST'])
@login_required
def test_cache():
"""Test cache functionality"""
try:
from events import day_events_cache, calendar_events_cache
data = request.get_json()
test_key = data.get('key', 'test_key')
test_value = data.get('value', ['test_value'])
# Test setting and getting from day events cache
day_events_cache.set(test_key, test_value)
retrieved_value = day_events_cache.get(test_key)
return jsonify({
'success': True,
'test_key': test_key,
'test_value': test_value,
'retrieved_value': retrieved_value,
'cache_hit': retrieved_value is not None
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/cache/clear', methods=['POST'])
@login_required
def clear_cache():
"""Clear all caches"""
try:
from events import day_events_cache, calendar_events_cache
# Clear both caches directly
day_events_cache.clear()
calendar_events_cache.clear()
return jsonify({
'success': True,
'message': 'All caches cleared successfully'
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/cache/get/<key>')
@login_required
def get_cache_value(key):
"""Get a specific cache value"""
try:
from events import day_events_cache, calendar_events_cache
# Try both caches
day_value = day_events_cache.get(key)
calendar_value = calendar_events_cache.get(key)
return jsonify({
'key': key,
'day_events_cache': day_value,
'calendar_events_cache': calendar_value,
'found_in_day': day_value is not None,
'found_in_calendar': calendar_value is not None
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/cache/set', methods=['POST'])
@login_required
def set_cache_value():
"""Set a cache value"""
try:
from events import day_events_cache, calendar_events_cache
data = request.get_json()
key = data.get('key')
value = data.get('value')
cache_type = data.get('cache_type', 'day') # 'day' or 'calendar'
if not key or value is None:
return jsonify({'error': 'Key and value are required'}), 400
if cache_type == 'day':
day_events_cache.set(key, value)
elif cache_type == 'calendar':
calendar_events_cache.set(key, value)
else:
return jsonify({'error': 'Invalid cache type'}), 400
return jsonify({
'success': True,
'message': f'Value set in {cache_type} cache',
'key': key,
'value': value
})
except Exception as e:
return jsonify({'error': str(e)}), 500