zoukankan      html  css  js  c++  java
  • cache在项目中的应用

    var Cache = function(max, buffer) {
        this.$cache = [];
        this.$max = max | 0 || 20;
        this.$buffer = buffer | 0 || 5;
    };
    Cache.prototype.set = function(key, cacheItem) {
        var cache = this.$cache;
        key = 'cache_' + key;
        var temp = cache[key];
        if (!cache.hasOwnProperty(key)) {
            if (cache.length >= this.$max + this.$buffer) { //缓存列表和缓冲区都满了
                cache.sort(function(a, b) {
                    return b.fre == a.fre ? b.time - a.time : b.fre - a.fre;
                });
                var counter = this.$buffer;
                while (counter--) { //缓冲区有多少,我们就一次性删除多少
                    var item = cache.pop();
                    delete cache[item.key];
                }
            }
            temp = {};
            cache.push(temp);
            cache[key] = temp;
        }
        temp.item = cacheItem;
        temp.fre = 1;
        temp.key = key;
        temp.time = new Date().getTime();
        return temp;
    };
    
    Cache.prototype.get = function(key) {
        key = 'cache_' + key;
        var cache = this.$cache;
        var result;
        if (cache.hasOwnProperty(key)) {
            result = cache[key];
            if (result.fre >= 1) {
                result.fre++;
                result.time = new Date().getTime();
                result = result.item;
            }
        }
        return result;
    };
    
    Cache.prototype.has = function(key) {
        key = 'cache_' + key;
        return this.$cache.hasOwnProperty(key);
    };
    
    Cache.prototype.del = function(key) {
        key = 'cache_' + key;
        var cache = this.$cache;
        var result = cache[key];
        if (result) {
            result.fre = -1;
            delete result.item;
            delete cache[key];
        }
    };
  • 相关阅读:
    JDBC与ODBC的区别与应用
    java web项目中classes文件夹下的class和WEB-INF/lib中jar里的class文件加载顺序
    构造方法的继承
    2015-J. PUMA
    阶乘之和 南邮NOJ 1093
    阶乘之和 南邮NOJ 1093
    阶乘之和 南邮NOJ 1093
    阶乘之和 南邮NOJ 1093
    数的计算
    数的计算
  • 原文地址:https://www.cnblogs.com/haohaoday/p/3620836.html
Copyright © 2011-2022 走看看