Source: cache/KeyValueCacheAdapter.js

const crypto = require('crypto');
const CacheAdapter = require('../cache').CacheAdapter;

/**
 *
 */
class KeyValueCacheAdapter extends CacheAdapter{
  constructor(storage) {
    super();
    this.storage = storage;
  }

  /** @inheritDoc */
  get(publicId, type, resourceType, transformation) {
    let key = KeyValueCacheAdapter.generateCacheKey(publicId, type, resourceType, transformation);
    return KeyValueCacheAdapter.extractData(this.storage.get(key));
  }

  /** @inheritDoc */
  set(publicId, type, resourceType, transformation, value) {
    let key = KeyValueCacheAdapter.generateCacheKey(publicId, type, resourceType, transformation);
    this.storage.set(key, KeyValueCacheAdapter.prepareData(publicId, value));
  }

  /** @inheritDoc */
  flushAll() {
    this.storage.clear();
  }

  static generateCacheKey(publicId, type, resourceType, transformation) {
    let sha1 = crypto.createHash('sha1');
    return sha1.update([resourceType, type, transformation, publicId].join('/')).digest('hex');
  }

  static prepareData(publicId, data) {
    return {public_id: publicId, breakpoints: data};
  }

  static extractData(data) {
    return data ? data.breakpoints : null;
  }
}

module.exports = KeyValueCacheAdapter;