1 |
|
2 | const CACHE = Symbol.for("com.cloudinary.cache");
|
3 | const CACHE_ADAPTER = Symbol.for("com.cloudinary.cacheAdapter");
|
4 | const utils = require('./utils');
|
5 | const {ensurePresenceOf} = utils;
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | class CacheAdapter {
|
11 | constructor(storage) {}
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | get(publicId, type, resourceType, transformation, format) {}
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | set(publicId, type, resourceType, transformation, format, value) {}
|
32 |
|
33 | |
34 |
|
35 |
|
36 | flushAll() {}
|
37 |
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | const Cache = {
|
44 | |
45 |
|
46 |
|
47 |
|
48 | CacheAdapter,
|
49 | |
50 |
|
51 |
|
52 |
|
53 | setAdapter(adapter) {
|
54 | if(this.adapter){
|
55 | console.warn("Overriding existing cache adapter");
|
56 | }
|
57 | this.adapter = adapter;
|
58 | },
|
59 | |
60 |
|
61 |
|
62 |
|
63 | getAdapter() {
|
64 | return this.adapter;
|
65 | },
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
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 |
|
85 |
|
86 |
|
87 |
|
88 |
|
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 |
|
105 |
|
106 |
|
107 | flushAll() {
|
108 | if(!this.adapter) {return undefined;}
|
109 | return this.adapter.flushAll();
|
110 | }
|
111 |
|
112 | };
|
113 |
|
114 |
|
115 | Object.defineProperty(Cache, "instance", {
|
116 | get() {
|
117 | return global[CACHE];
|
118 | }
|
119 | });
|
120 | Object.defineProperty(Cache, "adapter", {
|
121 | |
122 |
|
123 |
|
124 |
|
125 | get() {
|
126 | return global[CACHE_ADAPTER];
|
127 | },
|
128 | |
129 |
|
130 |
|
131 |
|
132 | set(adapter) {
|
133 | global[CACHE_ADAPTER] = adapter;
|
134 | }
|
135 | });
|
136 | Object.freeze(Cache);
|
137 |
|
138 |
|
139 | let symbols = Object.getOwnPropertySymbols(global);
|
140 | if(symbols.indexOf(CACHE) < 0) {
|
141 | global[CACHE] = Cache;
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | module.exports = Cache;
|
149 |
|