1 | LargeLocalStorage.contrib.URLCache = (function() { |
2 | var defaultOptions = { |
3 | manageRevocation: true |
4 | }; |
5 | |
6 | function defaults(options, defaultOptions) { |
7 | for (var k in defaultOptions) { |
8 | if (options[k] === undefined) |
9 | options[k] = defaultOptions[k]; |
10 | } |
11 | |
12 | return options; |
13 | } |
14 | |
15 | function add(docKey, attachKey, url) { |
16 | if (this.options.manageRevocation) |
17 | expunge.call(this, docKey, attachKey, true); |
18 | |
19 | var mainCache = this.cache.main; |
20 | var docCache = mainCache[docKey]; |
21 | if (!docCache) { |
22 | docCache = {}; |
23 | mainCache[docKey] = docCache; |
24 | } |
25 | |
26 | docCache[attachKey] = url; |
27 | this.cache.reverse[url] = {docKey: docKey, attachKey: attachKey}; |
28 | } |
29 | |
30 | function addAll(urlEntries) { |
31 | urlEntries.forEach(function(entry) { |
32 | add.call(this, entry.docKey, entry.attachKey, entry.url); |
33 | }, this); |
34 | } |
35 | |
36 | function expunge(docKey, attachKey, needsRevoke) { |
37 | function delAndRevoke(attachKey) { |
38 | var url = docCache[attachKey]; |
39 | delete docCache[attachKey]; |
40 | delete this.cache.reverse[url]; |
41 | if (this.options.manageRevocation && needsRevoke) |
42 | this.llshandler.revokeAttachmentURL(url, {bypassUrlCache: true}); |
43 | } |
44 | |
45 | var docCache = this.cache.main[docKey]; |
46 | if (docCache) { |
47 | if (attachKey) { |
48 | delAndRevoke.call(this, attachKey); |
49 | } else { |
50 | for (var attachKey in docCache) { |
51 | delAndRevoke.call(this, attachKey); |
52 | } |
53 | delete this.cache.main[docKey]; |
54 | } |
55 | } |
56 | } |
57 | |
58 | function expungeByUrl(url) { |
59 | var keys = this.cache.reverse[url]; |
60 | if (keys) { |
61 | expunge.call(this, keys.docKey, keys.attachKey, false); |
62 | } |
63 | } |
64 | |
65 | |
66 | function URLCache(llspipe, options) { |
67 | options = options || {}; |
68 | this.options = defaults(options, defaultOptions); |
69 | this.llshandler = llspipe.pipe.getHandler('lls'); |
70 | this.pending = {}; |
71 | this.cache = { |
72 | main: {}, |
73 | reverse: {} |
74 | }; |
75 | } |
76 | |
77 | URLCache.prototype = { |
78 | setAttachment: function(docKey, attachKey, blob) { |
79 | expunge.call(this, docKey, attachKey); |
80 | return this.__pipectx.next(docKey, attachKey, blob); |
81 | }, |
82 | |
83 | rmAttachment: function(docKey, attachKey) { |
84 | expunge.call(this, docKey, attachKey); |
85 | return this.__pipectx.next(docKey, attachKey); |
86 | }, |
87 | |
88 | rm: function(docKey) { |
89 | expunge.call(this, docKey); |
90 | return this.__pipectx.next(docKey); |
91 | }, |
92 | |
93 | revokeAttachmentURL: function(url, options) { |
94 | if (!options || !options.bypassUrlCache) |
95 | expungeByUrl.call(this, url); |
96 | |
97 | return this.__pipectx.next(url, options); |
98 | }, |
99 | |
100 | getAttachmentURL: function(docKey, attachKey) { |
101 | var pendingKey = docKey + attachKey; |
102 | var pending = this.pending[pendingKey]; |
103 | if (pending) |
104 | return pending; |
105 | |
106 | var promise = this.__pipectx.next(docKey, attachKey); |
107 | var self = this; |
108 | promise.then(function(url) { |
109 | add.call(self, docKey, attachKey, url); |
110 | delete self.pending[pendingKey]; |
111 | }); |
112 | |
113 | this.pending[pendingKey] = promise; |
114 | |
115 | return promise; |
116 | }, |
117 | |
118 | // TODO: pending between this and getAttachmentURL... |
119 | // Execute this as an ls and then |
120 | // a loop on getAttachmentURL instead??? |
121 | // doing it the way mentiond above |
122 | // will prevent us from leaking blobs. |
123 | getAllAttachmentURLs: function(docKey) { |
124 | var promise = this.__pipectx.next(docKey); |
125 | var self = this; |
126 | promise.then(function(urlEntries) { |
127 | addAll.call(self, urlEntries); |
128 | }); |
129 | |
130 | return promise; |
131 | }, |
132 | |
133 | clear: function() { |
134 | this.revokeAllCachedURLs(); |
135 | return this.__pipectx.next(); |
136 | }, |
137 | |
138 | revokeAllCachedURLs: function() { |
139 | for (var url in this.cache.reverse) { |
140 | this.llshandler.revokeAttachmentURL(url, {bypassUrlCache: true}); |
141 | } |
142 | |
143 | this.cache.reverse = {}; |
144 | this.cache.main = {}; |
145 | } |
146 | }; |
147 | |
148 | return { |
149 | addTo: function(lls, options) { |
150 | var cache = new URLCache(lls, options); |
151 | lls.pipe.addFirst('URLCache', cache); |
152 | return lls; |
153 | } |
154 | } |
155 | })(); |
From https://github.com/tantaman/LargeLocalStorage/blob/master/dist/contrib/URLCache.js
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1018988 |
Snippet name: | URLCache.js |
Eternal ID of this version: | #1018988/1 |
Text MD5: | 1fe57f11a79d0f847d1f057095722067 |
Author: | stefan |
Category: | javax / web |
Type: | Document |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-10-19 16:45:07 |
Source code size: | 3861 bytes / 155 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 313 / 91 |
Referenced in: | [show references] |