Skip to content

Commit

Permalink
优化缓存机制
Browse files Browse the repository at this point in the history
  • Loading branch information
baukh789 committed Jun 5, 2017
1 parent 3cbd74c commit 52dcd5a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 31 deletions.
70 changes: 45 additions & 25 deletions src/js/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,28 @@ const GridData = function(){
/*
* 用户记忆
* */
// TODO 需要处理项: 将所有的记忆信息放至一个字段, 不再使用一个表一个字段.
const UserMemory = function(){
/*
* 删除用户记忆
* $table: table [jTool Object]
* */
this.delUserMemory = function($table) {
// 如果未指定删除的table, 则全部清除
if(!$table || $table.length === 0) {
return false;
window.localStorage.removeItem('GridManagerMemory');
return true;
}
const _key = this.getMemoryKey($table);
if (!_key) {
let GridManagerMemory = window.localStorage.getItem('GridManagerMemory');
if (!GridManagerMemory) {
return false;
}
window.localStorage.removeItem(_key);
GridManagerMemory = JSON.parse(GridManagerMemory);
// 指定删除的table, 则定点清除
const _key = this.getMemoryKey($table);
delete(GridManagerMemory[_key]);
// 清除后, 重新存储
window.localStorage.setItem('GridManagerMemory', JSON.stringify(GridManagerMemory));
return true;
};
/*
Expand Down Expand Up @@ -103,15 +111,16 @@ const UserMemory = function(){
if(!_key) {
return {};
}
const _localStorage = window.localStorage.getItem(_key);
let GridManagerMemory = window.localStorage.getItem('GridManagerMemory');
//如无数据,增加属性标识:grid-manager-cache-error
if(!_localStorage){
if(!GridManagerMemory || GridManagerMemory === '{}'){
$table.attr('grid-manager-cache-error','error');
return {};
}
GridManagerMemory = JSON.parse(GridManagerMemory);
const _data = {
key: _key,
cache: JSON.parse(_localStorage)
cache: JSON.parse(GridManagerMemory[_key] || '{}')
};
return _data;
};
Expand Down Expand Up @@ -179,7 +188,15 @@ const UserMemory = function(){
_cache.page = _pageCache;
}
const _cacheString = JSON.stringify(_cache);
window.localStorage.setItem(_this.getMemoryKey(_table), _cacheString);
let GridManagerMemory = window.localStorage.getItem('GridManagerMemory');
if (!GridManagerMemory) {
GridManagerMemory = {};
}
else {
GridManagerMemory = JSON.parse(GridManagerMemory);
}
GridManagerMemory[_this.getMemoryKey(_table)] = _cacheString;
window.localStorage.setItem('GridManagerMemory', JSON.stringify(GridManagerMemory));
return _cacheString;
};
};
Expand All @@ -205,33 +222,37 @@ const Cache = {
}
/*
* @验证版本号清除列表缓存
* $.table: jTool table
* $.version: 版本号
* $table: jTool table
* version: 版本号
* */
,cleanTableCacheForVersion: function(table, version) {
,cleanTableCacheForVersion: function($table, version) {
const cacheVersion = window.localStorage.getItem('GridManagerVersion');
// 当前为第一次渲染
if(!cacheVersion) {
window.localStorage.setItem('GridManagerVersion', version);
}
// 版本变更
// 版本变更, 清除所有的用户记忆
if(cacheVersion && cacheVersion !== version) {
this.cleanTableCache(table, '版本已升级,原缓存被自动清除');
// 异步执行, 防止同页面有多个表格时,清理完第一个即对版本验证关键字段的修改. [如果该字段被修改, 那么同页面后续的表格将不再被清理]
setTimeout(function(){
window.localStorage.setItem('GridManagerVersion', version);
});
this.cleanTableCache(null, '版本已升级,原全部缓存被自动清除');
window.localStorage.setItem('GridManagerVersion', version);
}
}
/*
* @清除列表缓存
* $.table: table [jTool object]
* $.cleanText: 清除缓存的原因
* $table: table [jTool object]
* cleanText: 清除缓存的原因
* */
,cleanTableCache: function(table, cleanText) {
const Settings = this.getSettings(table);
this.delUserMemory(table);
Base.outLog(Settings.gridManagerName + '清除缓存成功,清除原因:'+ cleanText, 'info');
,cleanTableCache: function($table, cleanText) {
// 不指定table, 清除全部
if ($table === null) {
this.delUserMemory();
Base.outLog('清除缓存成功,清除原因:'+ cleanText, 'info');
// 指定table, 定点清除
} else {
const Settings = this.getSettings($table);
this.delUserMemory($table);
Base.outLog(Settings.gridManagerName + '清除缓存成功,清除原因:'+ cleanText, 'info');
}
}
/*
* @根据本地缓存thead配置列表: 获取本地缓存, 存储原位置顺序, 根据本地缓存进行配置
Expand All @@ -242,9 +263,8 @@ const Cache = {
const _this = this;
const _data = _this.getUserMemory(table), //本地缓存的数据
_domArray = [];

//验证:当前table 没有缓存数据
if(!_data || $.isEmptyObject(_data)) {
if(!_data || $.isEmptyObject(_data) || !_data.cache || $.isEmptyObject(_data.cache)) {
return;
}
// 列表的缓存数据
Expand Down
1 change: 0 additions & 1 deletion src/js/GridManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class GridManager {
* $.callback:回调
* */
init(jToolObj, arg, callback) {

const _this = this;
if(typeof arg.gridManagerName !== 'string' || arg.gridManagerName.trim() === ''){
arg.gridManagerName = jToolObj.attr('grid-manager'); //存储gridManagerName值
Expand Down
4 changes: 2 additions & 2 deletions test/Cache_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ describe('Cache.js', function() {
});

it('Cache.delUserMemory($table)', function() {
expect(Cache.delUserMemory()).toBe(false);
expect(Cache.delUserMemory($table)).toBe(true);
expect(Cache.delUserMemory()).toBe(true);
expect(Cache.delUserMemory($table)).toBe(false);
});

it('Cache.__getGridManager($table)', function() {
Expand Down
5 changes: 2 additions & 3 deletions test/Publish_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('Publish.js', function() {
it('PublishMethod.getLocalStorage($table)', function(){
let userMemory = null;
userMemory = PublishMethod.getLocalStorage($table);
expect(userMemory).toEqual({});
expect(userMemory.key).toEqual('/context.html-test-publish');
Cache.saveUserMemory($table);
userMemory = PublishMethod.getLocalStorage($table);
expect(userMemory.key).toBeDefined();
Expand All @@ -113,8 +113,7 @@ describe('Publish.js', function() {
let userMemory = null;
PublishMethod.clear($table);
userMemory = PublishMethod.getLocalStorage($table);
expect(userMemory).toEqual({});

expect(userMemory.cache).toEqual({});
userMemory = null;
});

Expand Down
1 change: 1 addition & 0 deletions version/v2.3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@

## 修复BUG
- 表头置顶 scroll 事件触发时表头跳屏BUG修复
- 版本更新时, 清除缓存未能清除全部BUG

0 comments on commit 52dcd5a

Please sign in to comment.