DataCache - Simple storage wrapper

Allows JSON data to be persisted easily by user scripts

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/10443/56962/DataCache%20-%20Simple%20storage%20wrapper.js

function DataCache(key) {
  this._key = key;
  this._data = {};
  this.load();
}
 
DataCache.prototype = {
  getValue: function(k) {
    return this._data[k];
  },
  setValue: function(k, v) {
    this._data[k] = v;
    this.save();
  },
  deleteValue: function(k) {
    if (k in this._data) {
      delete this._data[k];
      this.save();
    }
  },
  hasValue: function(k) {
    return k in this._data;
  },
  listValues: function() {
    return Object.keys(this._data).sort();
  },
  clear: function() {
    this._data = {};
    this.save();
  },
  save: function() {
    var s = JSON.stringify(this._data);
    GM_setValue(this._key, s);
    console.info('Cache(' + this._key + ') saved: ' + s);
  },
  load: function(s) {
    try {
      this._data = JSON.parse(s || GM_getValue(this._key));
    }
    catch (ex) {
      this.clear();
    }
  },
  edit: function() {
    var res = window.prompt('Edit cached package URLs', JSON.stringify(this._data, null, 2));
    if (res !== null) {
      try {
        this._data = res ? JSON.parse(res) : {};
        this.save();
      }
      catch (ex) {
        console.warn('Failed to update cache data: %s %o', ex.toString(), ex);
      }
    }
  },
  toString: function() {
    return 'Cache(' + this._key + '): [' + this.listValues.join('\n') + ']';
  },
  dump: function() {
    console.log('Cache(' + this._key + '):\n' + JSON.stringify(this._data, null, 2));
  }
};