mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-20 19:37:57 +01:00
Refactor cache module to avoid using zip store for smaller items.
This commit is contained in:
@@ -1250,7 +1250,7 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
|
|||||||
["toc", { start: "2" }],
|
["toc", { start: "2" }],
|
||||||
|
|
||||||
body]);
|
body]);
|
||||||
});
|
}, true);
|
||||||
|
|
||||||
cache.register("help/index.xml", function () {
|
cache.register("help/index.xml", function () {
|
||||||
return '<?xml version="1.0"?>\n' +
|
return '<?xml version="1.0"?>\n' +
|
||||||
@@ -1259,7 +1259,7 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
|
|||||||
["dl", { insertafter: name + "-index" },
|
["dl", { insertafter: name + "-index" },
|
||||||
template.map(iter(), util.identity)],
|
template.map(iter(), util.identity)],
|
||||||
"\n\n")]);
|
"\n\n")]);
|
||||||
});
|
}, true);
|
||||||
|
|
||||||
cache.register("help/gui.xml", function () {
|
cache.register("help/gui.xml", function () {
|
||||||
return '<?xml version="1.0"?>\n' +
|
return '<?xml version="1.0"?>\n' +
|
||||||
@@ -1271,7 +1271,7 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
|
|||||||
["dd", {}, val[0]]]
|
["dd", {}, val[0]]]
|
||||||
: undefined,
|
: undefined,
|
||||||
"\n")]]);
|
"\n")]]);
|
||||||
});
|
}, true);
|
||||||
|
|
||||||
cache.register("help/privacy.xml", function () {
|
cache.register("help/privacy.xml", function () {
|
||||||
return '<?xml version="1.0"?>\n' +
|
return '<?xml version="1.0"?>\n' +
|
||||||
@@ -1284,7 +1284,7 @@ var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
|
|||||||
[["dt", {}, name],
|
[["dt", {}, name],
|
||||||
["dd", {}, template.linkifyHelp(description, true)]],
|
["dd", {}, template.linkifyHelp(description, true)]],
|
||||||
"\n")]]);
|
"\n")]]);
|
||||||
});
|
}, true);
|
||||||
},
|
},
|
||||||
events: function initEvents() {
|
events: function initEvents() {
|
||||||
events.listen(window, dactyl, "events", true);
|
events.listen(window, dactyl, "events", true);
|
||||||
|
|||||||
@@ -563,9 +563,10 @@ var Modes = Module("modes", {
|
|||||||
return rec(roots);
|
return rec(roots);
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.register("modes.dtd", () =>
|
cache.register("modes.dtd",
|
||||||
util.makeDTD(iter({ "modes.tree": makeTree() },
|
() => util.makeDTD(iter({ "modes.tree": makeTree() },
|
||||||
config.dtd)));
|
config.dtd)),
|
||||||
|
true);
|
||||||
},
|
},
|
||||||
mappings: function initMappings() {
|
mappings: function initMappings() {
|
||||||
mappings.add([modes.BASE, modes.NORMAL],
|
mappings.add([modes.BASE, modes.NORMAL],
|
||||||
|
|||||||
@@ -10,37 +10,39 @@ defineModule("cache", {
|
|||||||
});
|
});
|
||||||
|
|
||||||
lazyRequire("overlay", ["overlay"]);
|
lazyRequire("overlay", ["overlay"]);
|
||||||
lazyRequire("storage", ["File"]);
|
lazyRequire("storage", ["File", "storage"]);
|
||||||
|
|
||||||
var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
|
var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
|
||||||
init: function init() {
|
init: function init() {
|
||||||
this.queue = [];
|
this.queue = [];
|
||||||
this.cache = {};
|
this.storage = storage.newMap("cache", { store: true });
|
||||||
this.providers = {};
|
this.providers = {};
|
||||||
this.globalProviders = this.providers;
|
this.globalProviders = this.providers;
|
||||||
this.providing = RealSet();
|
this.providing = RealSet();
|
||||||
this.localProviders = {};
|
this.localProviders = RealSet();
|
||||||
|
|
||||||
if (JSMLoader.cacheFlush)
|
if (JSMLoader.cacheFlush)
|
||||||
this.flush();
|
this.flush();
|
||||||
|
|
||||||
update(services["dactyl:"].providers, {
|
update(services["dactyl:"].providers, {
|
||||||
"cache": function (uri, path) {
|
"cache": (uri, path) => {
|
||||||
let contentType = "text/plain";
|
let contentType = "text/plain";
|
||||||
try {
|
try {
|
||||||
contentType = services.mime.getTypeFromURI(uri);
|
contentType = services.mime.getTypeFromURI(uri);
|
||||||
}
|
}
|
||||||
catch (e) {}
|
catch (e) {}
|
||||||
|
|
||||||
if (!cache.cacheReader || !cache.cacheReader.hasEntry(path))
|
if (this.storage.has(path) ||
|
||||||
return [contentType, cache.force(path)];
|
!this.cacheReader ||
|
||||||
|
!this.cacheReader.hasEntry(path))
|
||||||
|
return [contentType, this.force(path)];
|
||||||
|
|
||||||
let channel = services.StreamChannel(uri);
|
let channel = services.StreamChannel(uri);
|
||||||
try {
|
try {
|
||||||
channel.contentStream = cache.cacheReader.getInputStream(path);
|
channel.contentStream = this.cacheReader.getInputStream(path);
|
||||||
}
|
}
|
||||||
catch (e if e.result = Cr.NS_ERROR_FILE_CORRUPTED) {
|
catch (e if e.result = Cr.NS_ERROR_FILE_CORRUPTED) {
|
||||||
cache.flushDiskCache();
|
this.flushDiskCache();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
channel.contentType = contentType;
|
channel.contentType = contentType;
|
||||||
@@ -137,7 +139,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
|
|||||||
}),
|
}),
|
||||||
|
|
||||||
flush: function flush() {
|
flush: function flush() {
|
||||||
cache.cache = {};
|
this.storage.clear();
|
||||||
this.flushDiskCache();
|
this.flushDiskCache();
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -164,7 +166,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
|
|||||||
cache.processQueue();
|
cache.processQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
delete this.cache[name];
|
this.storage.remove(name);
|
||||||
},
|
},
|
||||||
|
|
||||||
flushJAR: function flushJAR(file) {
|
flushJAR: function flushJAR(file) {
|
||||||
@@ -176,6 +178,9 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
|
|||||||
},
|
},
|
||||||
|
|
||||||
force: function force(name, localOnly) {
|
force: function force(name, localOnly) {
|
||||||
|
if (this.storage.has(name))
|
||||||
|
return this.storage.get(name);
|
||||||
|
|
||||||
util.waitFor(() => !this.inQueue);
|
util.waitFor(() => !this.inQueue);
|
||||||
|
|
||||||
if (this.cacheReader && this.cacheReader.hasEntry(name)) {
|
if (this.cacheReader && this.cacheReader.hasEntry(name)) {
|
||||||
@@ -188,7 +193,7 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasOwnProperty(this.localProviders, name) && !this.isLocal) {
|
if (this.localProviders.has(name) && !this.isLocal) {
|
||||||
for each (let { cache } in overlay.modules)
|
for each (let { cache } in overlay.modules)
|
||||||
if (cache._has(name))
|
if (cache._has(name))
|
||||||
return cache.force(name, true);
|
return cache.force(name, true);
|
||||||
@@ -200,49 +205,54 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
|
|||||||
false);
|
false);
|
||||||
this.providing.add(name);
|
this.providing.add(name);
|
||||||
|
|
||||||
|
let [func, long] = this.providers[name];
|
||||||
try {
|
try {
|
||||||
let [func, self] = this.providers[name];
|
var value = func.call(this, name);
|
||||||
this.cache[name] = func.call(self || this, name);
|
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
this.providing.delete(name);
|
this.providing.delete(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.queue.push([Date.now(), name]);
|
if (!long)
|
||||||
cache.processQueue();
|
this.storage.set(name, value);
|
||||||
|
else {
|
||||||
|
cache.queue.push([Date.now(), name, value]);
|
||||||
|
cache.processQueue();
|
||||||
|
}
|
||||||
|
|
||||||
return this.cache[name];
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isLocal && !localOnly)
|
if (this.isLocal && !localOnly)
|
||||||
return cache.force(name);
|
return cache.force(name);
|
||||||
},
|
},
|
||||||
|
|
||||||
get: function get(name, callback, self) {
|
get: function get(name, callback, long) {
|
||||||
if (!hasOwnProperty(this.cache, name)) {
|
if (this.storage.has(name))
|
||||||
if (callback && !(hasOwnProperty(this.providers, name) ||
|
return this.storage.get(name);
|
||||||
hasOwnProperty(this.localProviders, name)))
|
|
||||||
this.register(name, callback, self);
|
|
||||||
|
|
||||||
this.cache[name] = this.force(name);
|
if (callback && !(hasOwnProperty(this.providers, name) ||
|
||||||
util.assert(this.cache[name] !== undefined,
|
this.localProviders.has(name)))
|
||||||
"No such cache key", false);
|
this.register(name, callback, long);
|
||||||
}
|
|
||||||
|
|
||||||
return this.cache[name];
|
var result = this.force(name);
|
||||||
|
util.assert(result !== undefined, "No such cache key", false);
|
||||||
|
|
||||||
|
return result;
|
||||||
},
|
},
|
||||||
|
|
||||||
_has: function _has(name) hasOwnProperty(this.providers, name)
|
_has: function _has(name) hasOwnProperty(this.providers, name)
|
||||||
|| hasOwnProperty(this.cache, name),
|
|| this.storage.has(name),
|
||||||
|
|
||||||
has: function has(name) [this.globalProviders, this.cache, this.localProviders]
|
has: function has(name) [this.globalProviders, this.localProviders]
|
||||||
.some(obj => hasOwnProperty(obj, name)),
|
.some(obj => isinstance(obj, ["Set"]) ? obj.has(name)
|
||||||
|
: hasOwnProperty(obj, name)),
|
||||||
|
|
||||||
register: function register(name, callback, self) {
|
register: function register(name, callback, long) {
|
||||||
if (this.isLocal)
|
if (this.isLocal)
|
||||||
this.localProviders[name] = true;
|
this.localProviders.add(name);
|
||||||
|
|
||||||
this.providers[name] = [callback, self];
|
this.providers[name] = [callback, long];
|
||||||
},
|
},
|
||||||
|
|
||||||
processQueue: function processQueue() {
|
processQueue: function processQueue() {
|
||||||
@@ -257,13 +267,15 @@ var Cache = Module("Cache", XPCOM(Ci.nsIRequestObserver), {
|
|||||||
this.getCacheWriter().removeEntry(entry, false);
|
this.getCacheWriter().removeEntry(entry, false);
|
||||||
removed++;
|
removed++;
|
||||||
}
|
}
|
||||||
if (removed)
|
if (removed) {
|
||||||
this.closeWriter();
|
this.closeWriter();
|
||||||
|
util.flushCache(this.cacheFile);
|
||||||
|
}
|
||||||
|
|
||||||
this.queue.splice(0).forEach(function ([time, entry]) {
|
this.queue.splice(0).forEach(function ([time, entry, value]) {
|
||||||
if (time && hasOwnProperty(this.cache, entry)) {
|
if (time && value != null) {
|
||||||
let stream = services.CharsetConv("UTF-8")
|
let stream = services.CharsetConv("UTF-8")
|
||||||
.convertToInputStream(this.stringify(this.cache[entry]));
|
.convertToInputStream(this.stringify(value));
|
||||||
|
|
||||||
this.getCacheWriter().addEntryStream(entry, time * 1000,
|
this.getCacheWriter().addEntryStream(entry, time * 1000,
|
||||||
this.compression, stream,
|
this.compression, stream,
|
||||||
|
|||||||
@@ -60,7 +60,8 @@ var ConfigBase = Class("ConfigBase", {
|
|||||||
|
|
||||||
this.protocolLoaded = true;
|
this.protocolLoaded = true;
|
||||||
this.timeout(function () {
|
this.timeout(function () {
|
||||||
cache.register("config.dtd", () => util.makeDTD(config.dtd));
|
cache.register("config.dtd", () => util.makeDTD(config.dtd),
|
||||||
|
true);
|
||||||
});
|
});
|
||||||
|
|
||||||
services["dactyl:"].pages["dtd"] = () => [null, cache.get("config.dtd")];
|
services["dactyl:"].pages["dtd"] = () => [null, cache.get("config.dtd")];
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ var Help = Module("Help", {
|
|||||||
["toc", { start: "2" }],
|
["toc", { start: "2" }],
|
||||||
|
|
||||||
body]);
|
body]);
|
||||||
});
|
}, true);
|
||||||
},
|
},
|
||||||
|
|
||||||
initialize: function initialize() {
|
initialize: function initialize() {
|
||||||
|
|||||||
@@ -826,17 +826,18 @@ var Options = Module("options", {
|
|||||||
opt.set(opt.globalValue, Option.SCOPE_GLOBAL, true);
|
opt.set(opt.globalValue, Option.SCOPE_GLOBAL, true);
|
||||||
}, window);
|
}, window);
|
||||||
|
|
||||||
modules.cache.register("options.dtd", () =>
|
modules.cache.register("options.dtd",
|
||||||
util.makeDTD(
|
() => util.makeDTD(
|
||||||
iter(([["option", o.name, "default"].join("."),
|
iter(([["option", o.name, "default"].join("."),
|
||||||
o.type === "string" ? o.defaultValue.replace(/'/g, "''") :
|
o.type === "string" ? o.defaultValue.replace(/'/g, "''") :
|
||||||
o.defaultValue === true ? "on" :
|
o.defaultValue === true ? "on" :
|
||||||
o.defaultValue === false ? "off" : o.stringDefaultValue]
|
o.defaultValue === false ? "off" : o.stringDefaultValue]
|
||||||
for (o in self)),
|
for (o in self)),
|
||||||
|
|
||||||
([["option", o.name, "type"].join("."), o.type] for (o in self)),
|
([["option", o.name, "type"].join("."), o.type] for (o in self)),
|
||||||
|
|
||||||
config.dtd)));
|
config.dtd)),
|
||||||
|
true);
|
||||||
},
|
},
|
||||||
|
|
||||||
signals: {
|
signals: {
|
||||||
|
|||||||
@@ -136,11 +136,13 @@ var ObjectStore = Class("ObjectStore", StoreBase, {
|
|||||||
},
|
},
|
||||||
|
|
||||||
get: function get(key, default_) {
|
get: function get(key, default_) {
|
||||||
return key in this._object ? this._object[key] :
|
return this.has(key) ? this._object[key] :
|
||||||
arguments.length > 1 ? this.set(key, default_) :
|
arguments.length > 1 ? this.set(key, default_) :
|
||||||
undefined;
|
undefined;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
has: function has(key) hasOwnProperty(this._object, key),
|
||||||
|
|
||||||
keys: function keys() Object.keys(this._object),
|
keys: function keys() Object.keys(this._object),
|
||||||
|
|
||||||
remove: function remove(key) {
|
remove: function remove(key) {
|
||||||
@@ -240,7 +242,7 @@ var Storage = Module("Storage", {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
newObject: function newObject(key, constructor, params) {
|
newObject: function newObject(key, constructor, params={}) {
|
||||||
if (params == null || !isObject(params))
|
if (params == null || !isObject(params))
|
||||||
throw Error("Invalid argument type");
|
throw Error("Invalid argument type");
|
||||||
|
|
||||||
@@ -269,11 +271,11 @@ var Storage = Module("Storage", {
|
|||||||
return this.keys[key];
|
return this.keys[key];
|
||||||
},
|
},
|
||||||
|
|
||||||
newMap: function newMap(key, options) {
|
newMap: function newMap(key, options={}) {
|
||||||
return this.newObject(key, ObjectStore, options);
|
return this.newObject(key, ObjectStore, options);
|
||||||
},
|
},
|
||||||
|
|
||||||
newArray: function newArray(key, options) {
|
newArray: function newArray(key, options={}) {
|
||||||
return this.newObject(key, ArrayStore, update({ type: Array }, options));
|
return this.newObject(key, ArrayStore, update({ type: Array }, options));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user