`
lyy3323
  • 浏览: 85984 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

抄袭ibatis缓存设计。自实现缓存设计

阅读更多
1.较为简单的缓存机制,不支持分布式缓存。
2.目前只实现了memory形式。如有扩展,请自实现fifo,lru等形式。
3.为了缓存机制过于复杂,暂不支持配置文件形式。以后会用第三方组件代替,没必要在此处下太大功夫。
4.增加了factory生成相应的cachemodel。保证单例。
5.使用方式:
5.1factory通过加载指定的缓存机制生产cachemodel(缓存包装类),每种cachecontrol对应一个cachemodel(单例)。
5.2通过操作cachemodel(内部包含了cachecontrol接口,解耦)来进行实际的动作。

核心code.

package com.common.utils.cacheUtils.myCache;

import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 缓存模式包装类,公用的设计均写在此处。(日志,控制时间等等。) <br>
 * 
 * @author yzx
 * @see
 * @since
 */
public class CacheModel {

    private static Logger     logger              = Logger.getLogger(CacheModel.class);
    private CacheControl      cacheControl;
    private static final int  MAX_OBJECT_LOG_SIZE = 100;
    private static final long NO_FLUSH_INTERVAL   = -99999;
    private String            id;
    private boolean           readOnly;
    private boolean           serialize;
    private long              lastFlush;
    private long              flushInterval;
    private long              flushIntervalSeconds;

    private CacheModel() {

    }

    /**
     * 缓存模式初始化
     * 
     * @param controllerClassName
     */
    protected CacheModel(String controllerClassName) {
        try {
            this.flushInterval = NO_FLUSH_INTERVAL;
            this.flushIntervalSeconds = NO_FLUSH_INTERVAL;
            this.lastFlush = System.currentTimeMillis();
            cacheControl = (CacheControl) Class.forName(controllerClassName).newInstance();
        } catch (Exception e) {
            logger.error("缓存控制器包装类初始化失败!" + e.getMessage());
            throw new RuntimeException("缓存控制器包装类初始化失败!");
        }

    }

    public void configure(Properties props) {
        cacheControl.configure(props);
    }

    /**
     * Clears the cache
     */
    public void flush() {
        synchronized (this) {
            cacheControl.flush(this);
            lastFlush = System.currentTimeMillis();
        }
    }

    public Object getObject(Object key) {
        Object value = null;
        // 暂不用,若使用,需要同步
        // if (flushInterval != NO_FLUSH_INTERVAL && System.currentTimeMillis() - lastFlush > flushInterval) {
        // flush();
        // }
        value = cacheControl.getObject(this, key);
        return value;

    }

    public void putObject(Object key, Object value) {
        synchronized (this) {
            cacheControl.putObject(this, key, value);
        }
    }

    public Object removeObject(Object key) {
        synchronized (this) {
            return cacheControl.removeObject(this, key);
        }

    }

    public static Logger getLogger() {
        return logger;
    }

    public static void setLogger(Logger logger) {
        CacheModel.logger = logger;
    }

    // public CacheControl getCacheControl() {
    // return cacheControl;
    // }
    //
    // public void setCacheControl(CacheControl cacheControl) {
    // this.cacheControl = cacheControl;
    // }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public boolean isReadOnly() {
        return readOnly;
    }

    public void setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
    }

    public boolean isSerialize() {
        return serialize;
    }

    public void setSerialize(boolean serialize) {
        this.serialize = serialize;
    }

    public long getLastFlush() {
        return lastFlush;
    }

    public void setLastFlush(long lastFlush) {
        this.lastFlush = lastFlush;
    }

    public long getFlushInterval() {
        return flushInterval;
    }

    public void setFlushInterval(long flushInterval) {
        this.flushInterval = flushInterval;
    }

    public long getFlushIntervalSeconds() {
        return flushIntervalSeconds;
    }

    public void setFlushIntervalSeconds(long flushIntervalSeconds) {
        this.flushIntervalSeconds = flushIntervalSeconds;
    }

    public static int getMaxObjectLogSize() {
        return MAX_OBJECT_LOG_SIZE;
    }

    public static long getNoFlushInterval() {
        return NO_FLUSH_INTERVAL;
    }

}



package com.common.utils.cacheUtils.myCache;

import java.util.Properties;

/**
 * Description:缓存自实现接口 <br>
 * 
 * @author yzx
 * @see
 * @since
 */
public interface CacheControl {

    /**
     * 清空缓存
     * 
     * @param cacheModel
     */
    public void flush(CacheModel cacheModel);

    /**
     * 获取缓存值
     * 
     * @param cacheModel
     * @param key
     * @return
     */
    public Object getObject(CacheModel cacheModel, Object key);

    /**
     * 移除某个缓存值
     * 
     * @param cacheModel
     * @param key
     * @return
     */
    public Object removeObject(CacheModel cacheModel, Object key);

    /**
     * 更新缓存
     * 
     * @param cacheModel
     * @param key
     * @param value
     */
    public void putObject(CacheModel cacheModel, Object key, Object value);

    /**
     * 为配置文件做扩展用,暂不用(不想在此处做的太复杂。将来会考虑用第三方组件)
     * 
     * @param props
     */
    @Deprecated
    public void configure(Properties props);
}



  • 大小: 49.3 KB
分享到:
评论
1 楼 lizhongyi199 2012-08-01  
源码不全啊,能不能上传完整看看。

相关推荐

Global site tag (gtag.js) - Google Analytics