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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Full configuration example:
cacheHeadersDenyList: ['access-control-allow-origin', 'content-encoding'], // Headers to exclude from the cache (must be lowercase, if empty array, no headers are excluded, cacheHeaders must be true)
cacheHeadersAllowList: ['content-type', 'content-security-policy'], // Headers to include in the cache (must be lowercase, if empty array, all headers are cached, cacheHeaders must be true)
cacheAuthorizedRequests: false, // Cache requests with authorization headers (set to true if you want to cache authorized requests)
cacheGetTimeoutInMs: 1000, // Timeout for getting cached data in milliseconds (default is 1 second)
cacheGetTimeoutInMs: 1000, // Timeout for getting cached data in milliseconds (default is 1 second, redis only)
autoPurgeCache: true, // Automatically purge cache on content CRUD operations
autoPurgeGraphQL: true, // Automatically purge GraphQL cache on content CRUD operations
autoPurgeCacheOnStart: true, // Automatically purge cache on Strapi startup
Expand Down Expand Up @@ -118,7 +118,7 @@ Possible configuration keys are listed below; omitted keys keep the plugin defau
| `cacheHeadersDenyList` | Header names (lowercase) to strip when `cacheHeaders` is `true` | Array of strings (default: `[]`) |
| `cacheHeadersAllowList` | If non-empty, only these header names (lowercase) are stored; if empty, all headers are stored (subject to deny list) | Array of strings (default: `[]`) |
| `cacheAuthorizedRequests` | Whether to cache requests that include an `Authorization` header | `true` or `false` (default: `false`) |
| `cacheGetTimeoutInMs` | Max time to wait for a cache read before treating it as a miss | Milliseconds (default: `1000`) |
| `cacheGetTimeoutInMs` | Max time to wait for a cache read before treating it as a miss (redis only) | Milliseconds (default: `1000`) |
| `autoPurgeCache` | Invalidate relevant REST cache entries after content create/update/delete | `true` or `false` (default: `true`) |
| `autoPurgeGraphQL` | Invalidate GraphQL cache after content create/update/delete | `true` or `false` (default: `false` if omitted; set `true` to enable) |
| `autoPurgeCacheOnStart` | Clear the cache when Strapi starts | `true` or `false` (default: `true`) |
Expand Down
18 changes: 4 additions & 14 deletions server/src/services/memory/provider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { Core } from '@strapi/strapi';
import { LRUCache } from 'lru-cache';
import { withTimeout } from '../../utils/withTimeout';
import { CacheProvider, CacheService } from '../../types/cache.types';
import { loggy } from '../../utils/log';

export class InMemoryCacheProvider implements CacheProvider {
private initialized = false;
private provider!: LRUCache<string, any>;
private cacheGetTimeoutInMs: number;

constructor(private strapi: Core.Strapi) {}

Expand All @@ -31,10 +29,6 @@ export class InMemoryCacheProvider implements CacheProvider {
allowStale,
});

this.cacheGetTimeoutInMs = Number(
this.strapi.plugin('strapi-cache').config('cacheGetTimeoutInMs')
);

loggy.info('Provider initialized');
}

Expand All @@ -50,16 +44,12 @@ export class InMemoryCacheProvider implements CacheProvider {
async get(key: string): Promise<any | null> {
if (!this.ready) return null;

return withTimeout(
() =>
new Promise((resolve) => {
resolve(this.provider.get(key));
}),
this.cacheGetTimeoutInMs
).catch((error) => {
try {
return this.provider.get(key);
} catch (error) {
loggy.error(`Error during get: ${error?.message || error}`);
return null;
});
}
}

async set(key: string, val: any): Promise<any | null> {
Expand Down
Loading