`
gaojingsong
  • 浏览: 1154107 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

Ibatis源码阅读LRU (least recently used) 之LruCacheController

阅读更多

/*

 *  Copyright 2004 Clinton Begin

 *

 *  Licensed under the Apache License, Version 2.0 (the "License");

 *  you may not use this file except in compliance with the License.

 *  You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 *  Unless required by applicable law or agreed to in writing, software

 *  distributed under the License is distributed on an "AS IS" BASIS,

 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 *  See the License for the specific language governing permissions and

 *  limitations under the License.

 */

package com.ibatis.sqlmap.engine.cache.lru;

 

import java.util.Collections;

import java.util.HashMap;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import java.util.Properties;

 

import com.ibatis.sqlmap.engine.cache.CacheController;

import com.ibatis.sqlmap.engine.cache.CacheModel;

 

/**

 * LRU (least recently used) cache controller implementation

 */

public class LruCacheController implements CacheController {

 

  private int cacheSize;

  private Map cache;

  private List keyList;

 

  /**

   * Default constructor

   */

  public LruCacheController() {

    this.cacheSize = 100;

    this.cache = Collections.synchronizedMap(new HashMap());

    this.keyList = Collections.synchronizedList(new LinkedList());

  }

 

  public int getCacheSize() {

    return cacheSize;

  }

 

  public void setCacheSize(int cacheSize) {

    this.cacheSize = cacheSize;

  }

 

  /**

   * Configures the cache

   *

   * @param props Optionally can contain properties [reference-type=WEAK|SOFT|STRONG]

   */

  public void setProperties(Properties props) {

    String size = props.getProperty("cache-size");

    if (size == null) {

      size = props.getProperty("size");

    }

    if (size != null) {

      cacheSize = Integer.parseInt(size);

    }

  }

 

  /**

   * Add an object to the cache

   *

   * @param cacheModel The cacheModel

   * @param key        The key of the object to be cached

   * @param value      The object to be cached

   */

  public void putObject(CacheModel cacheModel, Object key, Object value) {

    cache.put(key, value);

    keyList.add(key);

    if (keyList.size() > cacheSize) {

      try {

        Object oldestKey = keyList.remove(0);

        cache.remove(oldestKey);

      } catch (IndexOutOfBoundsException e) {

        //ignore

      }

    }

  }

 

  /**

   * Get an object out of the cache.

   *

   * @param cacheModel The cache model

   * @param key        The key of the object to be returned

   * @return The cached object (or null)

   */

  public Object getObject(CacheModel cacheModel, Object key) {

    Object result = cache.get(key);

    keyList.remove(key);

    if (result != null) {

      keyList.add(key);

    }

    return result;

  }

 

  public Object removeObject(CacheModel cacheModel, Object key) {

    keyList.remove(key);

    return cache.remove(key);

  }

 

  /**

   * Flushes the cache.

   *

   * @param cacheModel The cache model

   */

  public void flush(CacheModel cacheModel) {

    cache.clear();

    keyList.clear();

  }

 

}

 

0
6
分享到:
评论

相关推荐

    ibatis源码,ibatis源码 ibatis源码 ibatis源码

    ibatis的原码 ibatis源码 ibatis源码 ibatis源码

    ibatis源码

    ibatis框架源码剖析书中附带的光盘,ibatis源码分析

    iBATIS框架源码剖析源码

    iBATIS框架源码剖析源码 iBATIS框架源码剖析源码 iBATIS框架源码剖析源码

    最新ibatis 源码

    ibatis源码 学习参考 对于学习ibatis很有帮助

    iBATIS框架源码剖析

    iBATIS框架源码剖析

    ibatis框架源码剖析光盘资料

    mybatis的前身ibatis源码剖析

    iBatis框架源码剖析

    iBATIS一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目。于2010年6月16号被谷歌托管,改名为MyBatis。是一个基于SQL映射支持Java和·NET的持久层框架。

    ibatis源码及实例

    iBATIS一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目。最初侧重于密码软件的开发,现在是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data ...

    springMVC+ibatis的源码

    springmvc+ibatis写的开发源码,对学习springmvc+ibatis的框架搭建和配置有帮助,下载后能够在eclipse中运行。

    ibatis源码 例子

    ibatis 源码 例子 包含 源码,jar都有 部分代码 package com.icss.dao; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.List; import ...

    ibatis源码+api文档+jar包

    在csdn找它一上午花了31分,全都不行,还是从其他网上找到的,贡献出来, 各位注意如果你要看源码,最好也是用本zip包中的jar,不然debug看源码时,会出莫名其妙的错误

    iBatis源码jar包以后上传

    后继还有官方例子iBatis源码jar包以后上传

    ibatis 2.3.4 源码

    ibatis 2.3.4 的源码 public abstract Object insert(String paramString, Object paramObject) throws SQLException; public abstract Object insert(String paramString) throws SQLException; public ...

    IBatis源码+xsd+帮助

    IBatis源码+xsd+帮助,不错的一种OR Mapping框架!

    ibatis 学习源码

    PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd"> cacheModelsEnabled="true" enhancementEnabled="true" lazyLoadingEnabled="true" ...

    ibatis 分页源码修改jar

    NULL 博文链接:https://jsufly.iteye.com/blog/508249

    ibatisDemo 入门源码

    ibatisDemo 入门源码

Global site tag (gtag.js) - Google Analytics