ui-cache

0.0.3 • Public • Published

七鱼缓存库

七鱼缓存层基于不可变数据模型设计, 引用关系来实现不同缓存之间的通信。七鱼缓存库实现如下两个功能:

  1. 实体列表 的存储
  2. 多个缓存的实例引用同一份数据源
  3. 不同缓存实例之间的通信

cache

注: 接入的时候, 可以根据需求, 使用不可变数据概念(Immutable.js)。

构建npm指令

  "dev": "webpack -p --config webpack.config.js",
  "test": "karma start karma.conf.js"

使用说明

  1. 保证数据的唯一性, 多个缓存的实例引用同一份数据源
  2. Hash实体以 id 为主键, 现不支持自定义
    // 继承 Super
    var SuperCache = function(options){
        this.init(options)
    }
 
    SuperCache.prototype = Object.create(Cache.prototype);
 
    // 实例化
    var supercache = new SuperCache({
        name : 'super'
    });
 
    // 初始化设置列表
    supercache.setListInCache([{id : 1, name : 1}], 'super-list-1');
 
 
    // 继承 Sofia
    var SofiaCache = function(options){
        this.init(options)
    }
 
    SofiaCache.prototype = Object.create(Cache.prototype);
 
    // 实例化
    var sofiacache = new SuperCache({
        name : 'sofia'
    });
 
    // 初始化设置列表
    sofiacache.setListInCache([{id : 'a', name : 'a'}], 'sofia-list-1');
 

接口说明

全局缓存接口

getCacheByName(name)

通过名称查询全局缓存中 对象

    // equal
    supercache.getCacheByName('super');
 
    sofiacache.getCacheByName('super');

getCacheList(name, key)

通过 名称 查询全局缓存中 列表值

    supercache.getCacheList('super', 'super-list-1')
 
    sofiacache.getCacheList('super', 'super-list-1')

getCacheHash(name, id)

通过 名称id 查询全局缓存中 哈希值

    supercache.getCacheHash('super', 1)
 
    sofiacache.getCacheHash('sofia', 'a')

列表缓存接口

setListInCache(array|object, key)

对列表数据的 添加更新, 并对Hash进行 添加更新

    // add list
    sofiacache.setListInCache([{id : 'a', name : 'a'}], 'sofia-list-1');
 
    // add object
    sofiacache.setListInCache({id : 'b', name : 'b'}, 'sofia-list-1');
 
    // sofia-list-1 : [{id : 'a', name : 'a'}, {id : 'b', name : 'b'}]
 

getListInCache(key)

通过列表 key 获取相应列表索引

    sofiacache.setListInCache([{id : 'a', name : 'a'}], 'sofia-list-1');
    sofiacache.getListInCache('sofia-list-1');
 
    // list : [{id : 'a', name : 'a'}]

saveItemToCache(array|object)

保存数据项到 Hash

    // add list
    sofiacache.saveItemToCache([{id : 'a', name : 'a'}]);
 
    // add object
    sofiacache.saveItemToCache({id : 'b', name : 'b'});
 
    // hash: {a : {id : 'a', name : 'a'}, b : {id : 'b', name : 'b'}}
 

unshiftItemInCache(array|object, key)

前向插入数据到 key 列表

    // add list
    sofiacache.unshiftItemInCache([{id : 'a', name : 'a'}], 'sofia-list-1');
 
    // add object
    sofiacache.unshiftItemInCache({id : 'b', name : 'b'}, 'sofia-list-1');
 
    // sofia-list-1: [{id : 'b', name : 'b'}, {id : 'a', name : 'a'}]
 

deleteDataInCache(id)

同时删除 列表 中数据 和 Hash 中数据

    sofiacache.setListInCache([{id : 'a', name : 'a'}], 'sofia-list-1');
 
    sofiacache.deleteDataInCache('a');
 
    // hash : {}, list : []

removeItemInCache(id, key)

仅删除 key 列表中, id 的数据项, 不删除Hash中数据

    sofiacache.setListInCache([{id : 'a', name : 'a'}], 'sofia-list-1');
 
    sofiacache.removeItemInCache('a', 'unshift');
 
    // hash : {a : {id : 'a', name : 'a'}}, list : []

Readme

Keywords

Package Sidebar

Install

npm i ui-cache

Weekly Downloads

30

Version

0.0.3

License

MIT

Last publish

Collaborators

  • dkypooh