-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
194 lines (180 loc) · 12.6 KB
/
Copy pathindex.html
File metadata and controls
194 lines (180 loc) · 12.6 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
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Spring Cache Visualizer — @Cacheable, cache-aside, TTL & eviction, live</title>
<meta name="description" content="Watch Spring's @Cacheable in action: cache hits skip the database, misses load and store, TTL entries expire, and @CachePut / @CacheEvict update or clear the cache. See the cache-aside pattern and your DB-calls-saved count, live in your browser." />
<style>
:root{
--bg:#100a0a; --bg2:#170d0d; --card:#1d1010; --card2:#271515;
--line:#3a1e1e; --ink:#f5e9e9; --mut:#c79b9b; --dim:#8a5f5f;
--red:#ef4444; --red2:#dc2626; --redl:#fca5a5;
--green:#34d399; --amber:#fbbf24; --blue:#60a5fa; --cyan:#22d3ee;
}
*{box-sizing:border-box} html,body{margin:0;padding:0}
body{background:radial-gradient(1100px 560px at 82% -10%,#3a1414 0%,var(--bg) 55%);color:var(--ink);
font:15px/1.55 ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;min-height:100vh}
a{color:var(--redl);text-decoration:none} a:hover{text-decoration:underline}
.mono{font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace}
.wrap{max-width:1120px;margin:0 auto;padding:26px 20px 60px}
header.top{display:flex;align-items:flex-start;justify-content:space-between;gap:14px;flex-wrap:wrap}
h1{font-size:24px;margin:0;letter-spacing:-.4px;display:flex;align-items:center;gap:10px}
h1 .k{color:var(--red)}
.sub{color:var(--mut);margin:7px 0 0;max-width:760px}
.gh{border:1px solid var(--line);background:var(--card);color:var(--ink);border-radius:10px;padding:8px 14px;font-size:13px;white-space:nowrap}
.gh:hover{border-color:var(--red2);text-decoration:none}
.layout{display:grid;grid-template-columns:260px 1fr;gap:18px;margin-top:22px}
@media(max-width:880px){.layout{grid-template-columns:1fr}}
.panel{background:linear-gradient(180deg,var(--card),var(--bg2));border:1px solid var(--line);border-radius:14px;padding:16px}
.panel h2{font-size:12px;text-transform:uppercase;letter-spacing:.12em;color:var(--mut);margin:0 0 12px}
label.f{display:block;font-size:11px;text-transform:uppercase;letter-spacing:.06em;color:var(--dim);margin:12px 0 5px}
.idpick{display:flex;gap:6px}
.idpick button{flex:1;border:1px solid var(--line);background:var(--card2);color:var(--mut);border-radius:8px;padding:8px 0;cursor:pointer;font-family:ui-monospace,monospace}
.idpick button.on{border-color:var(--red);color:var(--ink);background:#2a1414}
.btn{border:1px solid var(--line);background:var(--card2);color:var(--ink);border-radius:9px;padding:10px 12px;cursor:pointer;font:inherit;font-size:13px;width:100%;margin-top:8px;text-align:left}
.btn:hover{border-color:var(--red2)}
.btn.primary{border:0;background:linear-gradient(180deg,var(--red),var(--red2));color:#180404;font-weight:800}
.btn code{color:inherit;background:rgba(0,0,0,.2);border:0}
.tog{display:flex;align-items:center;gap:8px;font-size:13px;color:var(--mut);margin-top:12px;cursor:pointer}
.tog input{accent-color:var(--red)}
.flow{display:flex;align-items:center;gap:8px;flex-wrap:wrap;margin-bottom:14px}
.fn{background:var(--card2);border:1px solid var(--line);border-radius:10px;padding:10px 12px;font-size:12.5px;transition:.15s;flex:none}
.fn .t{font-weight:700} .fn .s{color:var(--dim);font-size:10.5px;font-family:ui-monospace,monospace}
.fn.on{border-color:var(--red);box-shadow:0 0 20px -8px var(--red)}
.fn.hit{border-color:var(--green);box-shadow:0 0 20px -8px var(--green)}
.fn.miss{border-color:var(--amber);box-shadow:0 0 20px -8px var(--amber)}
.arrow{color:var(--dim)}
.stats{display:grid;grid-template-columns:repeat(4,1fr);gap:10px;margin-bottom:14px}
.stat{background:var(--card2);border:1px solid var(--line);border-radius:10px;padding:10px;text-align:center}
.stat .v{font-size:22px;font-weight:800} .stat .l{font-size:10.5px;color:var(--dim);margin-top:2px}
.stat .v.g{color:var(--green)} .stat .v.a{color:var(--amber)} .stat .v.r{color:var(--redl)}
.cache{display:flex;flex-direction:column;gap:8px;min-height:40px}
.entry{display:flex;align-items:center;gap:10px;background:#140b0b;border:1px solid var(--line);border-radius:9px;padding:9px 11px;font-family:ui-monospace,monospace;font-size:12.5px;animation:pop .25s}
@keyframes pop{from{transform:scale(.96);opacity:0}to{transform:none;opacity:1}}
.entry .key{color:var(--cyan)} .entry .val{color:var(--ink)} .entry.exp{opacity:.45}
.ttlbar{flex:1;height:6px;background:#2a1414;border-radius:4px;overflow:hidden;min-width:60px}
.ttlbar .fill{height:100%;background:linear-gradient(90deg,var(--green),var(--amber));transition:width .3s}
.entry .tag{font-size:10px;border:1px solid var(--line);border-radius:5px;padding:1px 6px;color:var(--dim)}
.empty{color:var(--dim);font-size:13px;padding:8px 2px}
.log{font-family:ui-monospace,Menlo,Consolas,monospace;font-size:11.5px;background:#140b0b;border:1px solid var(--line);border-radius:10px;padding:10px;height:150px;overflow:auto;margin-top:14px}
.log div{padding:2px 0;border-bottom:1px solid #1c1010}
.log .g{color:var(--green)} .log .a{color:var(--amber)} .log .m{color:var(--mut)} .log .r{color:var(--redl)} .log .b{color:var(--blue)}
.tips{margin-top:16px;background:#150c0c;border:1px solid var(--line);border-radius:12px;padding:13px 15px;color:var(--mut);font-size:13px;line-height:1.6}
.tips b{color:var(--ink)}
footer{margin-top:34px;color:var(--dim);font-size:12.5px;text-align:center;border-top:1px solid var(--line);padding-top:18px}
</style>
</head>
<body>
<div class="wrap">
<header class="top">
<div>
<h1><span class="k">⚡</span> Spring Cache Visualizer</h1>
<p class="sub">Watch Spring's <code>@Cacheable</code> at work: a <b>hit</b> skips the database, a <b>miss</b> loads and stores, entries <b>expire</b> on TTL, and <code>@CachePut</code> / <code>@CacheEvict</code> update or clear the cache. The <b>cache-aside</b> pattern, made visible.</p>
</div>
<a class="gh" id="ghlink" href="https://github.com/dev48v/cache-visualizer" target="_blank" rel="noopener">★ Star on GitHub</a>
</header>
<div class="layout">
<aside class="panel">
<h2>UserService</h2>
<label class="f">id</label>
<div class="idpick" id="idpick"><button data-id="1" class="on">1</button><button data-id="2">2</button><button data-id="3">3</button></div>
<button class="btn primary" id="get">▶ getUser(id) <code>@Cacheable</code></button>
<button class="btn" id="put">updateUser(id) <code>@CachePut</code></button>
<button class="btn" id="evict">deleteUser(id) <code>@CacheEvict</code></button>
<button class="btn" id="evictall">clearCache() <code>@CacheEvict(allEntries)</code></button>
<label class="tog"><input type="checkbox" id="ttl" checked> TTL expiry (7s)</label>
<button class="btn" id="reset" style="margin-top:14px">reset</button>
</aside>
<section>
<div class="stats">
<div class="stat"><div class="v" id="sCalls">0</div><div class="l">calls</div></div>
<div class="stat"><div class="v g" id="sHits">0</div><div class="l">cache hits</div></div>
<div class="stat"><div class="v a" id="sDb">0</div><div class="l">DB queries</div></div>
<div class="stat"><div class="v r" id="sSaved">0</div><div class="l">DB calls saved</div></div>
</div>
<div class="flow" id="flow">
<div class="fn" id="f-req"><div class="t">Caller</div><div class="s">getUser(1)</div></div>
<span class="arrow">→</span>
<div class="fn" id="f-proxy"><div class="t">@Cacheable proxy</div><div class="s">check cache first</div></div>
<span class="arrow">→</span>
<div class="fn" id="f-cache"><div class="t">Cache "users"</div><div class="s">key → value</div></div>
<span class="arrow">→</span>
<div class="fn" id="f-db"><div class="t">Database</div><div class="s">SELECT … (~800ms)</div></div>
</div>
<div class="panel" style="background:none;border-color:var(--line)">
<h2>Cache contents</h2>
<div class="cache" id="cache"></div>
</div>
<div class="log" id="log"></div>
<div class="tips">
<b>Try this:</b> click <b>getUser(1)</b> → cache <b>MISS</b>, the DB runs (~800ms) and the result is cached. Click it again → cache <b>HIT</b>, instant, <b>the DB is skipped</b> (watch "DB calls saved" climb). <code>@CachePut</code> always hits the DB and refreshes the entry (new rev); <code>@CacheEvict</code> removes it so the next read misses again. With <b>TTL</b> on, wait ~7s and the entry expires — the next call reloads it.
</div>
</section>
</div>
<footer>A teaching model of Spring Cache (@Cacheable/@CachePut/@CacheEvict) + the cache-aside pattern (as with Redis / Caffeine). MIT.</footer>
</div>
<script>
"use strict";
const $=s=>document.querySelector(s);
const TTL_MS=7000;
let cache=new Map(); // key -> {val, at}
let dbRev={1:0,2:0,3:0};
let calls=0, hits=0, db=0, busy=false, id=1;
function keyOf(i){ return "users::"+i; }
function now(){ return Date.now(); }
function log(cls,msg){ const d=document.createElement("div"); d.innerHTML=`<span class="${cls}">${msg}</span>`; const L=$("#log"); L.appendChild(d); L.scrollTop=L.scrollHeight; }
const sleep=ms=>new Promise(r=>setTimeout(r,ms));
function fclass(el,c){ $("#"+el).className="fn"+(c?" "+c:""); }
function clearFlow(){ ["f-req","f-proxy","f-cache","f-db"].forEach(e=>$("#"+e).className="fn"); }
function expired(e){ return $("#ttl").checked && (now()-e.at)>TTL_MS; }
function renderCache(){
const c=$("#cache");
if(cache.size===0){ c.innerHTML='<div class="empty">(empty)</div>'; return; }
c.innerHTML="";
for(const [k,e] of cache){
const age=now()-e.at; const left=Math.max(0,TTL_MS-age); const pct=$("#ttl").checked?Math.round(left/TTL_MS*100):100; const exp=expired(e);
c.insertAdjacentHTML("beforeend",`<div class="entry ${exp?'exp':''}"><span class="key">${k}</span> <span class="val">${e.val}</span>
${$("#ttl").checked?`<div class="ttlbar"><div class="fill" style="width:${pct}%"></div></div><span class="tag">${exp?'expired':Math.ceil(left/1000)+'s'}</span>`:'<span class="tag">no TTL</span>'}</div>`);
}
}
function stats(){ $("#sCalls").textContent=calls; $("#sHits").textContent=hits; $("#sDb").textContent=db; $("#sSaved").textContent=hits; }
async function dbLoad(i){ db++; fclass("f-cache","miss"); await sleep(60); fclass("f-db","on"); log("a",`DB SELECT * FROM users WHERE id=${i} (~800ms)`); await sleep(900); dbRev[i]++; return `{id:${i}, name:"User${i}", rev:${dbRev[i]}}`; }
async function getUser(){
if(busy) return; busy=true; const i=id; const k=keyOf(i);
clearFlow(); calls++; stats();
$("#f-req").querySelector(".s").textContent=`getUser(${i})`;
fclass("f-req","on"); await sleep(180); fclass("f-proxy","on"); await sleep(200);
const e=cache.get(k);
if(e && !expired(e)){
hits++; fclass("f-cache","hit"); log("g",`getUser(${i}) → cache HIT ${e.val} — DB skipped ✓`);
await sleep(350);
} else {
if(e && expired(e)){ log("m",`entry ${k} expired (TTL) → treated as miss`); cache.delete(k); }
log("a",`getUser(${i}) → cache MISS`);
const val=await dbLoad(i);
cache.set(k,{val,at:now()}); fclass("f-cache","hit"); log("g",`stored ${k} → ${val}`);
await sleep(250);
}
clearFlow(); stats(); renderCache(); busy=false;
}
async function putUser(){
if(busy) return; busy=true; const i=id; const k=keyOf(i);
clearFlow(); fclass("f-proxy","on"); await sleep(150);
log("b",`updateUser(${i}) @CachePut → always runs the method + refreshes cache`);
const val=await dbLoad(i);
cache.set(k,{val,at:now()}); fclass("f-cache","hit"); log("g",`cache updated ${k} → ${val}`);
await sleep(250); clearFlow(); stats(); renderCache(); busy=false;
}
function evictUser(){ const k=keyOf(id); if(cache.delete(k)) log("r",`@CacheEvict → removed ${k}`); else log("m",`${k} not in cache`); renderCache(); }
function evictAll(){ cache.clear(); log("r",`@CacheEvict(allEntries=true) → cache cleared`); renderCache(); }
function reset(){ cache.clear(); dbRev={1:0,2:0,3:0}; calls=hits=db=0; $("#log").innerHTML=""; clearFlow(); stats(); renderCache(); }
$("#idpick").onclick=e=>{ const b=e.target.closest("button"); if(!b)return; id=+b.dataset.id; document.querySelectorAll("#idpick button").forEach(x=>x.classList.toggle("on",x===b)); $("#f-req").querySelector(".s").textContent=`getUser(${id})`; };
$("#get").onclick=getUser; $("#put").onclick=putUser; $("#evict").onclick=evictUser; $("#evictall").onclick=evictAll; $("#reset").onclick=reset;
$("#ttl").onchange=renderCache;
setInterval(renderCache,500);
document.getElementById("ghlink").href="https://github.com/dev48v/cache-visualizer";
stats(); renderCache();
</script>
</body>
</html>