ConcurrentMapCache.java 5.1 KB
Newer Older
1
/*
2
 * Copyright 2002-2014 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.springframework.cache.concurrent;

import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

23 24
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
25
import org.springframework.util.Assert;
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/**
 * Simple {@link Cache} implementation based on the core JDK
 * {@code java.util.concurrent} package.
 *
 * <p>Useful for testing or simple caching scenarios, typically in combination
 * with {@link org.springframework.cache.support.SimpleCacheManager} or
 * dynamically through {@link ConcurrentMapCacheManager}.
 *
 * <p><b>Note:</b> As {@link ConcurrentHashMap} (the default implementation used)
 * does not allow for {@code null} values to be stored, this class will replace
 * them with a predefined internal object. This behavior can be changed through the
 * {@link #ConcurrentMapCache(String, ConcurrentMap, boolean)} constructor.
 *
 * @author Costin Leau
 * @author Juergen Hoeller
 * @since 3.1
 */
public class ConcurrentMapCache implements Cache {

	private static final Object NULL_HOLDER = new NullHolder();

	private final String name;

	private final ConcurrentMap<Object, Object> store;

	private final boolean allowNullValues;


	/**
	 * Create a new ConcurrentMapCache with the specified name.
	 * @param name the name of the cache
	 */
	public ConcurrentMapCache(String name) {
60
		this(name, new ConcurrentHashMap<Object, Object>(256), true);
61 62 63 64 65
	}

	/**
	 * Create a new ConcurrentMapCache with the specified name.
	 * @param name the name of the cache
J
Juergen Hoeller 已提交
66 67
	 * @param allowNullValues whether to accept and convert {@code null}
	 * values for this cache
68 69
	 */
	public ConcurrentMapCache(String name, boolean allowNullValues) {
70
		this(name, new ConcurrentHashMap<Object, Object>(256), allowNullValues);
71 72 73 74
	}

	/**
	 * Create a new ConcurrentMapCache with the specified name and the
J
Juergen Hoeller 已提交
75
	 * given internal {@link ConcurrentMap} to use.
76 77
	 * @param name the name of the cache
	 * @param store the ConcurrentMap to use as an internal store
78
	 * @param allowNullValues whether to allow {@code null} values
79 80 81
	 * (adapting them to an internal null holder value)
	 */
	public ConcurrentMapCache(String name, ConcurrentMap<Object, Object> store, boolean allowNullValues) {
82 83
		Assert.notNull(name, "Name must not be null");
		Assert.notNull(store, "Store must not be null");
84 85 86 87 88 89
		this.name = name;
		this.store = store;
		this.allowNullValues = allowNullValues;
	}


90
	@Override
J
Juergen Hoeller 已提交
91
	public final String getName() {
92 93 94
		return this.name;
	}

95
	@Override
J
Juergen Hoeller 已提交
96
	public final ConcurrentMap<Object, Object> getNativeCache() {
97 98 99
		return this.store;
	}

J
Juergen Hoeller 已提交
100
	public final boolean isAllowNullValues() {
101 102 103
		return this.allowNullValues;
	}

104
	@Override
105 106
	public ValueWrapper get(Object key) {
		Object value = this.store.get(key);
107
		return toWrapper(value);
108 109
	}

110 111 112 113
	@Override
	@SuppressWarnings("unchecked")
	public <T> T get(Object key, Class<T> type) {
		Object value = fromStoreValue(this.store.get(key));
114
		if (value != null && type != null && !type.isInstance(value)) {
115 116 117 118 119
			throw new IllegalStateException("Cached value is not of required type [" + type.getName() + "]: " + value);
		}
		return (T) value;
	}

120
	@Override
121 122 123 124
	public void put(Object key, Object value) {
		this.store.put(key, toStoreValue(value));
	}

125 126 127 128 129 130
	@Override
	public ValueWrapper putIfAbsent(Object key, Object value) {
		Object existing = this.store.putIfAbsent(key, value);
		return toWrapper(existing);
	}

131
	@Override
132 133 134 135
	public void evict(Object key) {
		this.store.remove(key);
	}

136
	@Override
137 138 139 140 141 142 143
	public void clear() {
		this.store.clear();
	}


	/**
	 * Convert the given value from the internal store to a user value
144
	 * returned from the get method (adapting {@code null}).
S
Stevo Slavic 已提交
145
	 * @param storeValue the store value
146 147 148 149 150 151 152 153 154 155 156
	 * @return the value to return to the user
	 */
	protected Object fromStoreValue(Object storeValue) {
		if (this.allowNullValues && storeValue == NULL_HOLDER) {
			return null;
		}
		return storeValue;
	}

	/**
	 * Convert the given user value, as passed into the put method,
157
	 * to a value in the internal store (adapting {@code null}).
158 159 160 161 162 163 164 165 166 167
	 * @param userValue the given user value
	 * @return the value to store
	 */
	protected Object toStoreValue(Object userValue) {
		if (this.allowNullValues && userValue == null) {
			return NULL_HOLDER;
		}
		return userValue;
	}

168 169 170
	private ValueWrapper toWrapper(Object value) {
		return (value != null ? new SimpleValueWrapper(fromStoreValue(value)) : null);
	}
171

P
Phillip Webb 已提交
172
	@SuppressWarnings("serial")
173 174 175 176
	private static class NullHolder implements Serializable {
	}

}