- Code:
name: source caching
created: 17:10 - 01/22/2016
updated: 17:10 - 01/22/2016
Giới thiệu:
Khi bạn sử dụng hệ thống lưu tải tệp này, trang của bạn chỉ tải dữ liệu lần đầu, những lần gọi sau điều được lấy từ bộ nhớ đệm, tiết kiệm băng thông, thời gian tải trang và dữ liệu tối đa.
Sử dụng mã javascript tuần nên không cần chạy nền jQuery.
Chỉ hỗ trợ trình duyệt có HTML5.
Lưu ý:
- Tùy vào việc cập nhật tệp nguồn, hãy tùy chỉnh thời gian nhớ đúng với lịch cập nhật của bạn.
- Đối với việc sử dụng trong diễn đàn, bạn các quản trị nên tạo mới (refresh) mỗi lần thành viên đăng nhập hoặc lần truy cập đầu tiên.
- Hạn chế việc sử dụng cho tệp có tính thay đổi cao, có hệ khắc phục bằng việc giảm thời gian nhớ.
Kịch bản:
- Code:
/*
name: source caching - lamhieu (@lh)
created: 17:10 - 01/22/2016
updated: 17:10 - 01/22/2016
*/
var _lh = {};
_lh.sourcecache = {
key: 'lamhieu',
request: {
create: function () {
var objectXHR = false;
if (window.XMLHttpRequest) {
var objectXHR = new XMLHttpRequest();
}else if (window.ActiveXObject) {
try {
var objectXHR = new ActiveXObject("Msxml2.XMLHTTP.6.0");
} catch (e) {
try {
var objectXHR = new ActiveXObject("Msxml2.XMLHTTP.3.0");
} catch (e) {
//.
}
}
}
return objectXHR;
},
get: function (url, callback) {
var xhr = _lh.sourcecache.request.create();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
callback({'return': true, 'status': xhr.status, 'response': (xhr.responseText ? xhr.responseText : null)});
}
};
xhr.open("GET", url, true);
xhr.send();
return true;
}
},
exec: function (type, content, callback) {
var type = typeof type == 'string' ? type : null,
content = typeof content == 'string' ? content : null,
callback = typeof callback == 'function' ? callback : function () {};
if (type == 'css' || type == 'stylesheet') {
var cssTag = document.createElement('style');
cssTag.type = 'text/css';
if (cssTag.styleSheet) {
cssTag.styleSheet.cssText = content;
}else {
cssTag.appendChild(document.createTextNode(content));
}
document.getElementsByTagName("head")[0].appendChild(csscssTag);
callback(true);
return true;
}else if (type == 'js' || type == 'javascript') {
eval(content);
callback(true);
return true;
}
},
load: function (type, url, timeout, callback) {
var type = typeof type == 'string' ? type : nullm
url = typeof url == 'string' ? url : null,
timeout = typeof timeout == 'number' ? timeout : null,
callback = typeof callback == 'function' ? callback : function () {};
if (type == null || url == null || timeout == null) {
callback(false);
return false;
}
var timeNow = Math.floor(Date.now() / 1000);
var cachedListObj = _lh.sourcecache.cache.each();
cachedListObj['sourcecache'] = typeof cachedListObj['sourcecache'] == 'object' ? cachedListObj['sourcecache'] : {};
if (typeof cachedListObj['sourcecache'][url] == 'object') {
var sourceObj = cachedListObj['sourcecache'][url];
var sourceType = typeof sourceObj['type'] == 'string' ? sourceObj['type'] : null,
sourceContent = typeof sourceObj['content'] == 'string' ? sourceObj['content'] : null,
sourceExpired = typeof sourceObj['added'] == 'number' && typeof sourceObj['timeout'] == 'number' ? sourceObj['added'] + sourceObj['timeout'] : 0;
if (sourceType == type) {
if (sourceExpired >= timeNow) {
var isExce = _lh.sourcecache.exec(type, sourceContent);
callback({'return': true, 'loaded': isExce, 'content': sourceContent, 'added': false, 'cached': true, 'timeleft': ((timeNow + timeout) - sourceExpired)});
return true;
}
}
}
return _lh.sourcecache.request.get(url, function (returnRequest) {
if (typeof returnRequest == 'object') {
if (typeof returnRequest['return'] == 'boolean' && returnRequest['return'] == true && typeof returnRequest['response'] == 'string') {
var sourceContent = returnRequest['response'];
var isExce = _lh.sourcecache.exec(type, sourceContent);
var isAdded = _lh.sourcecache.cache.push({'url': url, 'type': type, 'content': sourceContent, 'timeout': timeout});
callback({'return': true, 'loaded': isExce, 'content': sourceContent, 'added': isAdded, 'cached': false, 'timeleft': timeout});
}
}
});
},
checkif: {
json: function (str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
},
supported: function () {
if (typeof localStorage == 'object') {
return true;
}else {
return false;
}
},
},
storage: {
write: function (data, callback) {
var data = typeof data == 'object' ? data : {},
callback = typeof callback == 'function' ? callback : function () {};
data['sourcecache'] = typeof data['sourcecache'] == 'object' ? data['sourcecache'] : {};
if (typeof localStorage != 'object') {
callback(false);
return false;
}
localStorage[_lh.sourcecache.key] = JSON.stringify(data);
callback(true);
return true;
},
get: function (callback) {
callback = typeof callback == 'function' ? callback : function () {};
var contentStorage = null;
if (typeof localStorage === 'object' && typeof localStorage[_lh.sourcecache.key] === 'string') {
if (_lh.sourcecache.checkif.json(localStorage[_lh.sourcecache.key])) {
contentStorage = JSON.parse(localStorage[_lh.sourcecache.key]);
}
}
callback(contentStorage);
return contentStorage;
}
},
cache: {
push: function (object, callback) {
var object = typeof object == 'object' ? object : {},
callback = typeof callback == 'function' ? callback : function () {};
var sourceUrl = typeof object['url'] == 'string' ? object['url'] : null,
sourceType = typeof object['type'] == 'string' ? object['type'] : null,
sourceContent = typeof object['content'] == 'string' ? object['content'] : null,
sourceTimeout = typeof object['timeout'] == 'number' ? object['timeout'] : null;
if (sourceType == null || sourceType == null || sourceType == null) {
callback(false);
return false;
}
var timeNow = Math.floor(Date.now() / 1000);
var cachedListObj = _lh.sourcecache.cache.each();
cachedListObj['sourcecache'] = typeof cachedListObj['sourcecache'] == 'object' ? cachedListObj['sourcecache'] : {};
cachedListObj['sourcecache'][sourceUrl] = {'type': sourceType, 'content': sourceContent, 'added': timeNow, 'timeout': sourceTimeout};
return _lh.sourcecache.storage.write(cachedListObj, function (isPushed) {
callback(isPushed);
return isPushed;
});
},
each: function (callback) {
var callback = typeof callback == 'function' ? callback : function () {};
var cachedObj = _lh.sourcecache.storage.get();
if (cachedObj == null) {
cachedObj = {};
}
callback(cachedObj);
return cachedObj;
},
clean: function (callback) {
var callback = typeof callback == 'function' ? callback : function () {};
var timeNow = Math.floor(Date.now() / 1000);
var cachedListObj = _lh.sourcecache.cache.each();
cachedListObj['sourcecache'] = typeof cachedListObj['sourcecache'] == 'object' ? cachedListObj['sourcecache'] : {};
for (var sourceUrl in cachedListObj['sourcecache']) {
var sourceObj = cachedListObj['sourcecache'][sourceUrl];
if (sourceObj['added'] + sourceObj['timeout'] < timeNow) {
delete cachedListObj['sourcecache'][sourceUrl];
}
}
return _lh.sourcecache.storage.write(cachedListObj, function (isPushed) {
callback(isPushed);
return isPushed;
});
},
refresh: function (callback) {
var callback = typeof callback == 'function' ? callback : function () {};
var timeNow = Math.floor(Date.now() / 1000);
var cachedListObj = _lh.sourcecache.cache.each();
cachedListObj['sourcecache'] = typeof cachedListObj['sourcecache'] == 'object' ? cachedListObj['sourcecache'] : {};
return _lh.sourcecache.storage.write(cachedListObj, function (isPushed) {
callback(isPushed);
return isPushed;
});
}
}
};
(Mẹo: thêm vào thẻ <head> để được tải)
Sử dụng:
- Code:
var fileType = 'js', // kiểu tệp nguồn: css, js,..
fileUrl = '...', // đường dẫn đến tệp nguồn
fileTimeout = 12 * 60 * 60; // thời gian lưu trữ tạm, tín bằng giây. ví dụ: 12 * 60 * 60 là 12 giờ
fileCallback = function (fileLoadedObj) {
console.log(fileLoadedObj); // giá tri trả về
}; // hàm gọi lại sau khi hoàn tất
_lh.sourcecache.load(fileType, fileUrl, (12 * 60 * 60), fileTimeout);
Ví dụ:
- Code:
_lh.sourcecache.load('js', 'https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.1.4\/jquery.min.js', (24* 60 * 60));
Kiểm tra khả dụng:
- Code:
if (_lh.sourcecache.checkif.supported()) {
// trình duyệt hỗ trợ.
}else {
// trình duyệt không hỗ trợ.
}
Tags: [You must be registered and logged in to see this link.]