🍡 mochi
← All demos

Cache Events

A custom subscriber prints MochiCache lifecycle events to the server console with a [demo:cache-events] prefix. Refresh the page and watch your terminal — the subscriber lives in log.ts.

Cached value 2026-05-05T15:45:45.759Z
Status expired

Refresh under 3s for a fresh hit. Refresh between 3–10s for stale + a background revalidate. After 10s the next refresh blocks on a fresh fetch (expired). Each event prints a line in the server console.

<script>
  import { getSlowTime } from './log';

  const { value: time, status } = await getSlowTime();
</script>

<div class="card">
  <div class="row">
    <div>
      <span class="label">Cached value</span>
      <code>{time}</code>
    </div>
    <div>
      <span class="label">Status</span>
      <span class="status status-{status}">{status}</span>
    </div>
  </div>
  <p class="hint">
    Refresh under <strong>3s</strong> for a <code>fresh</code> hit. Refresh between
    <strong>3–10s</strong>
    for <code>stale</code> + a background revalidate. After <strong>10s</strong> the next refresh blocks on a fresh fetch (<code>expired</code>). Each event prints a line in the
    server console.
  </p>
</div>
Styles
<style>
  .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1rem 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
  }

  .row {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
    align-items: baseline;
  }

  .label {
    display: block;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    margin-bottom: 0.2rem;
  }

  code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono', 'Courier New', monospace;
    font-size: 0.92rem;
    color: var(--text);
  }

  .hint {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin: 0;
  }

  .status {
    font-size: 0.78rem;
    padding: 0.05rem 0.4rem;
    border-radius: 4px;
    border: 1px solid var(--border);
  }

  .status-fresh {
    color: #4ade80;
    border-color: rgba(74, 222, 128, 0.35);
  }

  .status-stale {
    color: #facc15;
    border-color: rgba(250, 204, 21, 0.35);
  }

  .status-expired {
    color: #f87171;
    border-color: rgba(248, 113, 113, 0.4);
  }

  .status-miss {
    color: var(--text-muted);
  }
</style>