UNPKG

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