Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,69 @@ CREATE TABLE IF NOT EXISTS query_cache_tags (
);
```

### Utilities for DatoCMS Cache Tags (Redis)

The following utilities provide Redis-based alternatives to the Postgres cache tags implementation above. They work with [DatoCMS cache tags](https://www.datocms.com/docs/content-delivery-api/cache-tags) and any Redis instance.

- `redis.storeQueryCacheTags`: Stores the cache tags of a query in Redis.
- `redis.queriesReferencingCacheTags`: Retrieves the queries that reference cache tags.
- `redis.deleteCacheTags`: Deletes cache tags from Redis.
- `redis.truncateCacheTags`: Wipes out all cache tags from Redis.

The Redis connection is automatically initialized on first use using the `REDIS_URL` environment variable.

#### Environment Variables

Add your Redis connection URL to your `.env.local` file:

```bash
# Required: Redis connection URL
# For Upstash Redis
REDIS_URL=rediss://default:your-token@your-endpoint.upstash.io:6379

# For Redis Cloud or other providers
REDIS_URL=redis://username:password@your-redis-host:6379

# For local development
REDIS_URL=redis://localhost:6379

# Optional: Key prefix for separating production/preview environments
# Useful when using the same Redis instance for multiple environments
REDIS_KEY_PREFIX=prod # For production
REDIS_KEY_PREFIX=preview # For preview/staging
# Leave empty for development (no prefix)
```

**Note**: Similar to how the Postgres version uses different table names, use `REDIS_KEY_PREFIX` to separate data between environments when using the same Redis instance.

#### Usage Example

```typescript
// Recommended: Use namespaces for clarity
import { generateQueryId, redis } from '@smartive/datocms-utils';

const queryId = generateQueryId(query, variables);

// Store cache tags for a query
await redis.storeQueryCacheTags(queryId, ['item:42', 'product', 'category:5']);

// Find all queries that reference specific tags
const affectedQueries = await redis.queriesReferencingCacheTags(['item:42']);

// Delete cache tags (keys will be recreated on next query)
await redis.deleteCacheTags(['item:42']);
```

#### Redis Data Structure

The Redis implementation uses Sets to track query-to-tag relationships:

- **Cache tag keys**: `{prefix}{tag}` → Set of query IDs

Where `{prefix}` is the optional `REDIS_KEY_PREFIX` environment variable (e.g., `prod:`, `preview:`).

When cache tags are invalidated, their keys are deleted entirely. Fresh mappings are created when queries run again.

### Other Utilities

- `classNames`: Cleans and joins an array of inputs with possible undefined or boolean values. Useful for tailwind classnames.
Expand Down
117 changes: 105 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@
"eslint": "9.39.2",
"eslint-import-resolver-typescript": "4.4.4",
"prettier": "3.8.1",
"typescript": "5.9.3"
"typescript": "5.9.3",
"ioredis": "5.4.1"
},
"dependencies": {
"@vercel/postgres": "^0.10.0",
"graphql": "^16.9.0"
},
"peerDependencies": {
"ioredis": "^5.4.0"
},
"peerDependenciesMeta": {
"ioredis": {
"optional": true
}
}
}
Loading