Skip to content

Cache Management

The application uses Redis for caching various types of data to improve performance. This document describes how to manage the cache system.

The cache uses the following key patterns:

  • erp:items:* - Product/item related data
  • erp:shipping:* - Shipping address data
  • erp:customer:* - Customer data
  • erp:order:* - Order data

Each entity type has specific sub-patterns:

  • Search results: erp:{entity}:search:*
  • Individual records: erp:{entity}:{id}

Different types of data have different Time-To-Live (TTL) values:

  • Search results: 5 minutes
  • Individual records: 10 minutes
  • Inventory data: 1 minute

The application provides an API endpoint for flushing the cache. This endpoint requires admin privileges.

To use the cache flush endpoint, you need to:

  1. Be authenticated
  2. Have the ‘admin’ role in your user metadata
POST /api/cache/flush

The endpoint accepts the following options in the request body:

  1. Flush by pattern:
{
"pattern": "erp:items:search:*"
}
  1. Flush by entity type:
{
"entity": "ITEM"
}

Valid entity types: ITEM, SHIPPING, CUSTOMER, ORDER

  1. Flush entire cache:
{}

Using curl:

Terminal window
# Flush specific pattern
curl -X POST http://localhost:3000/api/cache/flush \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${YOUR_AUTH_TOKEN}" \
-d '{"pattern": "erp:items:search:*"}'
# Flush all keys for an entity
curl -X POST http://localhost:3000/api/cache/flush \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${YOUR_AUTH_TOKEN}" \
-d '{"entity": "ITEM"}'
# Flush all cache
curl -X POST http://localhost:3000/api/cache/flush \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${YOUR_AUTH_TOKEN}"

Success response:

{
"success": true,
"data": {
"flushed": 42, // number of keys flushed, or "all" for full flush
"pattern": "erp:items:*" // only present when pattern specified
}
}

Error response:

{
"success": false,
"error": "Error message"
}
  1. Selective Flushing: Prefer flushing specific patterns or entities over flushing the entire cache
  2. Timing: Avoid flushing frequently accessed caches during peak usage times
  3. Monitoring: Monitor the cache hit rates and adjust TTLs accordingly
  4. Validation: Always validate that the flush was successful by checking the response

Common issues and solutions:

  1. Authentication Errors:

    • Ensure you’re including a valid auth token
    • Check that your user has the ‘admin’ role
  2. Pattern Not Found:

    • Check that the pattern matches the cache key structure
    • Note that no error is returned if no keys match the pattern
  3. Rate Limiting:

    • The endpoint is rate-limited to prevent abuse
    • Wait a few minutes if you receive a 429 response