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