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.lang.Nullable; 023import org.springframework.util.Assert; 024 025/** 026 * A no operation {@link Cache} implementation suitable for disabling caching. 027 * 028 * <p>Will simply accept any items into the cache not actually storing them. 029 * 030 * @author Costin Leau 031 * @author Stephane Nicoll 032 * @since 4.3.4 033 * @see NoOpCacheManager 034 */ 035public class NoOpCache implements Cache { 036 037 private final String name; 038 039 040 /** 041 * Create a {@link NoOpCache} instance with the specified name. 042 * @param name the name of the cache 043 */ 044 public NoOpCache(String name) { 045 Assert.notNull(name, "Cache name must not be null"); 046 this.name = name; 047 } 048 049 050 @Override 051 public String getName() { 052 return this.name; 053 } 054 055 @Override 056 public Object getNativeCache() { 057 return this; 058 } 059 060 @Override 061 @Nullable 062 public ValueWrapper get(Object key) { 063 return null; 064 } 065 066 @Override 067 @Nullable 068 public <T> T get(Object key, @Nullable Class<T> type) { 069 return null; 070 } 071 072 @Override 073 @Nullable 074 public <T> T get(Object key, Callable<T> valueLoader) { 075 try { 076 return valueLoader.call(); 077 } 078 catch (Exception ex) { 079 throw new ValueRetrievalException(key, valueLoader, ex); 080 } 081 } 082 083 @Override 084 public void put(Object key, @Nullable Object value) { 085 } 086 087 @Override 088 @Nullable 089 public ValueWrapper putIfAbsent(Object key, @Nullable Object value) { 090 return null; 091 } 092 093 @Override 094 public void evict(Object key) { 095 } 096 097 @Override 098 public boolean evictIfPresent(Object key) { 099 return false; 100 } 101 102 @Override 103 public void clear() { 104 } 105 106 @Override 107 public boolean invalidate() { 108 return false; 109 } 110 111}