SoftCache.java

  1. /*
  2.  *    Copyright 2009-2024 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.  *       https://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 java.util.concurrent.locks.ReentrantLock;

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

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

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

  42.   @Override
  43.   public String getId() {
  44.     return delegate.getId();
  45.   }

  46.   @Override
  47.   public int getSize() {
  48.     removeGarbageCollectedItems();
  49.     return delegate.getSize();
  50.   }

  51.   public void setSize(int size) {
  52.     this.numberOfHardLinks = size;
  53.   }

  54.   @Override
  55.   public void putObject(Object key, Object value) {
  56.     removeGarbageCollectedItems();
  57.     delegate.putObject(key, new SoftEntry(key, value, queueOfGarbageCollectedEntries));
  58.   }

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

  83.   @Override
  84.   public Object removeObject(Object key) {
  85.     removeGarbageCollectedItems();
  86.     @SuppressWarnings("unchecked")
  87.     SoftReference<Object> softReference = (SoftReference<Object>) delegate.removeObject(key);
  88.     return softReference == null ? null : softReference.get();
  89.   }

  90.   @Override
  91.   public void clear() {
  92.     lock.lock();
  93.     try {
  94.       hardLinksToAvoidGarbageCollection.clear();
  95.     } finally {
  96.       lock.unlock();
  97.     }
  98.     removeGarbageCollectedItems();
  99.     delegate.clear();
  100.   }

  101.   private void removeGarbageCollectedItems() {
  102.     SoftEntry sv;
  103.     while ((sv = (SoftEntry) queueOfGarbageCollectedEntries.poll()) != null) {
  104.       delegate.removeObject(sv.key);
  105.     }
  106.   }

  107.   private static class SoftEntry extends SoftReference<Object> {
  108.     private final Object key;

  109.     SoftEntry(Object key, Object value, ReferenceQueue<Object> garbageCollectionQueue) {
  110.       super(value, garbageCollectionQueue);
  111.       this.key = key;
  112.     }
  113.   }

  114. }