strapi-cache 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -32,3 +32,21 @@ or
32
32
  ```bash
33
33
  yarn add strapi-cache
34
34
  ```
35
+
36
+ ## ⚙️ Configuration
37
+
38
+ In your Strapi project, navigate to `config/plugins.js` and add the following configuration:
39
+
40
+ ```javascript
41
+ // config/plugins.{js,ts}
42
+ 'strapi-cache': {
43
+ enabled: true,
44
+ config: {
45
+ debug: false, // Enable debug logs
46
+ max: 1000, // Maximum number of items in the cache
47
+ ttl: 1000 * 60 * 60, // Time to live for cache items (1 hour)
48
+ size: 1024 * 1024 * 1024, // Maximum size of the cache (1 GB)
49
+ allowStale: false, // Allow stale cache items
50
+ },
51
+ },
52
+ ```
@@ -7,4 +7,3 @@ const en = {
7
7
  export {
8
8
  en as default
9
9
  };
10
- //# sourceMappingURL=en-BgoZyxl0.mjs.map
@@ -7,4 +7,3 @@ const en = {
7
7
  "strapi-cache.view.settings.subtitle": "Configure what to cache"
8
8
  };
9
9
  exports.default = en;
10
- //# sourceMappingURL=en-CIN0kRXJ.js.map
@@ -1,4 +1,49 @@
1
1
  "use strict";
2
- const index = require("../_chunks/index-BNNfEmGs.js");
3
- module.exports = index.index;
4
- //# sourceMappingURL=index.js.map
2
+ const react = require("react");
3
+ const __variableDynamicImportRuntimeHelper = (glob, path, segs) => {
4
+ const v = glob[path];
5
+ if (v) {
6
+ return typeof v === "function" ? v() : Promise.resolve(v);
7
+ }
8
+ return new Promise((_, reject) => {
9
+ (typeof queueMicrotask === "function" ? queueMicrotask : setTimeout)(
10
+ reject.bind(
11
+ null,
12
+ new Error(
13
+ "Unknown variable dynamic import: " + path + (path.split("/").length !== segs ? ". Note that variables only represent file names one level deep." : "")
14
+ )
15
+ )
16
+ );
17
+ });
18
+ };
19
+ const PLUGIN_ID = "strapi-cache";
20
+ const Initializer = ({ setPlugin }) => {
21
+ const ref = react.useRef(setPlugin);
22
+ react.useEffect(() => {
23
+ ref.current(PLUGIN_ID);
24
+ }, []);
25
+ return null;
26
+ };
27
+ const index = {
28
+ register(app) {
29
+ app.registerPlugin({
30
+ id: PLUGIN_ID,
31
+ initializer: Initializer,
32
+ isReady: false,
33
+ name: PLUGIN_ID
34
+ });
35
+ },
36
+ async registerTrads({ locales }) {
37
+ return Promise.all(
38
+ locales.map(async (locale) => {
39
+ try {
40
+ const { default: data } = await __variableDynamicImportRuntimeHelper(/* @__PURE__ */ Object.assign({ "./translations/en.json": () => Promise.resolve().then(() => require("../_chunks/en-CIN0kRXJ.js")) }), `./translations/${locale}.json`, 3);
41
+ return { data, locale };
42
+ } catch {
43
+ return { data: {}, locale };
44
+ }
45
+ })
46
+ );
47
+ }
48
+ };
49
+ module.exports = index;
@@ -1,5 +1,50 @@
1
- import { i } from "../_chunks/index-D84x8OCy.mjs";
1
+ import { useRef, useEffect } from "react";
2
+ const __variableDynamicImportRuntimeHelper = (glob, path, segs) => {
3
+ const v = glob[path];
4
+ if (v) {
5
+ return typeof v === "function" ? v() : Promise.resolve(v);
6
+ }
7
+ return new Promise((_, reject) => {
8
+ (typeof queueMicrotask === "function" ? queueMicrotask : setTimeout)(
9
+ reject.bind(
10
+ null,
11
+ new Error(
12
+ "Unknown variable dynamic import: " + path + (path.split("/").length !== segs ? ". Note that variables only represent file names one level deep." : "")
13
+ )
14
+ )
15
+ );
16
+ });
17
+ };
18
+ const PLUGIN_ID = "strapi-cache";
19
+ const Initializer = ({ setPlugin }) => {
20
+ const ref = useRef(setPlugin);
21
+ useEffect(() => {
22
+ ref.current(PLUGIN_ID);
23
+ }, []);
24
+ return null;
25
+ };
26
+ const index = {
27
+ register(app) {
28
+ app.registerPlugin({
29
+ id: PLUGIN_ID,
30
+ initializer: Initializer,
31
+ isReady: false,
32
+ name: PLUGIN_ID
33
+ });
34
+ },
35
+ async registerTrads({ locales }) {
36
+ return Promise.all(
37
+ locales.map(async (locale) => {
38
+ try {
39
+ const { default: data } = await __variableDynamicImportRuntimeHelper(/* @__PURE__ */ Object.assign({ "./translations/en.json": () => import("../_chunks/en-BgoZyxl0.mjs") }), `./translations/${locale}.json`, 3);
40
+ return { data, locale };
41
+ } catch {
42
+ return { data: {}, locale };
43
+ }
44
+ })
45
+ );
46
+ }
47
+ };
2
48
  export {
3
- i as default
49
+ index as default
4
50
  };
5
- //# sourceMappingURL=index.mjs.map
@@ -7,12 +7,24 @@ const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
7
7
  const rawBody__default = /* @__PURE__ */ _interopDefault(rawBody);
8
8
  const loggy = {
9
9
  info: (msg) => {
10
+ const shouldDebug = strapi.plugin("strapi-cache").config("debug") ?? false;
11
+ if (!shouldDebug) {
12
+ return;
13
+ }
10
14
  strapi.log.info(`[STRAPI CACHE] ${msg}`);
11
15
  },
12
16
  error: (msg) => {
17
+ const shouldDebug = strapi.plugin("strapi-cache").config("debug") ?? false;
18
+ if (!shouldDebug) {
19
+ return;
20
+ }
13
21
  strapi.log.error(`[STRAPI CACHE] ${msg}`);
14
22
  },
15
23
  warn: (msg) => {
24
+ const shouldDebug = strapi.plugin("strapi-cache").config("debug") ?? false;
25
+ if (!shouldDebug) {
26
+ return;
27
+ }
16
28
  strapi.log.warn(`[STRAPI CACHE] ${msg}`);
17
29
  }
18
30
  };
@@ -157,8 +169,29 @@ const register = ({ strapi: strapi2 }) => {
157
169
  strapi2.server.use(middlewares.graphql);
158
170
  };
159
171
  const config = {
160
- default: {},
161
- validator() {
172
+ default: ({ env }) => ({
173
+ debug: false,
174
+ max: 1e3,
175
+ ttl: 1e3 * 60 * 60,
176
+ size: 1024 * 1014 * 10,
177
+ allowStale: false
178
+ }),
179
+ validator: (config2) => {
180
+ if (typeof config2.debug !== "boolean") {
181
+ throw new Error(`Invalid config: debug must be a boolean`);
182
+ }
183
+ if (typeof config2.max !== "number") {
184
+ throw new Error(`Invalid config: max must be a number`);
185
+ }
186
+ if (typeof config2.ttl !== "number") {
187
+ throw new Error(`Invalid config: ttl must be a number`);
188
+ }
189
+ if (typeof config2.size !== "number") {
190
+ throw new Error(`Invalid config: size must be a number`);
191
+ }
192
+ if (typeof config2.allowStale !== "boolean") {
193
+ throw new Error(`Invalid config: allowStale must be a boolean`);
194
+ }
162
195
  }
163
196
  };
164
197
  const contentTypes = {};
@@ -221,10 +254,15 @@ const service = ({ strapi: strapi2 }) => {
221
254
  return;
222
255
  }
223
256
  initialized = true;
257
+ const max = strapi2.plugin("strapi-cache").config("max");
258
+ const ttl = strapi2.plugin("strapi-cache").config("ttl");
259
+ const size = strapi2.plugin("strapi-cache").config("size");
260
+ const allowStale = strapi2.plugin("strapi-cache").config("allowStale");
224
261
  provider = new lruCache.LRUCache({
225
- max: 1024 * 1014 * 10,
226
- ttl: 1e3 * 60 * 60,
227
- size: 1e3
262
+ max: Number(max),
263
+ ttl: Number(ttl),
264
+ size: Number(size),
265
+ allowStale: allowStale ? true : false
228
266
  });
229
267
  loggy.info("Provider initialized");
230
268
  },
@@ -371,4 +409,3 @@ const index = {
371
409
  middlewares
372
410
  };
373
411
  module.exports = index;
374
- //# sourceMappingURL=index.js.map
@@ -4,12 +4,24 @@ import { Readable } from "stream";
4
4
  import { LRUCache } from "lru-cache";
5
5
  const loggy = {
6
6
  info: (msg) => {
7
+ const shouldDebug = strapi.plugin("strapi-cache").config("debug") ?? false;
8
+ if (!shouldDebug) {
9
+ return;
10
+ }
7
11
  strapi.log.info(`[STRAPI CACHE] ${msg}`);
8
12
  },
9
13
  error: (msg) => {
14
+ const shouldDebug = strapi.plugin("strapi-cache").config("debug") ?? false;
15
+ if (!shouldDebug) {
16
+ return;
17
+ }
10
18
  strapi.log.error(`[STRAPI CACHE] ${msg}`);
11
19
  },
12
20
  warn: (msg) => {
21
+ const shouldDebug = strapi.plugin("strapi-cache").config("debug") ?? false;
22
+ if (!shouldDebug) {
23
+ return;
24
+ }
13
25
  strapi.log.warn(`[STRAPI CACHE] ${msg}`);
14
26
  }
15
27
  };
@@ -154,8 +166,29 @@ const register = ({ strapi: strapi2 }) => {
154
166
  strapi2.server.use(middlewares.graphql);
155
167
  };
156
168
  const config = {
157
- default: {},
158
- validator() {
169
+ default: ({ env }) => ({
170
+ debug: false,
171
+ max: 1e3,
172
+ ttl: 1e3 * 60 * 60,
173
+ size: 1024 * 1014 * 10,
174
+ allowStale: false
175
+ }),
176
+ validator: (config2) => {
177
+ if (typeof config2.debug !== "boolean") {
178
+ throw new Error(`Invalid config: debug must be a boolean`);
179
+ }
180
+ if (typeof config2.max !== "number") {
181
+ throw new Error(`Invalid config: max must be a number`);
182
+ }
183
+ if (typeof config2.ttl !== "number") {
184
+ throw new Error(`Invalid config: ttl must be a number`);
185
+ }
186
+ if (typeof config2.size !== "number") {
187
+ throw new Error(`Invalid config: size must be a number`);
188
+ }
189
+ if (typeof config2.allowStale !== "boolean") {
190
+ throw new Error(`Invalid config: allowStale must be a boolean`);
191
+ }
159
192
  }
160
193
  };
161
194
  const contentTypes = {};
@@ -218,10 +251,15 @@ const service = ({ strapi: strapi2 }) => {
218
251
  return;
219
252
  }
220
253
  initialized = true;
254
+ const max = strapi2.plugin("strapi-cache").config("max");
255
+ const ttl = strapi2.plugin("strapi-cache").config("ttl");
256
+ const size = strapi2.plugin("strapi-cache").config("size");
257
+ const allowStale = strapi2.plugin("strapi-cache").config("allowStale");
221
258
  provider = new LRUCache({
222
- max: 1024 * 1014 * 10,
223
- ttl: 1e3 * 60 * 60,
224
- size: 1e3
259
+ max: Number(max),
260
+ ttl: Number(ttl),
261
+ size: Number(size),
262
+ allowStale: allowStale ? true : false
225
263
  });
226
264
  loggy.info("Provider initialized");
227
265
  },
@@ -370,4 +408,3 @@ const index = {
370
408
  export {
371
409
  index as default
372
410
  };
373
- //# sourceMappingURL=index.mjs.map
@@ -1,5 +1,13 @@
1
1
  declare const _default: {
2
- default: {};
3
- validator(): void;
2
+ default: ({ env }: {
3
+ env: any;
4
+ }) => {
5
+ debug: boolean;
6
+ max: number;
7
+ ttl: number;
8
+ size: number;
9
+ allowStale: boolean;
10
+ };
11
+ validator: (config: any) => void;
4
12
  };
5
13
  export default _default;
@@ -12,8 +12,16 @@ declare const _default: {
12
12
  strapi: import("@strapi/types/dist/core").Strapi;
13
13
  }) => void;
14
14
  config: {
15
- default: {};
16
- validator(): void;
15
+ default: ({ env }: {
16
+ env: any;
17
+ }) => {
18
+ debug: boolean;
19
+ max: number;
20
+ ttl: number;
21
+ size: number;
22
+ allowStale: boolean;
23
+ };
24
+ validator: (config: any) => void;
17
25
  };
18
26
  controllers: {
19
27
  controller: ({ strapi }: {
package/package.json CHANGED
@@ -1,6 +1,11 @@
1
1
  {
2
- "version": "1.0.0",
3
- "keywords": ["strapi cache", "strapi rest cache", "strapi 5 cache","strapi v5 cache plugin"],
2
+ "version": "1.1.1",
3
+ "keywords": [
4
+ "strapi cache",
5
+ "strapi rest cache",
6
+ "strapi 5 cache",
7
+ "strapi v5 cache plugin"
8
+ ],
4
9
  "type": "commonjs",
5
10
  "exports": {
6
11
  "./package.json": "./package.json",
@@ -65,10 +70,10 @@
65
70
  "kind": "plugin",
66
71
  "name": "strapi-cache",
67
72
  "displayName": "Strapi Cache",
68
- "description": "A LRU-Cache plugin for strapi"
73
+ "description": "A LRU-Cache plugin for strapi v5"
69
74
  },
70
75
  "name": "strapi-cache",
71
- "description": "A LRU-Cache plugin for strapi",
76
+ "description": "A LRU-Cache plugin for strapi v5",
72
77
  "license": "MIT",
73
78
  "author": "TupiC <christoph.tupi@gmail.com>"
74
79
  }