MDL-64835 JS: Reduce cache invalidation checks
The cache invalidation check was previously occuring every time either the `set` or `get` function was called on the cache. However, the cache invalidation check is based on the jsrev which is static for the lifetime of the page. This change moved the invalidation to happen during the setup of the AMD module such that it only happens one time per storage type (Local + Session).
This commit is contained in:
Vendored
+1
-1
@@ -1 +1 @@
|
||||
define(["core/config"],function(a){var b=function(b){this.storage=b,this.supported=this.detectSupport(),this.hashSource=a.wwwroot+"/"+a.jsrev,this.hash=this.hashString(this.hashSource),this.prefix=this.hash+"/",this.jsrevPrefix=this.hashString(a.wwwroot)+"/jsrev"};return b.prototype.detectSupport=function(){if(a.jsrev==-1)return!1;if("undefined"==typeof this.storage)return!1;var b="test";try{return null!==this.storage&&(this.storage.setItem(b,"1"),this.storage.removeItem(b),!0)}catch(c){return!1}},b.prototype.prefixKey=function(a){return this.prefix+a},b.prototype.validateCache=function(){var b=this.storage.getItem(this.jsrevPrefix);if(null===b)return void this.storage.setItem(this.jsrevPrefix,a.jsrev);var c=a.jsrev;c!=b&&(this.storage.clear(),this.storage.setItem(this.jsrevPrefix,a.jsrev))},b.prototype.hashString=function(a){var b,c,d,e=0;if(0===a.length)return e;for(b=0,d=a.length;b<d;b++)c=a.charCodeAt(b),e=(e<<5)-e+c,e|=0;return e},b.prototype.get=function(a){return!!this.supported&&(this.validateCache(),a=this.prefixKey(a),this.storage.getItem(a))},b.prototype.set=function(a,b){if(!this.supported)return!1;this.validateCache(),a=this.prefixKey(a);try{this.storage.setItem(a,b)}catch(c){return!1}return!0},b});
|
||||
define(["core/config"],function(a){var b=function(b){this.storage=b,this.supported=this.detectSupport(),this.hashSource=a.wwwroot+"/"+a.jsrev,this.hash=this.hashString(this.hashSource),this.prefix=this.hash+"/",this.jsrevPrefix=this.hashString(a.wwwroot)+"/jsrev",this.validateCache()};return b.prototype.detectSupport=function(){if(a.jsrev==-1)return!1;if("undefined"==typeof this.storage)return!1;var b="test";try{return null!==this.storage&&(this.storage.setItem(b,"1"),this.storage.removeItem(b),!0)}catch(c){return!1}},b.prototype.prefixKey=function(a){return this.prefix+a},b.prototype.validateCache=function(){var b=this.storage.getItem(this.jsrevPrefix);if(null===b)return void this.storage.setItem(this.jsrevPrefix,a.jsrev);var c=a.jsrev;c!=b&&(this.storage.clear(),this.storage.setItem(this.jsrevPrefix,a.jsrev))},b.prototype.hashString=function(a){var b,c,d,e=0;if(0===a.length)return e;for(b=0,d=a.length;b<d;b++)c=a.charCodeAt(b),e=(e<<5)-e+c,e|=0;return e},b.prototype.get=function(a){return!!this.supported&&(a=this.prefixKey(a),this.storage.getItem(a))},b.prototype.set=function(a,b){if(!this.supported)return!1;a=this.prefixKey(a);try{this.storage.setItem(a,b)}catch(c){return!1}return!0},b});
|
||||
@@ -36,6 +36,7 @@ define(['core/config'], function(config) {
|
||||
this.hash = this.hashString(this.hashSource);
|
||||
this.prefix = this.hash + '/';
|
||||
this.jsrevPrefix = this.hashString(config.wwwroot) + '/jsrev';
|
||||
this.validateCache();
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -89,8 +90,8 @@ define(['core/config'], function(config) {
|
||||
this.storage.setItem(this.jsrevPrefix, config.jsrev);
|
||||
return;
|
||||
}
|
||||
var moodleVersion = config.jsrev;
|
||||
|
||||
var moodleVersion = config.jsrev;
|
||||
if (moodleVersion != cacheVersion) {
|
||||
this.storage.clear();
|
||||
this.storage.setItem(this.jsrevPrefix, config.jsrev);
|
||||
@@ -132,7 +133,6 @@ define(['core/config'], function(config) {
|
||||
if (!this.supported) {
|
||||
return false;
|
||||
}
|
||||
this.validateCache();
|
||||
key = this.prefixKey(key);
|
||||
|
||||
return this.storage.getItem(key);
|
||||
@@ -150,7 +150,6 @@ define(['core/config'], function(config) {
|
||||
if (!this.supported) {
|
||||
return false;
|
||||
}
|
||||
this.validateCache();
|
||||
key = this.prefixKey(key);
|
||||
// This can throw exceptions when the storage limit is reached.
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user