CacheController.js 8.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.SubCache = exports.CacheController = void 0;
  6. var _AdaptableController = _interopRequireDefault(require("./AdaptableController"));
  7. var _CacheAdapter = _interopRequireDefault(require("../Adapters/Cache/CacheAdapter"));
  8. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  9. const KEY_SEPARATOR_CHAR = ':';
  10. function joinKeys(...keys) {
  11. return keys.join(KEY_SEPARATOR_CHAR);
  12. }
  13. /**
  14. * Prefix all calls to the cache via a prefix string, useful when grouping Cache by object type.
  15. *
  16. * eg "Role" or "Session"
  17. */
  18. class SubCache {
  19. constructor(prefix, cacheController, ttl) {
  20. this.prefix = prefix;
  21. this.cache = cacheController;
  22. this.ttl = ttl;
  23. }
  24. get(key) {
  25. const cacheKey = joinKeys(this.prefix, key);
  26. return this.cache.get(cacheKey);
  27. }
  28. put(key, value, ttl) {
  29. const cacheKey = joinKeys(this.prefix, key);
  30. return this.cache.put(cacheKey, value, ttl);
  31. }
  32. del(key) {
  33. const cacheKey = joinKeys(this.prefix, key);
  34. return this.cache.del(cacheKey);
  35. }
  36. clear() {
  37. return this.cache.clear();
  38. }
  39. }
  40. exports.SubCache = SubCache;
  41. class CacheController extends _AdaptableController.default {
  42. constructor(adapter, appId, options = {}) {
  43. super(adapter, appId, options);
  44. this.role = new SubCache('role', this);
  45. this.user = new SubCache('user', this);
  46. this.graphQL = new SubCache('graphQL', this);
  47. }
  48. get(key) {
  49. const cacheKey = joinKeys(this.appId, key);
  50. return this.adapter.get(cacheKey).then(null, () => Promise.resolve(null));
  51. }
  52. put(key, value, ttl) {
  53. const cacheKey = joinKeys(this.appId, key);
  54. return this.adapter.put(cacheKey, value, ttl);
  55. }
  56. del(key) {
  57. const cacheKey = joinKeys(this.appId, key);
  58. return this.adapter.del(cacheKey);
  59. }
  60. clear() {
  61. return this.adapter.clear();
  62. }
  63. expectedAdapterType() {
  64. return _CacheAdapter.default;
  65. }
  66. }
  67. exports.CacheController = CacheController;
  68. var _default = exports.default = CacheController;
  69. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfQWRhcHRhYmxlQ29udHJvbGxlciIsIl9pbnRlcm9wUmVxdWlyZURlZmF1bHQiLCJyZXF1aXJlIiwiX0NhY2hlQWRhcHRlciIsImUiLCJfX2VzTW9kdWxlIiwiZGVmYXVsdCIsIktFWV9TRVBBUkFUT1JfQ0hBUiIsImpvaW5LZXlzIiwia2V5cyIsImpvaW4iLCJTdWJDYWNoZSIsImNvbnN0cnVjdG9yIiwicHJlZml4IiwiY2FjaGVDb250cm9sbGVyIiwidHRsIiwiY2FjaGUiLCJnZXQiLCJrZXkiLCJjYWNoZUtleSIsInB1dCIsInZhbHVlIiwiZGVsIiwiY2xlYXIiLCJleHBvcnRzIiwiQ2FjaGVDb250cm9sbGVyIiwiQWRhcHRhYmxlQ29udHJvbGxlciIsImFkYXB0ZXIiLCJhcHBJZCIsIm9wdGlvbnMiLCJyb2xlIiwidXNlciIsImdyYXBoUUwiLCJ0aGVuIiwiUHJvbWlzZSIsInJlc29sdmUiLCJleHBlY3RlZEFkYXB0ZXJUeXBlIiwiQ2FjaGVBZGFwdGVyIiwiX2RlZmF1bHQiXSwic291cmNlcyI6WyIuLi8uLi9zcmMvQ29udHJvbGxlcnMvQ2FjaGVDb250cm9sbGVyLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCBBZGFwdGFibGVDb250cm9sbGVyIGZyb20gJy4vQWRhcHRhYmxlQ29udHJvbGxlcic7XG5pbXBvcnQgQ2FjaGVBZGFwdGVyIGZyb20gJy4uL0FkYXB0ZXJzL0NhY2hlL0NhY2hlQWRhcHRlcic7XG5cbmNvbnN0IEtFWV9TRVBBUkFUT1JfQ0hBUiA9ICc6JztcblxuZnVuY3Rp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