SoftCache.java

  1. /*
  2.  *    Copyright 2009-2022 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.cache.decorators;

  17. import java.lang.ref.ReferenceQueue;
  18. import java.lang.ref.SoftReference;
  19. import java.util.Deque;
  20. import java.util.LinkedList;

  21. import org.apache.ibatis.cache.Cache;

  22. /**
  23.  * Soft Reference cache decorator
  24.  * Thanks to Dr. Heinz Kabutz for his guidance here.
  25.  *
  26.  * @author Clinton Begin
  27.  */
  28. public class SoftCache implements Cache {
  29.   private final Deque<Object> hardLinksToAvoidGarbageCollection;
  30.   private final ReferenceQueue<Object> queueOfGarbageCollectedEntries;
  31.   private final Cache delegate;
  32.   private int numberOfHardLinks;

  33.   public SoftCache(Cache delegate) {
  34.     this.delegate = delegate;
  35.     this.numberOfHardLinks = 256;
  36.     this.hardLinksToAvoidGarbageCollection = new LinkedList<>();
  37.     this.queueOfGarbageCollectedEntries = new ReferenceQueue<>();
  38.   }

  39.   @Override
  40.   public String getId() {
  41.     return delegate.getId();
  42.   }

  43.   @Override
  44.   public int getSize() {
  45.     removeGarbageCollectedItems();
  46.     return delegate.getSize();
  47.   }

  48.   public void setSize(int size) {
  49.     this.numberOfHardLinks = size;
  50.   }

  51.   @Override
  52.   public void putObject(Object key, Object value) {
  53.     removeGarbageCollectedItems();
  54.     delegate.putObject(key, new SoftEntry(key, value, queueOfGarbageCollectedEntries));
  55.   }

  56.   @Override
  57.   public Object getObject(Object key) {
  58.     Object result = null;
  59.     @SuppressWarnings("unchecked") // assumed delegate cache is totally managed by this cache
  60.     SoftReference<Object> softReference = (SoftReference<Object>) delegate.getObject(key);
  61.     if (softReference != null) {
  62.       result = softReference.get();
  63.       if (result == null) {
  64.         delegate.removeObject(key);
  65.       } else {
  66.         // See #586 (and #335) modifications need more than a read lock
  67.         synchronized (hardLinksToAvoidGarbageCollection) {
  68.           hardLinksToAvoidGarbageCollection.addFirst(result);
  69.           if (hardLinksToAvoidGarbageCollection.size() > numberOfHardLinks) {
  70.             hardLinksToAvoidGarbageCollection.removeLast();
  71.           }
  72.         }
  73.       }
  74.     }
  75.     return result;
  76.   }

  77.   @Override
  78.   public Object removeObject(Object key) {
  79.     removeGarbageCollectedItems();
  80.     @SuppressWarnings("unchecked")
  81.     SoftReference<Object> softReference = (SoftReference<Object>) delegate.removeObject(key);
  82.     return softReference == null ? null : softReference.get();
  83.   }

  84.   @Override
  85.   public void clear() {
  86.     synchronized (hardLinksToAvoidGarbageCollection) {
  87.       hardLinksToAvoidGarbageCollection.clear();
  88.     }
  89.     removeGarbageCollectedItems();
  90.     delegate.clear();
  91.   }

  92.   private void removeGarbageCollectedItems() {
  93.     SoftEntry sv;
  94.     while ((sv = (SoftEntry) queueOfGarbageCollectedEntries.poll()) != null) {
  95.       delegate.removeObject(sv.key);
  96.     }
  97.   }

  98.   private static class SoftEntry extends SoftReference<Object> {
  99.     private final Object key;

  100.     SoftEntry(Object key, Object value, ReferenceQueue<Object> garbageCollectionQueue) {
  101.       super(value, garbageCollectionQueue);
  102.       this.key = key;
  103.     }
  104.   }

  105. }