Added get(key, type) method to Cache interface

This new get variant not only allows for generically specifying the required value type; it also skips the ValueWrapper that the standard get method returns. Note that it is not possible to differentiate between non-existing cache entries and cached null values that way; for that purpose, the standard get variant needs to be used.

Issue: SPR-11061
This commit is contained in:
Juergen Hoeller
2013-11-04 14:31:41 +01:00
parent b093b84954
commit 50d3f71923
9 changed files with 81 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* 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.
@@ -39,14 +39,34 @@ public interface Cache {
Object getNativeCache();
/**
* Return the value to which this cache maps the specified key. Returns
* {@code null} if the cache contains no mapping for this key.
* @param key key whose associated value is to be returned.
* Return the value to which this cache maps the specified key.
* <p>Returns {@code null} if the cache contains no mapping for this key;
* otherwise, the cached value (which may be {@code null} itself) will
* be returned in a {@link ValueWrapper}.
* @param key the key whose associated value is to be returned
* @return the value to which this cache maps the specified key,
* or {@code null} if the cache contains no mapping for this key
* contained within a {@link ValueWrapper} which may also hold
* a cached {@code null} value. A straight {@code null} being
* returned means that the cache contains no mapping for this key.
* @see #get(Object, Class)
*/
ValueWrapper get(Object key);
/**
* Return the value to which this cache maps the specified key,
* generically specifying a type that return value will be cast to.
* <p>Note: This variant of {@code get} does not allow for differentiating
* between a cached {@code null} value and no cache entry found at all.
* Use the standard {@link #get(Object)} variant for that purpose instead.
* @param key the key whose associated value is to be returned
* @param type the required type of the returned value
* @return the value to which this cache maps the specified key
* (which may be {@code null} itself), or also {@code null} if
* the cache contains no mapping for this key
* @see #get(Object)
*/
<T> T get(Object key, Class<T> type);
/**
* Associate the specified value with the specified key in this cache.
* <p>If the cache previously contained a mapping for this key, the old

View File

@@ -16,13 +16,13 @@
package org.springframework.cache.concurrent;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
/**
* Simple {@link Cache} implementation based on the core JDK
* {@code java.util.concurrent} package.
@@ -103,6 +103,16 @@ public class ConcurrentMapCache implements Cache {
return (value != null ? new SimpleValueWrapper(fromStoreValue(value)) : null);
}
@Override
@SuppressWarnings("unchecked")
public <T> T get(Object key, Class<T> type) {
Object value = fromStoreValue(this.store.get(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.store.put(key, toStoreValue(value));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* 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.
@@ -93,6 +93,11 @@ public class NoOpCacheManager implements CacheManager {
return null;
}
@Override
public <T> T get(Object key, Class<T> type) {
return null;
}
@Override
public String getName() {
return this.name;