|
| 1 | +# Vercel KV Cache Setup |
| 2 | + |
| 3 | +This project has been updated to use Vercel KV for caching graph data instead of generating it on every build. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +### 1. Install Dependencies |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @vercel/kv |
| 11 | +``` |
| 12 | + |
| 13 | +### 2. Set up Vercel KV |
| 14 | + |
| 15 | +1. Go to your Vercel project dashboard |
| 16 | +2. Navigate to the "Storage" tab |
| 17 | +3. Create a new KV database |
| 18 | +4. Copy the environment variables to your `.env.local` file: |
| 19 | + ``` |
| 20 | + KV_REST_API_URL=your_kv_url |
| 21 | + KV_REST_API_TOKEN=your_kv_token |
| 22 | + ``` |
| 23 | + |
| 24 | +### 3. Environment Variables (Optional) |
| 25 | + |
| 26 | +For additional security, you can set up a cron secret: |
| 27 | + |
| 28 | +``` |
| 29 | +CRON_SECRET=your_secret_key |
| 30 | +``` |
| 31 | + |
| 32 | +## How It Works |
| 33 | + |
| 34 | +### 1. Automatic Updates |
| 35 | + |
| 36 | +- The cron job runs every 6 hours (`0 */6 * * *`) |
| 37 | +- It fetches fresh data from the databases |
| 38 | +- Stores the processed graph data in Vercel KV |
| 39 | + |
| 40 | +### 2. Data Serving |
| 41 | + |
| 42 | +- The main page fetches data from `/api/graph-data` |
| 43 | +- Data is served from KV cache (fast) |
| 44 | +- Includes browser caching headers |
| 45 | +- Automatic refresh every 5 minutes client-side |
| 46 | + |
| 47 | +### 3. Manual Updates |
| 48 | + |
| 49 | +- Visit `/api/update-cache` to manually trigger a cache update |
| 50 | +- Useful for testing and initial setup |
| 51 | + |
| 52 | +## API Routes |
| 53 | + |
| 54 | +- `POST /api/cron` - Cron job endpoint (called automatically) |
| 55 | +- `GET /api/graph-data` - Serves cached data to the frontend |
| 56 | +- `GET /api/update-cache` - Manual cache update trigger |
| 57 | + |
| 58 | +## Benefits |
| 59 | + |
| 60 | +1. **Faster Loading**: No database queries during page load |
| 61 | +2. **Reduced Database Load**: Queries run only every 6 hours |
| 62 | +3. **Better UX**: Loading states and error handling |
| 63 | +4. **Scalability**: Can handle more concurrent users |
| 64 | +5. **Cost Efficiency**: Fewer database connections |
| 65 | + |
| 66 | +## Monitoring |
| 67 | + |
| 68 | +Check the Vercel function logs to monitor: |
| 69 | + |
| 70 | +- Cron job execution |
| 71 | +- Cache update success/failures |
| 72 | +- Data freshness |
| 73 | + |
| 74 | +## Troubleshooting |
| 75 | + |
| 76 | +If you see "No data available": |
| 77 | + |
| 78 | +1. Check that the databases are accessible |
| 79 | +2. Manually trigger cache update via `/api/update-cache` |
| 80 | +3. Check Vercel function logs for errors |
| 81 | +4. Verify KV environment variables are set correctly |
0 commit comments