是否可以使用 Parse (parse.com) 的 Javascript SDK 缓存查询?
我找到的唯一引用是 https://parse.com/questions/javascript-sdk-caching并表示当前不支持它。
由于对最后一个链接的最后回复是 4 个月前,我想知道现在是否支持它,或者是否有解决方法来缓存此查询。
最佳答案
通过一些工作,您可以使用包装器方法自行缓存它(或任何东西)。
比如我修改了https://parse.com/docs/js/symbols/Parse.Query.html缓存:
var cacheKeys = [],
cacheVals = [];
function q(cls, ok, fail) { // query wrapper that implements caching
// store or fetch from cache if available:
var slot = cacheKeys.indexOf(cls);
if (slot !== -1) {
return ok(cacheVals[slot]);
} else {
slot=cacheKeys.length;
cacheKeys.push(cls);
}
//normal query code:
var query = new Parse.Query(cls);
query.find({
success: function(results) {
cacheVals[slot] = results; //cache results
ok(results); // call traditionally
},
error: fail || console.error.bind(console)
});
} // end q()
请注意,随着时间的推移,这会积累不需要的缓存条目,因此您可能需要使用 WeakMap 进行重构(如果您可以使用浏览器支持池),自己维护缓存,或者让它每次构建和重新加载不时地。另一个考虑因素是它希望每次都将完全相同的查询对象传递给 q()。您可以尝试使用 JSON.stringify(cls) 作为键,但不能保证结果。尽管如此,由于它只是一个缓存功能,因此由于 JSON key 重新排序而导致的缓存未命中不会破坏任何东西......
关于javascript - 使用 Parse Javascript SDK 缓存查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21744982/