-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathin_memory_based_caching.py
More file actions
75 lines (59 loc) · 2.26 KB
/
in_memory_based_caching.py
File metadata and controls
75 lines (59 loc) · 2.26 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
"""
This is in-memory caching with dynamic contents.
In-memory cache (dashboard example):
------------------------------------
Storage: Python dictionary in memory.
Data source: Dynamic database records.
Cache invalidation trigger: Database changes (e.g., max(updated_at) or record count).
Scope: Only valid within the current Python process.
Use case: Fast caching of dynamic content.
"""
from datetime import datetime, timedelta
import time
# Simulated database table. Assume below records are coming from database table dynamically
database = [
{"id": 1, "name": "Alice", "updated_at": datetime.now()},
{"id": 2, "name": "Bob", "updated_at": datetime.now()},
{"id": 3, "name": "Charlie", "updated_at": datetime.now()},
{"id": 4, "name": "David", "updated_at": datetime.now()},
{"id": 5, "name": "Eve", "updated_at": datetime.now()},
]
# Cache structure
cache = {
"html_content": None,
"last_updated": None # timestamp of latest record
}
def render_dashboard(records):
# Simple HTML rendering
html = "<h1>Dashboard</h1>\n<ul>\n"
for r in records:
html += f"<li>{r['id']}: {r['name']}</li>\n"
html += "</ul>\n"
return html
def get_dashboard():
# Get latest update timestamp from "DB"
table_last_updated = max(r["updated_at"] for r in database) if database else None
# Check cache
if cache["html_content"] and cache["last_updated"] == table_last_updated:
print("Serving dashboard from cache")
return cache["html_content"]
# Otherwise, regenerate HTML
print("Generating new dashboard HTML")
html_content = render_dashboard(database)
# Update cache
cache["html_content"] = html_content
cache["last_updated"] = table_last_updated
return html_content
if __name__ == "__main__":
# First request → generates HTML
print(get_dashboard())
time.sleep(2)
# Second request → should serve from cache
print(get_dashboard())
time.sleep(2)
# Simulate new records added to DB after 1 hour
new_time = datetime.now() + timedelta(hours=1)
database.append({"id": 6, "name": "Frank", "updated_at": new_time})
database.append({"id": 7, "name": "Grace", "updated_at": new_time})
# Next request → regenerates cache with new data
print(get_dashboard())