Use indexeddb cache for blobs

pull/1/head
yflory 4 years ago
parent 5946b10d0b
commit 11abd6170b

@ -1,8 +1,5 @@
(function(name, definition) { (function (window) {
if (typeof module !== 'undefined') { module.exports = definition(); } var factory = function (Cache) {
else if (typeof define === 'function' && typeof define.amd === 'object') { define(definition); }
else { this[name] = definition(); }
}('MediaTag', function() {
var cache; var cache;
var cypherChunkLength = 131088; var cypherChunkLength = 131088;
@ -133,6 +130,12 @@
cb = function () {}; cb = function () {};
}; };
var _src = src.replace(/(\/)*$/, ''); // Remove trailing slashes
var idx = _src.lastIndexOf('/');
var cacheKey = _src.slice(idx+1);
if (!/^[a-f0-9]{48}$/.test(cacheKey)) { cacheKey = undefined; }
var fetch = function () {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open('GET', src, true); xhr.open('GET', src, true);
xhr.responseType = 'arraybuffer'; xhr.responseType = 'arraybuffer';
@ -143,12 +146,30 @@
if (/^4/.test('' + this.status)) { return void cb("XHR_ERROR " + this.status); } if (/^4/.test('' + this.status)) { return void cb("XHR_ERROR " + this.status); }
var arrayBuffer = xhr.response; var arrayBuffer = xhr.response;
if (arrayBuffer) { cb(null, new Uint8Array(arrayBuffer)); } if (arrayBuffer) {
var u8 = new Uint8Array(arrayBuffer);
if (cacheKey) {
return void Cache.setBlobCache(cacheKey, u8, function (err) {
cb(null, u8);
});
}
cb(null, u8);
}
}; };
xhr.send(null); xhr.send(null);
}; };
if (!cacheKey) { return void fetch(); }
Cache.getBlobCache(cacheKey, function (err, u8) {
if (err || !u8) { return void fetch(); }
console.error('using cache', cacheKey);
cb(null, u8);
});
};
// Decryption tools // Decryption tools
var Decrypt = { var Decrypt = {
// Create a nonce // Create a nonce
@ -469,4 +490,19 @@
}; };
return init; return init;
})); };
if (typeof(module) !== 'undefined' && module.exports) {
module.exports = factory(
require("./outer/cache-store.js")
);
} else if ((typeof(define) !== 'undefined' && define !== null) && (define.amd !== null)) {
define([
'/common/outer/cache-store.js',
], function (Cache) {
return factory(Cache);
});
} else {
// unsupported initialization
}
}(typeof(window) !== 'undefined'? window : {}));

@ -8,6 +8,28 @@ define([
name: "cp_cache" name: "cp_cache"
}); });
S.getBlobCache = function (id, cb) {
cb = Util.once(Util.mkAsync(cb || function () {}));
cache.getItem(id, function (err, obj) {
if (err || !obj || !obj.c) {
return void cb(err || 'EINVAL');
}
cb(null, obj.c);
obj.t = +new Date();
cache.setItem(id, obj);
});
};
S.setBlobCache = function (id, u8, cb) {
cb = Util.once(Util.mkAsync(cb || function () {}));
if (!u8) { return void cb('EINVAL'); }
cache.setItem(id, {
c: u8,
t: (+new Date()) // 't' represent the "lastAccess" of this cache (get or set)
}, function (err) {
cb(err);
});
};
// id: channel ID or blob ID // id: channel ID or blob ID
// returns array of messages // returns array of messages
S.getChannelCache = function (id, cb) { S.getChannelCache = function (id, cb) {

Loading…
Cancel
Save