001/* 002 * Copyright 2002-2020 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.cache.support; 018 019import java.util.concurrent.Callable; 020 021import org.springframework.cache.Cache; 022import org.springframework.util.Assert; 023 024/** 025 * A no operation {@link Cache} implementation suitable for disabling caching. 026 * 027 * <p>Will simply accept any items into the cache not actually storing them. 028 * 029 * @author Costin Leau 030 * @author Stephane Nicoll 031 * @since 4.3.4 032 * @see NoOpCacheManager 033 */ 034public class NoOpCache implements Cache { 035 036 private final String name; 037 038 039 /** 040 * Create a {@link NoOpCache} instance with the specified name. 041 * @param name the name of the cache 042 */ 043 public NoOpCache(String name) { 044 Assert.notNull(name, "Cache name must not be null"); 045 this.name = name; 046 } 047 048 049 @Override 050 public String getName() { 051 return this.name; 052 } 053 054 @Override 055 public Object getNativeCache() { 056 return null; 057 } 058 059 @Override 060 public ValueWrapper get(Object key) { 061 return null; 062 } 063 064 @Override 065 public <T> T get(Object key, Class<T> type) { 066 return null; 067 } 068 069 @Override 070 public <T> T get(Object key, Callable<T> valueLoader) { 071 try { 072 return valueLoader.call(); 073 } 074 catch (Exception ex) { 075 throw new ValueRetrievalException(key, valueLoader, ex); 076 } 077 } 078 079 @Override 080 public void put(Object key, Object value) { 081 } 082 083 @Override 084 public ValueWrapper putIfAbsent(Object key, Object value) { 085 return null; 086 } 087 088 @Override 089 public void evict(Object key) { 090 } 091 092 @Override 093 public void clear() { 094 } 095 096}