Garbage collector for the cached items

@method clean
@private
@return {Undefined} undefined
3800
function () {
		return utility.iterate( cache.items, function ( v, k ) {
			if ( cache.expired( k ) ) {
				cache.expire( k, true );
			}
		});
	}
419134
function clean(nodes) {
        mark(nodes, []);
        return prune(nodes);
        function mark(nodes, ancestors) {
          _.each(nodes, function(node) {
            if (node.publish) {
              node.good = true;
              _.each(ancestors, function(ancestor) {
                ancestor.good = true;
              });
            }
            mark(node.children || [], ancestors.concat([ node ]));
          });
        }
        function prune(nodes) {
          var newNodes = [];
          _.each(nodes, function(node) {
            node.children = prune(node.children || []);
            if (node.good) {
              newNodes.push(node);
            }
          });
          return newNodes;
        }
      }
626295
function cleanObject(obj) {
  if (obj instanceof Object) {
    Object.keys(obj).forEach((key) => (obj[key] == null) && delete obj[key]);
  }
  return obj;
}
304128
function garbageCollector(dispatcher) {
    const instanceId = dispatcher.getInstanceId();
    const config = dispatcher.getConfig();
    const events = dispatcher.getEvents();
    const keys = dispatcher.getKeys();
    const { keyQueueName, keyQueueNameProcessingCommon, keyQueueNameDead, keyGCLock, keyGCLockTmp } = keys;

    const logger = dispatcher.getLogger();
    const lockManager = lockManagerFn(dispatcher, keyGCLock, keyGCLockTmp);

    /**
     *
     * @type {(object|null)}
     */
    let client = null;

    /**
     *
     * @type {number}
     */
    let timer = 0;

    /**
     * 
     * @param {string} message
     */
    function debug(message) {
        logger.debug({ gc: true }, message);
    }

    /**
     *
     * @param {Array} queues
     * @param {function} done
     */
    function collectProcessingQueuesMessages(queues, done) {
        if (queues.length) {
            let deadConsumer = false;
            const processingQueueName = queues.pop();
            debug(`Inspecting processing queue [${processingQueueName}]... `);
            const segments = redisKeys.getKeySegments(processingQueueName);
            if (segments.consumerId !== instanceId) {
                const purgeProcessingQueue = (name) => {
                    util.purgeProcessingQueue(client, name, (err) => {
                        if (err) dispatcher.error(err);
                    });
                };
                const onMessageCollected = (err) => {
                    if (err) dispatcher.error(err);
                    else {
                        if (deadConsumer) purgeProcessingQueue(processingQueueName);
                        collectProcessingQueuesMessages(queues, done);
                    }
                };
                const onRange = (err, range) => {
                    if (err) dispatcher.error(err);
                    else if (range.length) {
                        const message = new Message(range[0]);
                        const uuid = message.getId();
                        debug(`Collecting message [${uuid}]...`);
                        if (hasExpired(message)) {
                            collectExpiredMessage(message, processingQueueName, onMessageCollected);
                        } else {
                            collectMessage(message, processingQueueName, onMessageCollected);
                        }
                    } else onMessageCollected();
                };
                const onConsumerOnline = (err, online) => {
                    if (err) dispatcher.err(err);
                    else if (online) {
                        debug(`Consumer ID [${segments.consumerId}] is alive!`);
                        collectProcessingQueuesMessages(queues, done);
                    } else {
                        deadConsumer = true;
                        debug(`Consumer ID [${segments.consumerId}] seems to be dead. Fetching queue message...`);
                        client.lrange(processingQueueName, 0, 0, onRange);
                    }
                };
                debug(`Is consumer ID [${segments.consumerId}] alive?`);
                heartBeat.isOnline(client, segments.queueName, segments.consumerId, onConsumerOnline);
            } else {
                debug('Skipping self consumer instance ID...');
                collectProcessingQueuesMessages(queues, done);
            }
        } else done();
    }

    /**
     *
     */
    function runInspectionTimer() {
        if (dispatcher.isRunning()) {
            debug(`Waiting for ${GC_INSPECTION_INTERVAL} before a new iteration...`);
            timer = setTimeout(() => {
                debug('Time is up...');
                inspectProcessingQueues();
            }, GC_INSPECTION_INTERVAL);
        }
    }

    /**
     *
     */
    function inspectProcessingQueues() {
        lockManager.acquire((err) => {
            if (err) dispatcher.error(err);
            else {
                debug('Inspecting processing queues...');
                util.getProcessingQueuesOf(client, keyQueueNameProcessingCommon, (e, result) => {
                    if (e) dispatcher.error(e);
                    else if (result && result.length) {
                        debug(`Found [${result.length}] processing queues`);
                        collectProcessingQueuesMessages(result, runInspectionTimer);
                    } else {
                        debug('No processing queues found');
                        runInspectionTimer();
                    }
                });
            }
        });
    }

    /**
     * Move message to dead-letter queue when max attempts threshold is reached
     * otherwise requeue it again
     *
     * @param {object} message
     * @param {string} processingQueue
     * @param {function} cb
     */
    function collectMessage(message, processingQueue, cb) {
        const threshold = message.getRetryThreshold();
        const messageRetryThreshold = typeof threshold === 'number' ? threshold : dispatcher.getMessageRetryThreshold();

        const delay = message.getRetryDelay();
        const messageRetryDelay = typeof delay === 'number' ? delay : dispatcher.getMessageRetryDelay();

        let destQueueName = null;
        let delayed = false;
        let requeued = false;

        const multi = client.multi();

        /**
         * Only exceptions from non periodic messages are handled.
         * Periodic messages are ignored once they are delivered to a consumer.
         */
        if (!dispatcher.isPeriodic(message)) {
            const uuid = message.getId();

            /**
             * Attempts
             */
            const attempts = increaseAttempts(message);

            /**
             *
             */
            if (attempts < messageRetryThreshold) {
                debug(`Trying to consume message ID [${uuid}] again (attempts: [${attempts}]) ...`);
                if (messageRetryDelay) {
                    debug(`Scheduling message ID [${uuid}]  (delay: [${messageRetryDelay}])...`);
                    message.setScheduledDelay(messageRetryDelay);
                    dispatcher.schedule(message, multi);
                    delayed = true;
                } else {
                    debug(`Message ID [${uuid}] is going to be enqueued immediately...`);
                    destQueueName = keyQueueName;
                    requeued = true;
                }
            } else {
                debug(`Message ID [${uuid}] has exceeded max retry threshold...`);
                destQueueName = keyQueueNameDead;
            }

            /**
             *
             */
            if (destQueueName) {
                debug(`Moving message [${uuid}] to queue [${destQueueName}]...`);
                multi.lpush(destQueueName, message.toString());
            }
            multi.rpop(processingQueue);
            multi.exec((err) => {
                if (err) cb(err);
                else {
                    if (dispatcher.isTest()) {
                        if (requeued) dispatcher.emit(events.MESSAGE_REQUEUED, message);
                        else if (delayed) dispatcher.emit(events.MESSAGE_DELAYED, message);
                        else dispatcher.emit(events.MESSAGE_DEAD_LETTER, message);
                    }
                    cb();
                }
            });
        } else {
            client.rpop(processingQueue, cb);
        }
    }

    /**
     *
     * @param {object} message
     * @param {string} processingQueue
     * @param {function} cb
     */
    function collectExpiredMessage(message, processingQueue, cb) {
        const id = message.getId();
        debug(`Processing expired message [${id}]...`);
        // Just pop it out
        client.rpop(processingQueue, (err) => {
            if (err) dispatcher.error(err);
            else {
                cb();
                if (dispatcher.isTest()) dispatcher.emit(events.MESSAGE_DESTROYED, message);
            }
        });
    }

    /**
     *
     * @param {object} message
     * @returns {boolean}
     */
    function hasExpired(message) {
        const ttl = message.getTTL();
        const messageTTL = typeof ttl === 'number' ? ttl : dispatcher.getConsumerMessageTTL();
        if (messageTTL) {
            const curTime = new Date().getTime();
            const createdAt = message.getCreatedAt();
            return (((createdAt + messageTTL) - curTime) <= 0);
        }
        return false;
    }


    /**
     *
     * @param message
     * @return {number}
     */
    function increaseAttempts(message) {
        message[Message.PROPERTY_ATTEMPTS] += 1;
        return message[Message.PROPERTY_ATTEMPTS];
    }

    return {
        start() {
            client = redisClient.getNewInstance(config);
            lockManager.setRedisClient(client);
            inspectProcessingQueues();
        },

        stop() {
            if (timer) clearTimeout(timer);
            lockManager.release(() => {
                client.end(true);
                client = null;
                dispatcher.emit(events.GC_HALT);
            });
        },

        collectMessage,

        collectExpiredMessage,

        hasExpired,
    };
}
1136164
function flush () {
  return storage.keys().then(keys => {
    return storage.remove(
      keys.filter(key => key.indexOf(prefix) === 0 || key.indexOf(expire) === 0)
    )
  })
}
123095
function memoize( f ) {
   const cache = {};
   return key => {
      let value = cache[ key ];
      if( value === undefined ) {
         value = f( key );
         cache[ key ] = value;
      }
      return value;
   };
}
611279
function clean(collection) {
    var ns = /\S/;
    collection.each(function(el) {
        var d = el,
            n = d.firstChild,
            ni = -1,
            nx;
        while (n) {
            nx = n.nextSibling;
            if (n.nodeType == 3 && !ns.test(n.nodeValue)) {
                d.removeChild(n);
            } else {
                n.nodeIndex = ++ni; // FIXME not sure what this is for, and causes IE to bomb (the setter) - @rem
            }
            n = nx;
        }
    });
}
374732
function _getCached( cached, data )
	{
		if( !data ){ return cached; }

		let i = -1, len = cached.length;
        while (++i < len) {
            cached[i].data = data;
        }
        return cached;
	}
135245
function clearCache() {
    this._identityId = null;
    delete this.params.IdentityId;

    var poolId = this.params.IdentityPoolId;
    var loginId = this.params.LoginId || '';
    delete this.storage[this.localStorageKey.id + poolId + loginId];
    delete this.storage[this.localStorageKey.providers + poolId + loginId];
  }
284456
private function cleanMemory()
    {
        $usedMemory = round(memory_get_usage() / 1024 / 1024, 0);
        if ($usedMemory > 250 && $this->loopWithoutCleaningMemory > 300) {
            BeanFactoryCache::clearCache();
            $this->loopWithoutCleaningMemory = 0;
            $msg = "Memory Cleaned because usage is around {$usedMemory} Mib. ";
            $msg.= 'Collected ' . gc_collect_cycles()  . ' cycles.';
            $this->getLogger()->warning($this->logPrefix . $msg);
        } else {
            $this->loopWithoutCleaningMemory++;
        }

        return true;
    }
1321860
function cacher() {
  let cache = false;
  return function(read) {
    return function readable(end, cb) {
      if (end === null && cache) {
        cb(null, cache);
      } else {
        read(end, function (end, data) {
          cache = data;
          cb(end, data)
        });
      }
    }
  }
}
1312642
function loadUncached () {
    // execute original query
    return this.query.executeQuery()
    // cache result
    .then(results => {
        // cache results if there are any
        if (results.length > 0) {
            // cache results - do not wait to complete
            this.cacheResults(results)
        }
        // resolve with results
        return results
    })
}
1353097
function cleanRes (res) {
    return {
        body: res.body,
        rawHeaders: res.rawHeaders,
        statusCode: res.statusCode,
    }
}
329691
function CacheManager() {
  var self = this;
  self.caches = {};

  Object.defineProperty(self, 'stats', {
    get: function () {
      return Object.keys(self.caches).reduce(function(a,region){
        a[region] = self.caches[region].stats;
        return a;
      },{});
    }
  });
}
623092
function Cacherrr() {
      var expire = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0];
      var exclude = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];

      _classCallCheck(this, Cacherrr);

      // time for cache to expire
      this.expire = expire;
      // array of paths to be excluded from caching
      this.exclude = exclude;
      // cache entries
      this.entries = {};
    }
1141863
function doDelete(filter) {
    console.log('folder delete called', filter);
    return new Promise(function(resolve, reject) {
      var keysPromise;
      var deletes = [], deletedKeys = [];

      if (common.keyTypes.indexOf(typeof filter) !== -1) {
        filter = [filter];
      } else if (filter instanceof Array) {
      } else if (filter === true || typeof filter === 'object') {
        console.log('object/true delete handler');
        // Get the existing keys
        readKeys().then(function(existingKeys) {
          console.log('existing keys are', existingKeys);
          if (filter === true) {
            deletedKeys = existingKeys;
            existingKeys.forEach(function(key) {
              deletes.push(saveData(key));
            });
          } else {
            if (options.cacheValues) {
              console.log('processing filter', filter);
              common.processFilter(cachedData, filter, function(key) {
                console.log('deleting matching item', key);
                deletes.push(saveData(key).then(function() {
                  delete cachedData[key];
                  deletedKeys.push(key);
                }));
              });
            } else {
              console.log('processing filter on non-cached data');
              existingKeys.forEach(function(key) {
                deletes.push(readData(key).then(function(data) {
                  console.log('loaded data for', key);
                  if (common.runFilter(data, filter)) {
                    return saveData(key).then(function() {
                      if (options.cacheKeys) {
                        cachedKeys.splice(cachedKeys.indexOf(key), 1);
                      }
                      deletedKeys.push(key);
                    });
                  } else {
                    return Promise.resolve();
                  }
                }));
              });
            }
          }

          return Promise.all(deletes).then(function() {
            if (filter === true && options.cacheValues) {
              cachedData = {};
            }
            if (options.cacheValues && options.cacheKeys) {
              cachedKeys = Object.keys(cachedData);
            } else if (filter === true && options.cacheKeys) {
              cachedKeys = [];
            }
            resolve(deletedKeys);
          });
        }).catch(function(error) {
          reject(error);
        });

        return;
      } else {
        reject({
          message: 'filter needs to be an object containing a filter'
        });
        return;
      }

      /* Get the list of keys */
      if (options.cacheKeys) {
        keysPromise = Promise.resolve(cachedKeys);
      } else {
        keysPromise = readKeys();
      }

      keysPromise.then(function(keys) {
        console.log('got current keys', keys);
        filter.forEach(function(id) {
          if (keys.indexOf(id) !== -1) {
            // TODO XXX Remove once file watch implemented
            deletes.push(saveData(id).then(function() {
              if (options.cacheValues) {
                delete cachedData[id];
              } else if (options.cacheKeys) {
                // Delete remove key from cached keys if caching keys but not values
                cachedKeys.splice(cachedKeys.indexOf(id), 1);
              }

              deletedKeys.push(id);
            }));
          }
        });

        Promise.all(deletes).then(function() {
          // Redo cached keys if caching keys and values (otherwise done above)
          if (options.cacheKeys && options.cacheValues) {
            cachedKeys = Object.keys(cachedData);
          }
          resolve(deletedKeys);
        });
      }).catch(function(error) {
        reject(error);
      });
    });
  }
607028
function makeStorageHelper(storage) {
  return {
    storage: storage,
    set: function set(name, value, minutes) {
      if (value == null) {
        this.storage.removeItem(name);
      } else {
        this.storage.setItem(name, JSON.stringify({
          value: value,
          expired_at: minutes ? new Date().getTime() + minutes * 60 * 1000 : null
        }));
      }
    },
    get: function get$$1(name) {
      var t = this.storage.getItem(name);

      if (t) {
        t = JSON.parse(t);

        if (!t.expired_at || t.expired_at > new Date().getTime()) {
          return t.value;
        } else {
          this.storage.removeItem(name);
        }
      }

      return null;
    },
    clear: function clear() {
      this.storage.clear();
    }
  };
}