Cache.java

  1. /*
  2.  *    Copyright 2009-2023 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;

  17. import java.util.concurrent.locks.ReadWriteLock;

  18. /**
  19.  * SPI for cache providers.
  20.  * <p>
  21.  * One instance of cache will be created for each namespace.
  22.  * <p>
  23.  * The cache implementation must have a constructor that receives the cache id as an String parameter.
  24.  * <p>
  25.  * MyBatis will pass the namespace as id to the constructor.
  26.  *
  27.  * <pre>
  28.  * public MyCache(final String id) {
  29.  *   if (id == null) {
  30.  *     throw new IllegalArgumentException("Cache instances require an ID");
  31.  *   }
  32.  *   this.id = id;
  33.  *   initialize();
  34.  * }
  35.  * </pre>
  36.  *
  37.  * @author Clinton Begin
  38.  */

  39. public interface Cache {

  40.   /**
  41.    * @return The identifier of this cache
  42.    */
  43.   String getId();

  44.   /**
  45.    * @param key
  46.    *          Can be any object but usually it is a {@link CacheKey}
  47.    * @param value
  48.    *          The result of a select.
  49.    */
  50.   void putObject(Object key, Object value);

  51.   /**
  52.    * @param key
  53.    *          The key
  54.    *
  55.    * @return The object stored in the cache.
  56.    */
  57.   Object getObject(Object key);

  58.   /**
  59.    * As of 3.3.0 this method is only called during a rollback for any previous value that was missing in the cache. This
  60.    * lets any blocking cache to release the lock that may have previously put on the key. A blocking cache puts a lock
  61.    * when a value is null and releases it when the value is back again. This way other threads will wait for the value
  62.    * to be available instead of hitting the database.
  63.    *
  64.    * @param key
  65.    *          The key
  66.    *
  67.    * @return Not used
  68.    */
  69.   Object removeObject(Object key);

  70.   /**
  71.    * Clears this cache instance.
  72.    */
  73.   void clear();

  74.   /**
  75.    * Optional. This method is not called by the core.
  76.    *
  77.    * @return The number of elements stored in the cache (not its capacity).
  78.    */
  79.   int getSize();

  80.   /**
  81.    * Optional. As of 3.2.6 this method is no longer called by the core.
  82.    * <p>
  83.    * Any locking needed by the cache must be provided internally by the cache provider.
  84.    *
  85.    * @return A ReadWriteLock
  86.    */
  87.   default ReadWriteLock getReadWriteLock() {
  88.     return null;
  89.   }

  90. }