Cache.get(key, type) should also work in case of null value found in cache

Issue: SPR-11567
This commit is contained in:
Juergen Hoeller
2014-03-18 00:50:59 +01:00
parent 57af56aeeb
commit 38e7c4776b
7 changed files with 36 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -24,6 +24,7 @@ package org.springframework.cache;
* cache methods that return {@code null}).
*
* @author Costin Leau
* @author Juergen Hoeller
* @since 3.1
*/
public interface Cache {
@@ -59,11 +60,14 @@ public interface Cache {
* 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
* @param type the required type of the returned value (may be
* {@code null} to bypass a type check; in case of a {@code null}
* value found in the cache, the specified type is irrelevant)
* @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)
* @since 4.0
*/
<T> T get(Object key, Class<T> type);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -110,7 +110,7 @@ public class ConcurrentMapCache implements Cache {
@SuppressWarnings("unchecked")
public <T> T get(Object key, Class<T> type) {
Object value = fromStoreValue(this.store.get(key));
if (type != null && !type.isInstance(value)) {
if (value != null && type != null && !type.isInstance(value)) {
throw new IllegalStateException("Cached value is not of required type [" + type.getName() + "]: " + value);
}
return (T) value;