Introduced GuavaCacheManager as an alternative to ConcurrentMapCacheManager

This commit is contained in:
Juergen Hoeller
2013-12-10 11:59:17 +01:00
parent 4c8a7899f2
commit 6d7ce439b1
8 changed files with 455 additions and 4 deletions

View File

@@ -0,0 +1,147 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* 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.guava;
import java.io.Serializable;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.util.Assert;
/**
* Spring {@link Cache} adapter implementation on top of a
* Guava {@link com.google.common.cache.Cache} instance.
*
* <p>Requires Google Guava 12.0 or higher.
*
* @author Costin Leau
* @author Juergen Hoeller
* @since 3.1
*/
public class GuavaCache implements Cache {
private static final Object NULL_HOLDER = new NullHolder();
private final String name;
private final com.google.common.cache.Cache<Object, Object> cache;
private final boolean allowNullValues;
/**
* Create a {@link GuavaCache} instance.
* @param name the name of the cache
* @param cache backing Guava Cache instance
*/
public GuavaCache(String name, com.google.common.cache.Cache<Object, Object> cache) {
this(name, cache, true);
}
/**
* Create a {@link GuavaCache} instance.
* @param name the name of the cache
* @param cache backing Guava Cache instance
* @param allowNullValues whether to accept and convert null values for this cache
*/
public GuavaCache(String name, com.google.common.cache.Cache<Object, Object> cache, boolean allowNullValues) {
Assert.notNull(name, "Name must not be null");
Assert.notNull(cache, "Cache must not be null");
this.name = name;
this.cache = cache;
this.allowNullValues = allowNullValues;
}
@Override
public String getName() {
return this.name;
}
@Override
public com.google.common.cache.Cache<Object, Object> getNativeCache() {
return this.cache;
}
public boolean isAllowNullValues() {
return this.allowNullValues;
}
@Override
public ValueWrapper get(Object key) {
Object value = this.cache.getIfPresent(key);
return (value != null ? new SimpleValueWrapper(fromStoreValue(value)) : null);
}
@Override
@SuppressWarnings("unchecked")
public <T> T get(Object key, Class<T> type) {
Object value = fromStoreValue(this.cache.getIfPresent(key));
if (type != null && !type.isInstance(value)) {
throw new IllegalStateException("Cached value is not of required type [" + type.getName() + "]: " + value);
}
return (T) value;
}
@Override
public void put(Object key, Object value) {
this.cache.put(key, toStoreValue(value));
}
@Override
public void evict(Object key) {
this.cache.invalidate(key);
}
@Override
public void clear() {
this.cache.invalidateAll();
}
/**
* Convert the given value from the internal store to a user value
* returned from the get method (adapting {@code null}).
* @param storeValue the store value
* @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,
* to a value in the internal store (adapting {@code null}).
* @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;
}
@SuppressWarnings("serial")
private static class NullHolder implements Serializable {
}
}

View File

@@ -0,0 +1,188 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* 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.guava;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheBuilderSpec;
import com.google.common.cache.CacheLoader;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.util.Assert;
/**
* {@link CacheManager} implementation that lazily builds {@link GuavaCache}
* instances for each {@link #getCache} request. Also supports a 'static' mode
* where the set of cache names is pre-defined through {@link #setCacheNames},
* with no dynamic creation of further cache regions at runtime.
*
* <p>The configuration of the underlying cache can be fine-tuned through a
* Guava {@link CacheBuilder} or {@link CacheBuilderSpec}, passed into this
* CacheManager through {@link #setCacheBuilder}/{@link #setCacheBuilderSpec}.
* A {@link CacheBuilderSpec}-compliant expression value can also be applied
* via the {@link #setCacheSpecification "cacheSpecification"} bean property.
*
* <p>Requires Google Guava 12.0 or higher.
*
* @author Juergen Hoeller
* @since 4.0
* @see GuavaCache
*/
public class GuavaCacheManager implements CacheManager, DisposableBean {
private final ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<String, Cache>(16);
private boolean dynamic = true;
private CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
private CacheLoader<Object, Object> cacheLoader;
/**
* Construct a dynamic GuavaCacheManager,
* lazily creating cache instances as they are being requested.
*/
public GuavaCacheManager() {
}
/**
* Construct a static GuavaCacheManager,
* managing caches for the specified cache names only.
*/
public GuavaCacheManager(String... cacheNames) {
setCacheNames(Arrays.asList(cacheNames));
}
/**
* Specify the set of cache names for this CacheManager's 'static' mode.
* <p>The number of caches and their names will be fixed after a call to this method,
* with no creation of further cache regions at runtime.
*/
public void setCacheNames(Collection<String> cacheNames) {
if (cacheNames != null) {
for (String name : cacheNames) {
this.cacheMap.put(name, createGuavaCache(name));
}
this.dynamic = false;
}
}
/**
* Set the Guava CacheBuilder to use for building each individual
* {@link GuavaCache} instance.
* @see #createNativeGuavaCache
* @see com.google.common.cache.CacheBuilder#build()
*/
public void setCacheBuilder(CacheBuilder<Object, Object> cacheBuilder) {
Assert.notNull(cacheBuilder, "CacheBuilder must not be null");
this.cacheBuilder = cacheBuilder;
}
/**
* Set the Guava CacheBuilderSpec to use for building each individual
* {@link GuavaCache} instance.
* @see #createNativeGuavaCache
* @see com.google.common.cache.CacheBuilder#from(CacheBuilderSpec)
*/
public void setCacheBuilderSpec(CacheBuilderSpec cacheBuilderSpec) {
this.cacheBuilder = CacheBuilder.from(cacheBuilderSpec);
}
/**
* Set the Guava cache specification String to use for building each
* individual {@link GuavaCache} instance. The given value needs to
* comply with Guava's {@link CacheBuilderSpec} (see its javadoc).
* @see #createNativeGuavaCache
* @see com.google.common.cache.CacheBuilder#from(String)
*/
public void setCacheSpecification(String cacheSpecification) {
this.cacheBuilder = CacheBuilder.from(cacheSpecification);
}
/**
* Set the Guava CacheLoader to use for building each individual
* {@link GuavaCache} instance, turning it into a LoadingCache.
* @see #createNativeGuavaCache
* @see com.google.common.cache.CacheBuilder#build(CacheLoader)
* @see com.google.common.cache.LoadingCache
*/
public void setCacheLoader(CacheLoader<Object, Object> cacheLoader) {
this.cacheLoader = cacheLoader;
}
@Override
public Collection<String> getCacheNames() {
return Collections.unmodifiableSet(this.cacheMap.keySet());
}
@Override
public Cache getCache(String name) {
Cache cache = this.cacheMap.get(name);
if (cache == null && this.dynamic) {
synchronized (this.cacheMap) {
cache = this.cacheMap.get(name);
if (cache == null) {
cache = createGuavaCache(name);
this.cacheMap.put(name, cache);
}
}
}
return cache;
}
/**
* Create a new GuavaCache instance for the specified cache name.
* @param name the name of the cache
* @return the Spring GuavaCache adapter (or a decorator thereof)
*/
protected Cache createGuavaCache(String name) {
return new GuavaCache(name, createNativeGuavaCache(name));
}
/**
* Create a native Guava Cache instance for the specified cache name.
* @param name the name of the cache
* @return the native Guava Cache instance
*/
protected com.google.common.cache.Cache<Object, Object> createNativeGuavaCache(String name) {
if (this.cacheLoader != null) {
return this.cacheBuilder.build(this.cacheLoader);
}
else {
return this.cacheBuilder.build();
}
}
@Override
public void destroy() {
for (Cache cache : this.cacheMap.values()) {
}
}
}

View File

@@ -0,0 +1,6 @@
/**
* Support classes for the open source cache in Google's
* <a href="http://code.google.com/p/guava-libraries/">Guava</a> library,
* allowing to set up Guava caches within Spring's cache abstraction.
*/
package org.springframework.cache.guava;