1 /** 2 * Copyright 2009-2015 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; 17 18 import java.util.concurrent.locks.ReadWriteLock; 19 20 /** 21 * SPI for cache providers. 22 * 23 * One instance of cache will be created for each namespace. 24 * 25 * The cache implementation must have a constructor that receives the cache id as an String parameter. 26 * 27 * MyBatis will pass the namespace as id to the constructor. 28 * 29 * <pre> 30 * public MyCache(final String id) { 31 * if (id == null) { 32 * throw new IllegalArgumentException("Cache instances require an ID"); 33 * } 34 * this.id = id; 35 * initialize(); 36 * } 37 * </pre> 38 * 39 * @author Clinton Begin 40 */ 41 42 public interface Cache { 43 44 /** 45 * @return The identifier of this cache 46 */ 47 String getId(); 48 49 /** 50 * @param key Can be any object but usually it is a {@link CacheKey} 51 * @param value The result of a select. 52 */ 53 void putObject(Object key, Object value); 54 55 /** 56 * @param key The key 57 * @return The object stored in the cache. 58 */ 59 Object getObject(Object key); 60 61 /** 62 * As of 3.3.0 this method is only called during a rollback 63 * for any previous value that was missing in the cache. 64 * This lets any blocking cache to release the lock that 65 * may have previously put on the key. 66 * A blocking cache puts a lock when a value is null 67 * and releases it when the value is back again. 68 * This way other threads will wait for the value to be 69 * available instead of hitting the database. 70 * 71 * 72 * @param key The key 73 * @return Not used 74 */ 75 Object removeObject(Object key); 76 77 /** 78 * Clears this cache instance 79 */ 80 void clear(); 81 82 /** 83 * Optional. This method is not called by the core. 84 * 85 * @return The number of elements stored in the cache (not its capacity). 86 */ 87 int getSize(); 88 89 /** 90 * Optional. As of 3.2.6 this method is no longer called by the core. 91 * 92 * Any locking needed by the cache must be provided internally by the cache provider. 93 * 94 * @return A ReadWriteLock 95 */ 96 ReadWriteLock getReadWriteLock(); 97 98 }