eip-cloud-services 1.1.9 → 1.1.11

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.
Files changed (3) hide show
  1. package/package.json +2 -2
  2. package/src/cdn.js +19 -1
  3. package/src/redis.js +40 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eip-cloud-services",
3
- "version": "1.1.9",
3
+ "version": "1.1.11",
4
4
  "description": "Houses a collection of helpers for connecting with Cloud services.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -24,4 +24,4 @@
24
24
  "mysql": "^2.18.1",
25
25
  "redis": "^4.6.7"
26
26
  }
27
- }
27
+ }
package/src/cdn.js CHANGED
@@ -11,6 +11,8 @@ const packageJson = require ( '../package.json' );
11
11
  const { cwd } = require ( 'process' );
12
12
  const { log } = config?.s3?.logsFunction ? require ( `${ cwd ()}/${config.s3.logsFunction}` ) : console;
13
13
 
14
+ const redis = require('./redis');
15
+
14
16
  /**
15
17
  * Create a CDN invalidation for the specified key(s) and environment.
16
18
  *
@@ -44,8 +46,24 @@ exports.createInvalidation = async ( cdn, key, environment = 'production' ) => {
44
46
  throw new Error ( 'Invalid key argument. Expected a string or an array of strings.' );
45
47
  }
46
48
 
49
+ if ( config?.redis?.host ) {
50
+ const redisKey = `cdn-invalidation:${cdn}:${environment}:${key}`;
51
+ const redisValue = await redis.get(redisKey);
52
+
53
+ if(redisValue) {
54
+ if ( config.cdn.log )
55
+ log ( `CDN [INVALIDATE]: Invalidation already in progress - skipping: ${paths.map ( path => `https://${cdn}${environment !== 'production' ? '-test' : ''}.eip.telegraph.co.uk${path}` ).join ( ', ' )}\n` );
56
+ await redis.increment(redisKey);
57
+
58
+ return;
59
+ }
60
+ else{
61
+ await redis.set(redisKey, 1, cdnSettings.type === 'google' ? 300 : 120); // 5 minutes for google, 2 minutes for amazon
62
+ }
63
+ }
64
+
47
65
  if ( config.cdn.log )
48
- log ( `CDN [INVALIDATE]: ${paths.map ( path => `https://${cdn}${environment !== 'production' ? '-test' : ''}.eip.telegraph.co.uk/${path}` ).join ( ', ' )}\n` );
66
+ log ( `CDN [INVALIDATE]: ${paths.map ( path => `https://${cdn}${environment !== 'production' ? '-test' : ''}.eip.telegraph.co.uk${path}` ).join ( ', ' )}\n` );
49
67
 
50
68
  switch ( cdnSettings.type ) {
51
69
  case 'google':
package/src/redis.js CHANGED
@@ -607,6 +607,46 @@ exports.listRange = async ( key, start = 0, end = -1 ) => {
607
607
  }
608
608
  };
609
609
 
610
+ /**
611
+ * Increments the integer value of a key by the provided amount, or by 1 if no amount is specified.
612
+ * @param {string} key - The key whose value is to be incremented.
613
+ * @param {number} [increment=1] - The amount by which to increment the key's value. Defaults to 1.
614
+ * @returns {Promise<number>} - A promise that resolves with the new value after increment.
615
+ *
616
+ * @description This method uses the Redis INCRBY command to increment the value of the key.
617
+ * It defaults to incrementing by 1 if no specific increment value is provided.
618
+ */
619
+ exports.increment = async (key, increment = 1) => {
620
+ try {
621
+ const client = await getClient();
622
+ const fullKey = (!config?.redis?.prefix || key.startsWith(config?.redis?.prefix)) ? key : config?.redis?.prefix + key;
623
+ return await client.incrby(fullKey, increment);
624
+ } catch (error) {
625
+ console.log('REDIS LIB ERROR [increment]', error);
626
+ throw error;
627
+ }
628
+ };
629
+
630
+ /**
631
+ * Decrements the integer value of a key by the provided amount, or by 1 if no amount is specified.
632
+ * @param {string} key - The key whose value is to be decremented.
633
+ * @param {number} [decrement=1] - The amount by which to decrement the key's value. Defaults to 1.
634
+ * @returns {Promise<number>} - A promise that resolves with the new value after decrement.
635
+ *
636
+ * @description This method uses the Redis DECRBY command to decrement the value of the key.
637
+ * It defaults to decrementing by 1 if no specific decrement value is provided.
638
+ */
639
+ exports.decrement = async (key, decrement = 1) => {
640
+ try {
641
+ const client = await getClient();
642
+ const fullKey = (!config?.redis?.prefix || key.startsWith(config?.redis?.prefix)) ? key : config?.redis?.prefix + key;
643
+ return await client.decrby(fullKey, decrement);
644
+ } catch (error) {
645
+ console.log('REDIS LIB ERROR [decrement]', error);
646
+ throw error;
647
+ }
648
+ };
649
+
610
650
  /**
611
651
  * Retrieves the expiration time of a key in seconds.
612
652
  * @param {string} key - The key to get the expiration time of.