/** * Redis Caching Demo Page * Demonstrates server-side caching with Redis */ import { useState, useRef } from 'react'; import { Link } from 'react-router-dom'; // Simple in-memory cache simulation for demo purposes interface CacheEntry { value: string; expiresAt: number; } export default function RedisDemo() { const cacheRef = useRef>(new Map()); const [key, setKey] = useState('demo-key'); const [value, setValue] = useState('Hello, Redis!'); const [ttl, setTtl] = useState(3600); const [result, setResult] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleSet = async () => { setLoading(true); setError(null); try { // Simulate Redis SET with TTL const expiresAt = Date.now() + ttl * 1000; cacheRef.current.set(key, { value, expiresAt }); setResult(`SET "${key}" = "${value}" (TTL: ${ttl}s)\n\nResponse: ${JSON.stringify({ success: true, key, ttl }, null, 2)}`); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to set cache'); } setLoading(false); }; const handleGet = async () => { setLoading(true); setError(null); try { // Simulate Redis GET const entry = cacheRef.current.get(key); if (!entry) { setResult(`GET "${key}"\n\nResponse: ${JSON.stringify({ value: null, found: false }, null, 2)}`); } else if (Date.now() > entry.expiresAt) { cacheRef.current.delete(key); setResult(`GET "${key}"\n\nResponse: ${JSON.stringify({ value: null, found: false, expired: true }, null, 2)}`); } else { setResult(`GET "${key}"\n\nResponse: ${JSON.stringify({ value: entry.value, found: true }, null, 2)}`); } } catch (err) { setError(err instanceof Error ? err.message : 'Failed to get cache'); } setLoading(false); }; const handleDelete = async () => { setLoading(true); setError(null); try { // Simulate Redis DELETE const existed = cacheRef.current.has(key); cacheRef.current.delete(key); setResult(`DELETE "${key}"\n\nResponse: ${JSON.stringify({ success: true, deleted: existed }, null, 2)}`); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to delete cache'); } setLoading(false); }; const handleCheckTTL = async () => { setLoading(true); setError(null); try { // Simulate Redis TTL check const entry = cacheRef.current.get(key); if (!entry) { setResult(`TTL "${key}"\n\nResponse: ${JSON.stringify({ ttl: -2, message: 'Key does not exist' }, null, 2)}`); } else { const remainingTtl = Math.max(0, Math.floor((entry.expiresAt - Date.now()) / 1000)); setResult(`TTL "${key}"\n\nResponse: ${JSON.stringify({ ttl: remainingTtl, expiresIn: `${remainingTtl} seconds` }, null, 2)}`); } } catch (err) { setError(err instanceof Error ? err.message : 'Failed to check TTL'); } setLoading(false); }; return (
← Back to Home

Redis Caching Demo

Server-side caching for optimal performance and cost reduction.

{/* Cache Operations */}

Cache Operations

setKey(e.target.value)} className="input" placeholder="Cache key" />