UNPKG

4.91 kBJavaScriptView Raw
1"use strict";
2
3var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
4
5var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
6
7function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
9/* eslint-disable class-methods-use-this */
10
11var CACHE = Symbol.for("com.cloudinary.cache");
12var CACHE_ADAPTER = Symbol.for("com.cloudinary.cacheAdapter");
13
14var _require = require('./utils'),
15 ensurePresenceOf = _require.ensurePresenceOf,
16 generate_transformation_string = _require.generate_transformation_string;
17
18/**
19 * The adapter used to communicate with the underlying cache storage
20 */
21
22
23var CacheAdapter = function () {
24 function CacheAdapter() {
25 _classCallCheck(this, CacheAdapter);
26 }
27
28 _createClass(CacheAdapter, [{
29 key: "get",
30
31 /**
32 * Get a value from the cache
33 * @param {string} publicId
34 * @param {string} type
35 * @param {string} resourceType
36 * @param {string} transformation
37 * @param {string} format
38 * @return {*} the value associated with the provided arguments
39 */
40 value: function get(publicId, type, resourceType, transformation, format) {}
41
42 /**
43 * Set a new value in the cache
44 * @param {string} publicId
45 * @param {string} type
46 * @param {string} resourceType
47 * @param {string} transformation
48 * @param {string} format
49 * @param {*} value
50 */
51
52 }, {
53 key: "set",
54 value: function set(publicId, type, resourceType, transformation, format, value) {}
55
56 /**
57 * Delete all values in the cache
58 */
59
60 }, {
61 key: "flushAll",
62 value: function flushAll() {}
63 }]);
64
65 return CacheAdapter;
66}();
67/**
68 * @class Cache
69 * Stores and retrieves values identified by publicId / options pairs
70 */
71
72
73var Cache = {
74 /**
75 * The adapter interface. Extend this class to implement a specific adapter.
76 * @type CacheAdapter
77 */
78 CacheAdapter,
79 /**
80 * Set the cache adapter
81 * @param {CacheAdapter} adapter The cache adapter
82 */
83 setAdapter(adapter) {
84 if (this.adapter) {
85 console.warn("Overriding existing cache adapter");
86 }
87 this.adapter = adapter;
88 },
89 /**
90 * Get the adapter the Cache is using
91 * @return {CacheAdapter} the current cache adapter
92 */
93 getAdapter() {
94 return this.adapter;
95 },
96 /**
97 * Get an item from the cache
98 * @param {string} publicId
99 * @param {object} options
100 * @return {*}
101 */
102 get(publicId, options) {
103 if (!this.adapter) {
104 return undefined;
105 }
106 ensurePresenceOf({ publicId });
107 var transformation = generate_transformation_string(_extends({}, options));
108 return this.adapter.get(publicId, options.type || 'upload', options.resource_type || 'image', transformation, options.format);
109 },
110 /**
111 * Set a new value in the cache
112 * @param {string} publicId
113 * @param {object} options
114 * @param {*} value
115 * @return {*}
116 */
117 set(publicId, options, value) {
118 if (!this.adapter) {
119 return undefined;
120 }
121 ensurePresenceOf({ publicId, value });
122 var transformation = generate_transformation_string(_extends({}, options));
123 return this.adapter.set(publicId, options.type || 'upload', options.resource_type || 'image', transformation, options.format, value);
124 },
125 /**
126 * Clear all items in the cache
127 * @return {*} Returns the value from the adapter's flushAll() method
128 */
129 flushAll() {
130 if (!this.adapter) {
131 return undefined;
132 }
133 return this.adapter.flushAll();
134 }
135
136};
137
138// Define singleton property
139Object.defineProperty(Cache, "instance", {
140 get() {
141 return global[CACHE];
142 }
143});
144Object.defineProperty(Cache, "adapter", {
145 /**
146 *
147 * @return {CacheAdapter} The current cache adapter
148 */
149 get() {
150 return global[CACHE_ADAPTER];
151 },
152 /**
153 * Set the cache adapter to be used by Cache
154 * @param {CacheAdapter} adapter Cache adapter
155 */
156 set(adapter) {
157 global[CACHE_ADAPTER] = adapter;
158 }
159});
160Object.freeze(Cache);
161
162// Instantiate the singleton
163var symbols = Object.getOwnPropertySymbols(global);
164if (symbols.indexOf(CACHE) < 0) {
165 global[CACHE] = Cache;
166}
167
168/**
169 * Store key value pairs
170
171 */
172module.exports = Cache;
\No newline at end of file