UNPKG

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