Last active
August 29, 2015 14:08
-
-
Save zacksleo/0f20723fcd1df7884057 to your computer and use it in GitHub Desktop.
LargeLocalStorageControl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| define(function(require, exports, module){ | |
| var $ = require('jquery'); | |
| $.ajaxSettings.async = false; | |
| var LargeLocalStorage = require('largeLocalStorage/largeLocalStorage'); | |
| // for debug : window.lls = LargeLocalStorage; | |
| /** | |
| *get Local Data 获取数据 | |
| * @param {String} name | |
| * @returns {JSON} | |
| */ | |
| exports.getData = function(name){ | |
| var value = null; | |
| var storage = new LargeLocalStorage({ | |
| size: 20 * 1024 * 1024, | |
| name: 'lls-ezts-db' | |
| }); | |
| storage.initialized.then(function(){ | |
| storage.getContents(name, {json: true}).then(function(contents){ | |
| value = content; | |
| }).then(function(contents){ | |
| /** | |
| * local data unavailable 本地缓存数据不存在 | |
| */ | |
| if(typeof (contents) == "undefined" || contents == null){ | |
| console.log('1.1:local data unavailable 本地无数据'); | |
| var refer = getLocalStoreControlByName(name); | |
| if(refer){ | |
| $.getJSON(refer.url, function(result){ | |
| storage.setContents(name, result, {json: true}); | |
| storage.setContents(name + '-update-time', refer.update_time); | |
| value = result; | |
| return value; | |
| console.log('1.2:Server data request success 数据请求成功'); | |
| }); | |
| } | |
| console.log('1.3:get server data success 数据获取成功'); | |
| /** | |
| * 本地缓存数据存在 | |
| */ | |
| }else{ | |
| var update_time = storage.getContents(name + '-update-time'); | |
| var refer = getLocalStoreControlByName(name); | |
| /** | |
| * Update local data which is expired 本地缓存数据过期,更新缓存 | |
| */ | |
| if(typeof (update_time) == "undefined" || update_time < refer.update_time){ | |
| console.log('2.1:local data expired 本地缓存数据过期'); | |
| $.getJSON(refer.url, function(result){ | |
| storage.setContents(name, result, {json: true}); | |
| storage.setContents(name + '-update-time', refer.update_time); | |
| value = result; | |
| return value; | |
| }); | |
| }else{ | |
| console.log('3.1: local data available 本地缓存数据可用'); | |
| console.log('3.2:local data is 本地缓存数据为' + value); | |
| value = contents; | |
| return contents; | |
| } | |
| } | |
| }); | |
| }); | |
| console.info('4:end 结束:' + value); | |
| return value; | |
| }; | |
| /** | |
| * 查找缓存控制对照表 | |
| * @param {String} name | |
| * @returns {Object} | |
| */ | |
| function getLocalStoreControlByName(name){ | |
| var dict = window.localStorageControl; | |
| for(var i = 0; i < dict.length; i++){ | |
| if(dict[i].name == name){ | |
| return dict[i]; | |
| } | |
| } | |
| } | |
| }); | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
eg:
localStorageControl = [{"id":"1","name":"getUserList","update_time":"1414034724","url":"/lsc/combo/func/getUserList"},{"id":"17","name":"getDbTypeList","update_time":"1414034724","url":"/lsccombo/func/getDbTypeList"}]