美文网首页
phis-客户端缓存技术分析

phis-客户端缓存技术分析

作者: 359c7a79d70f | 来源:发表于2016-07-10 22:11 被阅读56次

    /** phis 缓存加载! 如果浏览器支持和html5,则调用phis\EAPDomain\unieap\ria3.3\unieap\clientCache\localStorage.js 组件做客户端缓存操作! 如果浏览器不支持的话:则调用phis\EAPDomain\unieap\ria3.3\unieap\clientCache\activexSQLite.js */

    dojo.require("unieap.global");
    dojo.require("unieap.util.util");
    dojo.provide("unieap.cache");
    (function(){
        var isHTML5Available = ('localStorage' in window) && window['localStorage'] !== null && window['localStorage'] !== undefined;
        if(isHTML5Available){
            dojo.require("unieap.clientCache.localStorage");
        }else{
            dojo.require("unieap.clientCache.activexSQLite");
        }
        unieap.cache = new unieap.clientCache();
    })();
    /**
     目前phis只支持IE8/9 所以客户端缓存采用的都是js通过自定义的ActiveX控件调用SQLite完成!
     所以,以上代码端都是走的else分支!初始实例化的本质都是
     在 本地:C:/phis_local/db/目录下,new一个名字为:phislocalcache+ gsLoginInfo.getOrgId()(4028c8813a002456013a00a4c6ca001f)+.db 的SQLite数据库
     
     phis\EAPDomain\unieap\ria3.3\unieap\clientCache\activexSQLite.js 
    */
    dojo.require("unieap.global");
    dojo.require("unieap.util.util");
    dojo.provide("unieap.clientCache.activexSQLite");
    /**
     * @declaredClass:
     *      unieap.cache
     * @summary:
     *      缓存操作底层接口,用于操作google gears
     * @example:
     * |<script type="text/javascript">
     * |    //判断google gears是否可用
     * |    var isCacheUsable=unieap.cache.isAvailable();
     * |    alert(isCacheUsable);
     * |</script>
     * @example:
     * |<script type="text/javascript">
     * |    //获得缓存在google gears中的总记录数
     * |    var totalCount=unieap.cache.getCount();
     * |</script>
     * @example:
     * |<script type="text/javascript">
     * |    //删除google gears中缓存的所有数据
     * |    unieap.cache.clear();
     * |</script>
     */
    dojo.declare("unieap.clientCache", null, {
        // 数据库存放系统路径
        DB_PATH : "C:/phis_local/db/",
        
        // SQLite中要创建的database的名字
        DB_NAME : "phislocalcache",
        
        // 本地数据库文件
        _cache_dbPath : "",
        
        // SQLite中要创建的table的名字
        CODELIST_TABLE_NAME : "unieap_codelist_table",
        
        constructor: function(param) {
            dojo.mixin(this, param);
        },
        
        _cache_db : null,
        
        _initActiveX:function(){
            var _db = null;
            
            if(document.getElementById('sqliteObj')){
                _db = document.getElementById('sqliteObj');
            } else {
                if (navigator.appVersion.indexOf("MSIE") != -1) {
                    // IE
                    var obj = document.createElement("object");
                    obj.setAttribute("id","sqliteObj");
                    obj.setAttribute("style","visibility:hidden;width:0px; height:0px;");
                    obj.setAttribute("classid","clsid:3E22694D-7B92-42A1-89A7-668E2F7AA107");
                    obj.setAttribute("progid","LiteX.LiteConnection");
                    document.appendChild(obj);
                } else {
                    alert("请使用IE浏览器");
                    return null;
                }
                _db = document.getElementById('sqliteObj');
            }
            if (!_db) {
                return null;
            }
            return _db;
        },
        
        /**
         * 初始化本地数据库
         * 
         * @return 数据库对象,如果初始化失败则返回nul
         */
        _initDB:function(){
            if(typeof(gsLoginInfo) != 'undefined' && typeof(gsLoginInfo.getOrgId) != 'undefined'){
                if(unieap.isEmpty(this._cache_dbPath)){
                    this._cache_dbPath += this.DB_PATH + + this.DB_NAME + gsLoginInfo.getOrgId() + ".db";
                }
            } else if(typeof(getCurrentOrgID) != 'undefined'){
                if(unieap.isEmpty(this._cache_dbPath)){
                    this._cache_dbPath += this.DB_PATH + + this.DB_NAME + getCurrentOrgID() + ".db";
                }
            }
            
            if (this._cache_db) {
                this._cache_db.path = this._cache_dbPath;
                //打开数据库,每次使用结束后必须执行Close()方法关闭
                try{
                    this._cache_db.Open();
                }catch(e){
                    this._cache_db.Close();
                    this._cache_db.Open();
                }
                return this._cache_db;
            }
            if(unieap.global.isUseClientCache == false){
                return null;
            }   
            try {
                //创建db  
                this._cache_db = this._initActiveX();
                if(this._cache_db){
                    this._cache_db.path = this._cache_dbPath;
                    //打开数据库,每次使用结束后必须执行Close()方法关闭
                    this._cache_db.Open();
                }
            }catch(e){
                MessageBox.alert({
                    title:"错误",
                    message:"没有找到数据库文件: "+e.message
                });
                this._cache_db = null;
            }
            return this._cache_db;
        },
        
        // 创建数据字典DB以及表
        /**
         *@summary:
         *      在SQLite中创建codelist_table表
         * @example:
         * |<script>
         * |    unieap.cache.creatCodeListTable();
         * |</script>   
         */
        creatCodeListTable : function(){
            var db = this._initDB();
            if (db == null) {
                return null;
            } 
            
            db.execute("CREATE TABLE IF NOT EXISTS "
            + unieap.cache.CODELIST_TABLE_NAME + "(key TEXT,value TEXT,timestamp INT)");
            db.execute("CREATE UNIQUE INDEX IF NOT EXISTS key_index"
                + " ON " + unieap.cache.CODELIST_TABLE_NAME + " (key)");
            
            try{
                db.Close();
            } catch(ex){}
        },
        
        /**
         *@summary:
         *      判断ActiveX是否可用
         * @description:
         *      如果ActiveX没有注册或者用户在gloabal.js中设置unieap.global.isUseClientCache为false,
         *      程序返回的也为false
         */
        isAvailable : function(){
            return unieap.global.isUseClientCache 
                    && this._initActiveX()!= null;
        },
        
        //清除gears中codelist_table表的所有记录
        /**
         *@summary:
         *      清除gears中codelist_table表的所有记录
         * @example:
         * |<script>
         * |    unieap.cache.clear();
         * |</script>   
         * 通过unieap的debug工具查看缓存数据是否被清空  
         */
        clear : function() {
            var db = this._initDB();
            if (db == null) {
                return;
            }
            db.execute("DELETE FROM " + this.CODELIST_TABLE_NAME);
            
            try{
                db.Close();
            }catch(ex){
            }
        },
        
        //往codelist_table表中插入数据
        //unieap.cache.put('name','neusoft');
        //如果不设置timestamp,timestamp值为默认为1
        /**
         *@summary:
         *      往codelist_table表中插入数据
         * @param:
         *      {string} key  
         * @param:
         *      {string} value 
         * @param:
         *      {string} timestamp   
         * @example:
         * |<script>
         * |    var value = "[
         * |        {CODEVALUE:1,CODENAME:'汉族'},
         * |        {CODEVALUE:2,CODENAME:'回族'},
         * |        {CODEVALUE:3,CODENAME:'白族'}
         * |    ]";
         * |    unieap.cache.put("dept",value,String(new Date().getTime()));
         * |</script> 
         * 通过unieap的debug工具查看缓存数据是否被清空
         */
        put : function(key, value,timestamp) {
            var db = this._initDB();
            if (db == null) {
                return;
            }
            db.execute("REPLACE INTO " + this.CODELIST_TABLE_NAME
                    + " VALUES (?,?,?)", key, value,timestamp||1);
            
            try{
                db.Close();
            }catch(ex){
            }
        },
        
        //往codelist_table表中插入数据
        //unieap.cache.putMultiple(['name','age'],['neusoft',20]);
        //如果不设置timestamp,timestamp值为默认为1
        putMultiple : function(keys, values,timestamps) {
            var db = this._initDB();
            if (db == null) {
                return;
            }
            db.execute("BEGIN TRANSACTION");
            var _stmt = "REPLACE INTO " + this.CODELIST_TABLE_NAME
                    + " VALUES (?, ?,?)";
            for (var i = 0,j = 0,k=0, key, value,timestamp; (key = keys[i++]) && (value = values[j++])&&(timestamp=timestamps&&timestamps[k++]||1);) {
            
                db.execute(_stmt, key, value, timestamp);
            }
            db.execute("COMMIT TRANSACTION");
            try{
                db.Close();
            }catch(ex){
            }
        },
        
        //从codelist_table表中获取数据
        ////unieap.cache.get('name')=>'neusoft'
        get : function(key) {
            var db = this._initDB();
            var value = null;
            if (db == null) {
                return value;
            }
            var rs = db.Prepare("SELECT value FROM " + unieap.cache.CODELIST_TABLE_NAME + " WHERE key = '"+key+"'");
            while (!rs.Step()) {
                value = rs.ColumnValue(0);
            }
            
            this._setRsDsClose(rs, ds);
            return value;
        },
        
        //删除codelist_table表中的一条记录
        //unieap.cache.remove('name')=>key为name的信息被删除了
        remove : function(key) {
            var db = this._initDB();
            if (db == null) {
                return;
            }
            db.execute("DELETE FROM " + this.CODELIST_TABLE_NAME
                    + " WHERE key = ?", key);
            
            try{
                db.Close();
            }catch(ex){
            }
        },
        
        //获取codelist_table表中的所有keys值
        getKeys : function(){
            var db = this._initDB();
            var value = [];
            if (db == null) {
                return null;
            }
            var rs = db.Prepare("SELECT key FROM " + this.CODELIST_TABLE_NAME);
            while (!rs.Step()) {
                value.push(rs.ColumnValue(0));
            }
            
            this._setRsDsClose(rs, ds);
            
            return value;
        },
        
        //获取codelist_table表中的总记录数
        getCount : function() {
            var db = this._initDB();
            if (db == null) {
                return null;
            }
            var rs = db.Prepare("SELECT count(*) FROM " + this.CODELIST_TABLE_NAME);
            var result = rs.ColumnValue(0);
            
            this._setRsDsClose(rs, ds);
            
            return result;
        },
        
        //获取codelist_table表中所有记录的timestamp
        //返回值为一个object,格式为obj[key]=timestamp
        //查找的时间戳大于2649600000,也就是大于1970年1月1日
        getAllTimeStamps : function(){
            var db = this._initDB();
            if (db == null) {
                return null;
            }
            var rs = db.Prepare("SELECT key,timestamp FROM " + this.CODELIST_TABLE_NAME+' WHERE timestamp>2649600000');
            var value={}
            while(!rs.Step()){
                value[rs.ColumnValue(0)]=rs.ColumnValue(1);
            }
            
            this._setRsDsClose(rs, ds);
            
            return value;
        },
        
        // 关闭rs,ds
        _setRsDsClose: function(rs, ds){
            try{
                rs.Close();
            }catch(ex){
            }
            try{
                db.Close();
            }catch(ex){
            }
        }
        
    });
    

    相关文章

      网友评论

          本文标题:phis-客户端缓存技术分析

          本文链接:https://www.haomeiwen.com/subject/noidjttx.html