@onlineapps/conn-base-cache 1.0.3 → 1.0.5
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/package.json +2 -2
- package/src/config.js +3 -3
- package/src/defaults.js +1 -3
- package/src/index.js +9 -12
- package/tests/integration/cache.integration.test.js +3 -3
- package/tests/unit/CacheConnector.edge.test.js +16 -2
- package/tests/unit/CacheConnector.test.js +15 -1
- package/onlineapps-conn-base-cache-1.0.1.tgz +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onlineapps/conn-base-cache",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Redis cache connector with TTL, invalidation, and namespace support for OA Drive microservices",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"author": "OA Drive Team",
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@onlineapps/runtime-config": "1.0.
|
|
23
|
+
"@onlineapps/runtime-config": "1.0.2",
|
|
24
24
|
"ioredis": "^5.3.2"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
package/src/config.js
CHANGED
|
@@ -26,9 +26,9 @@ const DEFAULTS = require('./defaults');
|
|
|
26
26
|
const runtimeCfg = createRuntimeConfig({
|
|
27
27
|
defaults: DEFAULTS,
|
|
28
28
|
schema: {
|
|
29
|
-
//
|
|
30
|
-
host: { env: 'REDIS_HOST',
|
|
31
|
-
port: { env: 'REDIS_PORT',
|
|
29
|
+
// Redis topology (FAIL-FAST)
|
|
30
|
+
host: { env: 'REDIS_HOST', required: true },
|
|
31
|
+
port: { env: 'REDIS_PORT', required: true, type: 'number' },
|
|
32
32
|
password: { env: 'REDIS_PASSWORD' },
|
|
33
33
|
db: { env: 'REDIS_DB', defaultKey: 'defaultDb', type: 'number' },
|
|
34
34
|
|
package/src/defaults.js
CHANGED
|
@@ -4,13 +4,11 @@
|
|
|
4
4
|
* Module-owned defaults for @onlineapps/conn-base-cache.
|
|
5
5
|
*
|
|
6
6
|
* Ownership rule:
|
|
7
|
-
* - Defaults that belong to this cache connector live here (
|
|
7
|
+
* - Defaults that belong to this cache connector live here (TTL, retry policy, key prefix, db).
|
|
8
8
|
* - Callers can override via explicit config or environment variables.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
module.exports = {
|
|
12
|
-
defaultHost: 'localhost',
|
|
13
|
-
defaultPort: 6379,
|
|
14
12
|
keyPrefix: 'cache:',
|
|
15
13
|
defaultDb: 0,
|
|
16
14
|
defaultTTLSeconds: 3600,
|
package/src/index.js
CHANGED
|
@@ -23,11 +23,8 @@ const runtimeCfg = require('./config');
|
|
|
23
23
|
* @class CacheConnector
|
|
24
24
|
*
|
|
25
25
|
* @example <caption>Basic Usage</caption>
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* port: 6379,
|
|
29
|
-
* defaultTTL: 3600
|
|
30
|
-
* });
|
|
26
|
+
* // FAIL-FAST: requires REDIS_HOST and REDIS_PORT env (or pass explicit host/port)
|
|
27
|
+
* const cache = new CacheConnector({ defaultTTL: 3600 });
|
|
31
28
|
* await cache.connect();
|
|
32
29
|
* await cache.set('user:123', { name: 'John' }, 600);
|
|
33
30
|
* const user = await cache.get('user:123');
|
|
@@ -44,8 +41,8 @@ class CacheConnector {
|
|
|
44
41
|
*
|
|
45
42
|
* @constructor
|
|
46
43
|
* @param {Object} [config={}] - Configuration options
|
|
47
|
-
* @param {string}
|
|
48
|
-
* @param {number}
|
|
44
|
+
* @param {string} config.host - Redis server host (or set REDIS_HOST env)
|
|
45
|
+
* @param {number} config.port - Redis server port (or set REDIS_PORT env)
|
|
49
46
|
* @param {string} [config.password] - Redis password for authentication
|
|
50
47
|
* @param {number} [config.db=0] - Redis database number to use
|
|
51
48
|
* @param {number} [config.defaultTTL=3600] - Default TTL in seconds for cached items
|
|
@@ -59,8 +56,8 @@ class CacheConnector {
|
|
|
59
56
|
*
|
|
60
57
|
* @example <caption>Full Configuration</caption>
|
|
61
58
|
* const cache = new CacheConnector({
|
|
62
|
-
* host: 'redis.
|
|
63
|
-
* port:
|
|
59
|
+
* host: 'redis.internal',
|
|
60
|
+
* port: 6379,
|
|
64
61
|
* password: 'secret',
|
|
65
62
|
* namespace: 'my-service',
|
|
66
63
|
* defaultTTL: 1800,
|
|
@@ -877,8 +874,8 @@ class CacheConnector {
|
|
|
877
874
|
|
|
878
875
|
/**
|
|
879
876
|
* @typedef {Object} CacheConfig
|
|
880
|
-
* @property {string}
|
|
881
|
-
* @property {number}
|
|
877
|
+
* @property {string} host - Redis host (or set REDIS_HOST env)
|
|
878
|
+
* @property {number} port - Redis port (or set REDIS_PORT env)
|
|
882
879
|
* @property {string} [password] - Redis password
|
|
883
880
|
* @property {number} [db=0] - Redis database
|
|
884
881
|
* @property {number} [defaultTTL=3600] - Default TTL in seconds
|
|
@@ -932,7 +929,7 @@ module.exports = CacheConnector;
|
|
|
932
929
|
*
|
|
933
930
|
* @example
|
|
934
931
|
* const cache = CacheConnector.create({
|
|
935
|
-
*
|
|
932
|
+
* // FAIL-FAST: requires REDIS_HOST + REDIS_PORT env (or pass host + port)
|
|
936
933
|
* namespace: 'my-service'
|
|
937
934
|
* });
|
|
938
935
|
*/
|
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
const CacheConnector = require('../../src/index');
|
|
9
9
|
|
|
10
10
|
const SKIP_INTEGRATION = process.env.SKIP_INTEGRATION === 'true';
|
|
11
|
-
const REDIS_HOST = process.env.REDIS_HOST
|
|
12
|
-
const REDIS_PORT = process.env.REDIS_PORT
|
|
11
|
+
const REDIS_HOST = process.env.REDIS_HOST;
|
|
12
|
+
const REDIS_PORT = process.env.REDIS_PORT ? parseInt(process.env.REDIS_PORT, 10) : undefined;
|
|
13
13
|
|
|
14
14
|
describe('CacheConnector - Integration Tests @integration', () => {
|
|
15
|
-
if (SKIP_INTEGRATION) {
|
|
15
|
+
if (SKIP_INTEGRATION || !REDIS_HOST || !REDIS_PORT) {
|
|
16
16
|
it.skip('Skipping integration tests (SKIP_INTEGRATION=true)', () => {});
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
@@ -12,8 +12,18 @@ const CacheConnector = require('../../src/index');
|
|
|
12
12
|
describe('CacheConnector - Edge Cases & Full Coverage @unit', () => {
|
|
13
13
|
let cacheConnector;
|
|
14
14
|
let mockRedisClient;
|
|
15
|
+
let oldEnv;
|
|
15
16
|
|
|
16
17
|
beforeEach(() => {
|
|
18
|
+
oldEnv = {
|
|
19
|
+
REDIS_HOST: process.env.REDIS_HOST,
|
|
20
|
+
REDIS_PORT: process.env.REDIS_PORT,
|
|
21
|
+
REDIS_PASSWORD: process.env.REDIS_PASSWORD,
|
|
22
|
+
REDIS_DB: process.env.REDIS_DB,
|
|
23
|
+
};
|
|
24
|
+
process.env.REDIS_HOST = '127.0.0.1';
|
|
25
|
+
process.env.REDIS_PORT = '6379';
|
|
26
|
+
|
|
17
27
|
mockRedisClient = {
|
|
18
28
|
get: jest.fn(),
|
|
19
29
|
set: jest.fn(),
|
|
@@ -39,13 +49,17 @@ describe('CacheConnector - Edge Cases & Full Coverage @unit', () => {
|
|
|
39
49
|
|
|
40
50
|
afterEach(() => {
|
|
41
51
|
jest.clearAllMocks();
|
|
52
|
+
for (const k of Object.keys(oldEnv)) {
|
|
53
|
+
if (oldEnv[k] === undefined) delete process.env[k];
|
|
54
|
+
else process.env[k] = oldEnv[k];
|
|
55
|
+
}
|
|
42
56
|
});
|
|
43
57
|
|
|
44
58
|
describe('Constructor Edge Cases', () => {
|
|
45
59
|
it('should handle undefined config', () => {
|
|
46
60
|
const cache = new CacheConnector(undefined);
|
|
47
61
|
expect(cache.config).toBeDefined();
|
|
48
|
-
expect(cache.config.host).toBe('
|
|
62
|
+
expect(cache.config.host).toBe('127.0.0.1');
|
|
49
63
|
});
|
|
50
64
|
|
|
51
65
|
it('should handle null config', () => {
|
|
@@ -87,7 +101,7 @@ describe('CacheConnector - Edge Cases & Full Coverage @unit', () => {
|
|
|
87
101
|
|
|
88
102
|
describe('Connection Edge Cases', () => {
|
|
89
103
|
beforeEach(() => {
|
|
90
|
-
cacheConnector = new CacheConnector({ host: '
|
|
104
|
+
cacheConnector = new CacheConnector({ host: '127.0.0.1', port: 6379 });
|
|
91
105
|
});
|
|
92
106
|
|
|
93
107
|
it('should handle connection timeout', async () => {
|
|
@@ -12,8 +12,18 @@ const CacheConnector = require('../../src/index');
|
|
|
12
12
|
describe('CacheConnector - Unit Tests @unit', () => {
|
|
13
13
|
let cacheConnector;
|
|
14
14
|
let mockRedisClient;
|
|
15
|
+
let oldEnv;
|
|
15
16
|
|
|
16
17
|
beforeEach(() => {
|
|
18
|
+
oldEnv = {
|
|
19
|
+
REDIS_HOST: process.env.REDIS_HOST,
|
|
20
|
+
REDIS_PORT: process.env.REDIS_PORT,
|
|
21
|
+
REDIS_PASSWORD: process.env.REDIS_PASSWORD,
|
|
22
|
+
REDIS_DB: process.env.REDIS_DB,
|
|
23
|
+
};
|
|
24
|
+
process.env.REDIS_HOST = '127.0.0.1';
|
|
25
|
+
process.env.REDIS_PORT = '6379';
|
|
26
|
+
|
|
17
27
|
// Create mock Redis client with all methods
|
|
18
28
|
mockRedisClient = {
|
|
19
29
|
get: jest.fn(),
|
|
@@ -43,12 +53,16 @@ describe('CacheConnector - Unit Tests @unit', () => {
|
|
|
43
53
|
|
|
44
54
|
afterEach(() => {
|
|
45
55
|
jest.clearAllMocks();
|
|
56
|
+
for (const k of Object.keys(oldEnv)) {
|
|
57
|
+
if (oldEnv[k] === undefined) delete process.env[k];
|
|
58
|
+
else process.env[k] = oldEnv[k];
|
|
59
|
+
}
|
|
46
60
|
});
|
|
47
61
|
|
|
48
62
|
describe('constructor', () => {
|
|
49
63
|
it('should initialize with default configuration', () => {
|
|
50
64
|
const cache = new CacheConnector();
|
|
51
|
-
expect(cache.config.host).toBe('
|
|
65
|
+
expect(cache.config.host).toBe('127.0.0.1');
|
|
52
66
|
expect(cache.config.port).toBe(6379);
|
|
53
67
|
expect(cache.config.keyPrefix).toBe('cache:');
|
|
54
68
|
expect(cache.config.defaultTTL).toBe(3600);
|
|
Binary file
|