Skip to content

Commit

Permalink
refactor: cache module
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Apr 13, 2024
1 parent b8141cb commit 1360d8e
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions lib/cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

// Modules
const _ = require('lodash');
const fs = require('fs');
const jsonfile = require('jsonfile');
const Log = require('./logger');
Expand All @@ -28,11 +27,11 @@ class Cache extends NodeCache {
*
* @since 3.0.0
* @alias lando.cache.set
* @param {String} key The name of the key to store the data with.
* @param {Any} data The data to store in the cache.
* @param {string} key The name of the key to store the data with.
* @param {any} data The data to store in the cache.
* @param {Object} [opts] Options to pass into the cache
* @param {Boolean} [opts.persist=false] Whether this cache data should persist between processes. Eg in a file instead of memory
* @param {Integer} [opts.ttl=0] Seconds the cache should live. 0 mean forever.
* @param {boolean} [opts.persist=false] Whether this cache data should persist between processes. Eg in a file instead of memory
* @param {number} [opts.ttl=0] Seconds the cache should live. 0 mean forever.
* @example
* // Add a string to the cache
* lando.cache.set('mykey', 'mystring');
Expand All @@ -47,17 +46,20 @@ class Cache extends NodeCache {
// Unsafe cache key patterns
const patterns = {
controlRe: /[\x00-\x1f\x80-\x9f]/g,
illegalRe: /[\/\?<>\\:\*\|":]/g,
illegalRe: /[/?<>\\*|":]/g,
reservedRe: /^\.+$/,
windowsReservedRe: /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i,
windowsTrailingRe: /[\. ]+$/,
windowsReservedRe: /^(con|prn|aux|nul|com\d|lpt\d)(\..*)?$/i,
windowsTrailingRe: /[. ]+$/,
};
_.map(patterns, pattern => {
if (key.match(pattern)) throw new Error(`Invalid cache key: ${key}`);

Object.values(patterns).forEach(pattern => {
if (pattern.test(key)) {
throw new Error(`Invalid cache key: ${key}`);
}
});

// Try to set cache
if (this.__set(key, data, ttl)) {
if (super.set(key, data, ttl)) {
this.log.debug('Cached %j with key %s for %j', data, key, {persist, ttl});
} else {
this.log.debug('Failed to cache %j with key %s', data, key);
Expand All @@ -72,15 +74,15 @@ class Cache extends NodeCache {
*
* @since 3.0.0
* @alias lando.cache.get
* @param {String} key The name of the key to retrieve the data.
* @return {Any} The data stored in the cache if applicable.
* @param {string} key The name of the key to retrieve the data.
* @return {any} The data stored in the cache if applicable.
* @example
* // Get the data stored with key mykey
* const data = lando.cache.get('mykey');
*/
get(key) {
// Get from cache
const memResult = this.__get(key);
const memResult = super.get(key);

// Return result if its in memcache
if (memResult) {
Expand Down Expand Up @@ -108,7 +110,7 @@ class Cache extends NodeCache {
*/
remove(key) {
// Try to get cache
if (this.__del(key)) this.log.debug('Removed key %s from memcache.', key);
if (super.del(key)) this.log.debug('Removed key %s from memcache.', key);
else this.log.debug('Failed to remove key %s from memcache.', key);

// Also remove file if applicable
Expand All @@ -120,21 +122,6 @@ class Cache extends NodeCache {
};
};

/*
* Stores the old get method.
*/
Cache.prototype.__get = NodeCache.prototype.get;

/*
* Stores the old set method.
*/
Cache.prototype.__set = NodeCache.prototype.set;

/*
* Stores the old del method.
*/
Cache.prototype.__del = NodeCache.prototype.del;

/*
* Return the class
*/
Expand Down

0 comments on commit 1360d8e

Please sign in to comment.