@akemona-org/strapi-hook-redis 3.7.0

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015-present Strapi Solutions SAS
2
+
3
+ Portions of the Strapi software are licensed as follows:
4
+
5
+ * All software that resides under an "ee/" directory (the “EE Software”), if that directory exists, is licensed under the license defined in "ee/LICENSE".
6
+
7
+ * All software outside of the above-mentioned directories or restrictions above is available under the "MIT Expat" license as set forth below.
8
+
9
+ MIT Expat License
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # strapi-redis
2
+
3
+ [![npm version](https://img.shields.io/npm/v/strapi-redis.svg)](https://www.npmjs.org/package/strapi-redis)
4
+ [![npm downloads](https://img.shields.io/npm/dm/strapi-redis.svg)](https://www.npmjs.org/package/strapi-redis)
5
+ [![npm dependencies](https://david-dm.org/strapi/strapi-redis.svg)](https://david-dm.org/strapi/strapi-redis)
6
+ [![Build status](https://travis-ci.org/strapi/strapi-redis.svg?branch=master)](https://travis-ci.org/strapi/strapi-redis)
7
+ [![Slack status](https://slack.strapi.io/badge.svg)](https://slack.strapi.io)
8
+
9
+ This built-in hook allows you to use [Redis](https://redis.io/) as a databases connection. Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.
10
+
11
+ ---
12
+
13
+ ## Deprecation Warning :warning:
14
+
15
+ Hello, we've some news to share!
16
+
17
+ We released Strapi V4 in Q4 2021 and Strapi V3 will reach end-of-support around the end of Q3 2022.
18
+
19
+ Since this package won't be ported in V4, we took the decision to deprecate it.
20
+
21
+ If you’ve contributed to the development of this package, thank you again for that! We hope to see you on the V4 soon.
22
+
23
+ The Akemona team
24
+
25
+ ---
26
+
27
+ ## Motivation
28
+
29
+ We developed this hook to use Redis as cache database for our Strapi apps.
30
+
31
+ ## Resources
32
+
33
+ - [License](LICENSE)
34
+
35
+ ## Links
36
+
37
+ - [Strapi website](https://strapi.akemona.com/)
38
+ - [Strapi community on Slack](https://slack.strapi.io)
39
+ - [Strapi news on Twitter](https://twitter.com/strapijs)
package/lib/index.js ADDED
@@ -0,0 +1,165 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Module dependencies
5
+ */
6
+
7
+ // Core
8
+ const util = require('util');
9
+ /* eslint-disable prefer-template */
10
+
11
+ // Public node modules.
12
+ const _ = require('lodash');
13
+ const Redis = require('ioredis');
14
+ const stackTrace = require('stack-trace');
15
+ /**
16
+ * Redis hook
17
+ */
18
+
19
+ module.exports = function(strapi) {
20
+ const hook = {
21
+ /**
22
+ * Default options
23
+ */
24
+
25
+ defaults: {
26
+ port: 6379,
27
+ host: 'localhost',
28
+ options: {
29
+ db: 0,
30
+ },
31
+ showFriendlyErrorStack: process.env.NODE_ENV !== 'production',
32
+ },
33
+
34
+ /**
35
+ * Initialize the hook
36
+ */
37
+
38
+ initialize: () => {
39
+ if (
40
+ _.isEmpty(strapi.models) ||
41
+ !_.pickBy(strapi.config.connections, {
42
+ connector: 'strapi-hook-redis',
43
+ })
44
+ ) {
45
+ return;
46
+ }
47
+
48
+ const connections = _.pickBy(strapi.config.connections, {
49
+ connector: 'strapi-hook-redis',
50
+ });
51
+
52
+ if (_.size(connections) === 0) {
53
+ return;
54
+ }
55
+
56
+ const done = _.after(_.size(connections), () => {
57
+ return;
58
+ });
59
+
60
+ // For each connection in the config register a new Knex connection.
61
+ _.forEach(connections, (connection, name) => {
62
+ // Apply defaults
63
+ _.defaults(connection.settings, strapi.config.hook.settings.redis);
64
+
65
+ const redis = new Redis(
66
+ _.defaultsDeep(
67
+ {
68
+ port: _.get(connection.settings, 'port'),
69
+ host: _.get(connection.settings, 'host'),
70
+ options: {
71
+ db: _.get(connection.options, 'database') || 0,
72
+ },
73
+ },
74
+ strapi.config.hook.settings.redis
75
+ )
76
+ );
77
+
78
+ redis.on('error', err => {
79
+ strapi.log.error(err);
80
+ process.exit(0);
81
+ });
82
+
83
+ // Utils function.
84
+ // Behavior: Try to retrieve data from Redis, if null
85
+ // execute callback and set the value in Redis for this serial key.
86
+ redis.cache = async ({ expired = 60 * 60, serial }, cb, type) => {
87
+ if (_.isEmpty(serial)) {
88
+ strapi.log.warn(
89
+ `Be careful, you're using cache() function of strapi-redis without serial`
90
+ );
91
+
92
+ const traces = stackTrace.get();
93
+
94
+ strapi.log.warn(
95
+ `> [${traces[1].getLineNumber()}] ${traces[1]
96
+ .getFileName()
97
+ .replace(strapi.config.appPath, '')}`
98
+ );
99
+
100
+ return await cb();
101
+ }
102
+
103
+ let cache = await redis.get(serial);
104
+
105
+ if (!cache) {
106
+ cache = await cb();
107
+
108
+ if (cache && _.get(connection, 'options.disabledCaching') !== true) {
109
+ switch (type) {
110
+ case 'json':
111
+ redis.set(serial, JSON.stringify(cache), 'ex', expired);
112
+ break;
113
+ case 'int':
114
+ default:
115
+ redis.set(serial, cache, 'ex', expired);
116
+ break;
117
+ }
118
+ }
119
+ }
120
+
121
+ switch (type) {
122
+ case 'int':
123
+ return parseInt(cache);
124
+ case 'float':
125
+ return _.toNumber(cache);
126
+ case 'json':
127
+ try {
128
+ return _.isObject(cache) ? cache : JSON.parse(cache);
129
+ } catch (e) {
130
+ return cache;
131
+ }
132
+ default:
133
+ return cache;
134
+ }
135
+ };
136
+
137
+ // Define as new connection.
138
+ strapi.connections[name] = redis;
139
+
140
+ // Expose global
141
+ if (_.get(connection, 'options.global') !== false) {
142
+ global[_.get(connection, 'options.globalName') || 'redis'] = redis;
143
+ }
144
+
145
+ if (_.get(connection, 'options.debug') === true) {
146
+ redis.monitor((err, monitor) => {
147
+ if (err) {
148
+ console.error(err);
149
+ }
150
+ // Entering monitoring mode.
151
+ monitor.on('monitor', (time, args) => {
152
+ console.log(time + ': ' + util.inspect(args));
153
+ });
154
+ });
155
+ }
156
+
157
+ redis.on('ready', () => {
158
+ done();
159
+ });
160
+ });
161
+ },
162
+ };
163
+
164
+ return hook;
165
+ };
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ // Public node modules
4
+ const rimraf = require('rimraf');
5
+
6
+ // Logger.
7
+ const logger = require('@akemona-org/strapi-utils').logger;
8
+
9
+ module.exports = (scope, success, error) => {
10
+ const Redis = require(`ioredis`);
11
+ const redis = new Redis({
12
+ port: scope.database.settings.port,
13
+ host: scope.database.settings.host,
14
+ password: scope.database.settings.password,
15
+ db: scope.database.settings.database,
16
+ });
17
+
18
+ redis.connect((err) => {
19
+ redis.disconnect();
20
+
21
+ if (err) {
22
+ logger.warn('Database connection has failed! Make sure your database is running.');
23
+ return error();
24
+ }
25
+
26
+ logger.info('The app has been connected to the database successfully!');
27
+
28
+ rimraf(scope.tmpPath, (err) => {
29
+ if (err) {
30
+ console.log(`Error removing connection test folder: ${scope.tmpPath}`);
31
+ }
32
+ logger.info('Copying the dashboard...');
33
+
34
+ success();
35
+ });
36
+ });
37
+ };
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@akemona-org/strapi-hook-redis",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "3.7.0",
7
+ "description": "Redis hook for the Strapi framework",
8
+ "homepage": "https://strapi.akemona.com",
9
+ "keywords": [
10
+ "redis",
11
+ "hook",
12
+ "memory",
13
+ "cache",
14
+ "strapi"
15
+ ],
16
+ "directories": {
17
+ "lib": "./lib"
18
+ },
19
+ "main": "./lib",
20
+ "dependencies": {
21
+ "@akemona-org/strapi-utils": "3.7.0",
22
+ "ioredis": "^4.27.1",
23
+ "lodash": "4.17.21",
24
+ "rimraf": "3.0.2",
25
+ "stack-trace": "0.0.10"
26
+ },
27
+ "author": {
28
+ "email": "strapi@akemona.com",
29
+ "name": "Akemona team",
30
+ "url": "https://strapi.akemona.com"
31
+ },
32
+ "maintainers": [
33
+ {
34
+ "name": "Akemona team",
35
+ "email": "strapi@akemona.com",
36
+ "url": "https://strapi.akemona.com"
37
+ }
38
+ ],
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git://github.com/akemona/strapi.git"
42
+ },
43
+ "bugs": {
44
+ "url": "https://github.com/akemona/strapi/issues"
45
+ },
46
+ "engines": {
47
+ "node": ">=10.16.0 <=14.x.x",
48
+ "npm": ">=6.0.0"
49
+ },
50
+ "license": "SEE LICENSE IN LICENSE",
51
+ "scripts": {
52
+ "test": "echo \"no tests yet\""
53
+ },
54
+ "gitHead": "129a8d6191b55810fd66448dcc47fee829df986c"
55
+ }