# Cache Middleware Usage

The caching middleware can be used to cache GET requests for better performance.

## Basic Usage

```javascript
import express from 'express';
import { cacheMiddleware } from '../middleware/cache.js';

const router = express.Router();

// Cache response for 30 seconds (default)
router.get('/endpoint', cacheMiddleware(30), async (req, res) => {
  // Your route handler
  const data = await fetchData();
  res.json({ success: true, data });
});

// Cache for 60 seconds
router.get('/slow-endpoint', cacheMiddleware(60), async (req, res) => {
  const data = await expensiveOperation();
  res.json({ success: true, data });
});

// No caching (bypass cache middleware)
router.get('/realtime', async (req, res) => {
  const data = await getRealtimeData();
  res.json({ success: true, data });
});
```

## When to Use Caching

✅ **Use caching for:**
- Expensive database queries
- Data that doesn't change frequently
- API endpoints that are called repeatedly
- Aggregated/summarized data

❌ **Don't use caching for:**
- Real-time data (prices, alerts)
- User-specific data
- Data that changes frequently
- POST/PUT/DELETE requests (automatically bypassed)

## Cache Management

```javascript
import { clearCache, getCacheStats } from '../middleware/cache.js';

// Clear all cached data
clearCache();

// Get cache statistics
const stats = getCacheStats();
console.log(stats);
// {
//   keys: 5,
//   hits: 150,
//   misses: 20,
//   ksize: 1024,
//   vsize: 51200
// }
```
