<commit_before>'use strict';

angular.module('spinnaker.caches.core', [
  'angular-data.DSCacheFactory',
])
.factory('deckCacheFactory', function(DSCacheFactory) {

    var caches = Object.create(null);

    function buildCacheKey(namespace, cacheId) {
      if (!namespace || !cacheId) {
        return namespace || cacheId;
      }
      return [namespace, cacheId].join(':');
    }

    function clearCache(namespace, key) {
      if (caches[key] && caches[key].destroy) {
        caches[key].destroy();
        createCache(namespace, key, caches[key].config);
      }
    }

    function clearCaches() {
      Object.keys(caches).forEach(clearCache);
    }

    function createCache(namespace, key, config) {
      addLocalStorageCache(namespace, key, config);
    }

    // Provides number of keys and min/max age of all keys in the cache
    function getStats(cache) {
      var keys = cache.keys(),
        ageMin = new Date().getTime(),
        ageMax = 0;

      keys.forEach(function (key) {
        var info = cache.info(key);
        ageMin = Math.min(ageMin, info.created);
        ageMax = Math.max(ageMax, info.created);
      });

      return {
        keys: keys.length,
        ageMin: ageMin || null,
        ageMax: ageMax || null
      };
    }

    function getStoragePrefix(key, version) {
      return 'angular-cache.caches.' + key + ':' + version + '.';
    }

    function addLocalStorageCache(namespace, cacheId, cacheConfig) {
      var key = buildCacheKey(namespace, cacheId);
      var cacheFactory = cacheConfig.cacheFactory || DSCacheFactory;
      var maxAge = cacheConfig.maxAge || 2 * 24 * 60 * 60 * 1000,
        currentVersion = cacheConfig.version || 1;

      clearPreviousVersions(namespace, cacheId, currentVersion, cacheFactory);

      cacheFactory(key, {
        maxAge: maxAge,
        deleteOnExpire: 'aggressive',
        storageMode: 'localStorage',
        storagePrefix: getStoragePrefix(key, currentVersion),
        recycleFreq: 5000, // ms
      });
      caches[key] = cacheFactory.get(key);
      caches[key].getStats = getStats.bind(null, caches[key]);
      caches[key].config = cacheConfig;
    }

    function clearPreviousVersions(namespace, cacheId, currentVersion, cacheFactory) {
      if (currentVersion) {

        // clear non-versioned cache (TODO: remove after 5/15/15)
        cacheFactory(cacheId, { storageMode: 'localStorage', });
        cacheFactory.get(cacheId).removeAll();
        cacheFactory.get(cacheId).destroy();

        // clear previous versions
        for (var i = 0; i < currentVersion; i++) {
          // non-namespaced (TODO: remove after 5/15/15)
          cacheFactory(cacheId, {
            storageMode: 'localStorage',
            storagePrefix: getStoragePrefix(cacheId, i+1),<commit_msg>clear infrastructure caches when quota exceeded<commit_after>'use strict';

angular.module('spinnaker.caches.core', [
  'angular-data.DSCacheFactory',
])
.factory('deckCacheFactory', function(DSCacheFactory, $log) {

    var caches = Object.create(null);

    function buildCacheKey(namespace, cacheId) {
      if (!namespace || !cacheId) {
        return namespace || cacheId;
      }
      return [namespace, cacheId].join(':');
    }

    function clearCache(namespace, key) {
      if (caches[key] && caches[key].destroy) {
        caches[key].destroy();
        createCache(namespace, key, caches[key].config);
      }
    }

    function clearCaches() {
      Object.keys(caches).forEach(clearCache);
    }

    function createCache(namespace, key, config) {
      addLocalStorageCache(namespace, key, config);
    }

    // Provides number of keys and min/max age of all keys in the cache
    function getStats(cache) {
      var keys = cache.keys(),
        ageMin = new Date().getTime(),
        ageMax = 0;

      keys.forEach(function (key) {
        var info = cache.info(key) || {};
        ageMin = Math.min(ageMin, info.created);
        ageMax = Math.max(ageMax, info.created);
      });

      return {
        keys: keys.length,
        ageMin: ageMin || null,
        ageMax: ageMax || null
      };
    }

    function getStoragePrefix(key, version) {
      return 'angular-cache.caches.' + key + ':' + version + '.';
    }

    var selfClearingLocalStorage = {
      setItem: function(k, v) {
        try {
          window.localStorage.setItem(k, v);
        } catch (e) {
          $log.warn('Local Storage Error! Clearing caches and trying again.\nException:', e);
          for (var key in window.localStorage) {
            // invalidate keystore for any angular caches
            if (key.match(/angular-cache\.caches\.infrastructure:.*.\.keys/)) {
              window.localStorage.setItem(key, '[]');
            }
            // clear the data itself
            if (key.match(/angular-cache\.caches\.infrastructure:.*.\.data\./)) {
              window.localStorage.removeItem(key);
            }
          }
          window.localStorage.setItem(k, v);
        }
      },
      getItem: function(k) { return window.localStorage.getItem(k); },
      removeItem: function(k) { return window.localStorage.removeItem(k); },
    };

    function addLocalStorageCache(namespace, cacheId, cacheConfig) {
      var key = buildCacheKey(namespace, cacheId);
      var cacheFactory = cacheConfig.cacheFactory || DSCacheFactory;
      var maxAge = cacheConfig.maxAge || 2 * 24 * 60 * 60 * 1000,
        currentVersion = cacheConfig.version || 1;

      clearPreviousVersions(namespace, cacheId, currentVersion, cacheFactory);

      cacheFactory(key, {
        maxAge: maxAge,
        deleteOnExpire: 'aggressive',
        storageMode: 'localStorage',
        storagePrefix: getStoragePrefix(key, currentVersion),
        recycleFreq: 5000, // ms,
        storageImpl: selfClearingLocalStorage,
      });
      caches[key] = cacheFactory.get(key);
      caches[key].getStats = getStats.bind(null, caches[key]);
      caches[key].config = cacheConfig;
    }

    function clearPreviousVersions(namespace, cacheId, currentVersion, cacheFactory) {
      if (currentVersion) {

        // clear non-versioned cache (TODO: remove after 5/15/15)
        cacheFactory(cacheId, { storageMode: 'localStorage', });
        cacheFactory.get(cacheId).removeAll();
        cacheFactory.get(cacheId).destroy();

        // clear previous versions
        for (var i = 0; i < currentVersion; i++) {
          // non-namespaced (TODO: remove after 5/15/15)
          cacheFactory(cacheId, {
            storageMode: 'localStorage',
            storagePrefix: getStoragePrefix(cacheId, i+1),
{'fraction_non_alphanumeric': 0.12767897856817145, 'fraction_numerical': 0.008815929472564219, 'mean_word_length': 2.5490830636461705, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3392117', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1886, 'n_words': 413}