zoukankan      html  css  js  c++  java
  • mybatis-3 cache 源码赏析

     总结:

    从缓存策略源码,可以分析java相关类库

    mybatis-3/src/main/java/org/apache/ibatis/cache/decorators/SoftCache.java

    package org.apache.ibatis.cache.decorators;
       
      import java.lang.ref.ReferenceQueue;
      import java.lang.ref.SoftReference;
      import java.util.Deque;
      import java.util.LinkedList;
      import java.util.concurrent.locks.ReadWriteLock;
       
      import org.apache.ibatis.cache.Cache;
     

    https://github.com/mybatis/mybatis-3/blob/67a1c4ea5726c605d297398a8ab9a42ab7d4ebf1/src/main/java/org/apache/ibatis/cache/decorators/FifoCache.java

    /**
     *    Copyright 2009-2018 the original author or authors.
     *
     *    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 org.apache.ibatis.cache.decorators;
    
    import java.util.Deque;
    import java.util.LinkedList;
    import java.util.concurrent.locks.ReadWriteLock;
    
    import org.apache.ibatis.cache.Cache;
    
    /**
     * FIFO (first in, first out) cache decorator
     *
     * @author Clinton Begin
     */
    public class FifoCache implements Cache {
    
      private final Cache delegate;
      private final Deque<Object> keyList;
      private int size;
    
      public FifoCache(Cache delegate) {
        this.delegate = delegate;
        this.keyList = new LinkedList<>();
        this.size = 1024;
      }
    
      @Override
      public String getId() {
        return delegate.getId();
      }
    
      @Override
      public int getSize() {
        return delegate.getSize();
      }
    
      public void setSize(int size) {
        this.size = size;
      }
    
      @Override
      public void putObject(Object key, Object value) {
        cycleKeyList(key);
        delegate.putObject(key, value);
      }
    
      @Override
      public Object getObject(Object key) {
        return delegate.getObject(key);
      }
    
      @Override
      public Object removeObject(Object key) {
        return delegate.removeObject(key);
      }
    
      @Override
      public void clear() {
        delegate.clear();
        keyList.clear();
      }
    
      @Override
      public ReadWriteLock getReadWriteLock() {
        return null;
      }
    
      private void cycleKeyList(Object key) {
        keyList.addLast(key);
        if (keyList.size() > size) {
          Object oldestKey = keyList.removeFirst();
          delegate.removeObject(oldestKey);
        }
      }
    
    }
    

      

  • 相关阅读:
    java基本数据类型
    解决Eclipse导入项目工程出现The project was not built since错误
    解决java错误“编码 GBK 的不可映射字符”
    helloworld每一个程序员的开始!
    java环境变量path也配置了,但还是显示javac不是内部或外部命令的解决办法
    WAS8.5.5根据CVE-2019-4279漏洞升级
    ansible 配置数据源源头为163
    升级openssh
    ansible-1 参数常用模块
    xlsxwriter模块详解
  • 原文地址:https://www.cnblogs.com/rsapaper/p/10186344.html
Copyright © 2011-2022 走看看