001/*
002 * Copyright 2002-2017 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.interceptor;
018
019import org.springframework.cache.Cache;
020import org.springframework.util.Assert;
021
022/**
023 * A base component for invoking {@link Cache} operations and using a
024 * configurable {@link CacheErrorHandler} when an exception occurs.
025 *
026 * @author Stephane Nicoll
027 * @since 4.1
028 * @see org.springframework.cache.interceptor.CacheErrorHandler
029 */
030public abstract class AbstractCacheInvoker {
031
032        private CacheErrorHandler errorHandler;
033
034
035        protected AbstractCacheInvoker() {
036                this(new SimpleCacheErrorHandler());
037        }
038
039        protected AbstractCacheInvoker(CacheErrorHandler errorHandler) {
040                Assert.notNull(errorHandler, "ErrorHandler must not be null");
041                this.errorHandler = errorHandler;
042        }
043
044
045        /**
046         * Set the {@link CacheErrorHandler} instance to use to handle errors
047         * thrown by the cache provider. By default, a {@link SimpleCacheErrorHandler}
048         * is used who throws any exception as is.
049         */
050        public void setErrorHandler(CacheErrorHandler errorHandler) {
051                this.errorHandler = errorHandler;
052        }
053
054        /**
055         * Return the {@link CacheErrorHandler} to use.
056         */
057        public CacheErrorHandler getErrorHandler() {
058                return this.errorHandler;
059        }
060
061
062        /**
063         * Execute {@link Cache#get(Object)} on the specified {@link Cache} and
064         * invoke the error handler if an exception occurs. Return {@code null}
065         * if the handler does not throw any exception, which simulates a cache
066         * miss in case of error.
067         * @see Cache#get(Object)
068         */
069        protected Cache.ValueWrapper doGet(Cache cache, Object key) {
070                try {
071                        return cache.get(key);
072                }
073                catch (RuntimeException ex) {
074                        getErrorHandler().handleCacheGetError(ex, cache, key);
075                        return null;  // If the exception is handled, return a cache miss
076                }
077        }
078
079        /**
080         * Execute {@link Cache#put(Object, Object)} on the specified {@link Cache}
081         * and invoke the error handler if an exception occurs.
082         */
083        protected void doPut(Cache cache, Object key, Object result) {
084                try {
085                        cache.put(key, result);
086                }
087                catch (RuntimeException ex) {
088                        getErrorHandler().handleCachePutError(ex, cache, key, result);
089                }
090        }
091
092        /**
093         * Execute {@link Cache#evict(Object)} on the specified {@link Cache} and
094         * invoke the error handler if an exception occurs.
095         */
096        protected void doEvict(Cache cache, Object key) {
097                try {
098                        cache.evict(key);
099                }
100                catch (RuntimeException ex) {
101                        getErrorHandler().handleCacheEvictError(ex, cache, key);
102                }
103        }
104
105        /**
106         * Execute {@link Cache#clear()} on the specified {@link Cache} and
107         * invoke the error handler if an exception occurs.
108         */
109        protected void doClear(Cache cache) {
110                try {
111                        cache.clear();
112                }
113                catch (RuntimeException ex) {
114                        getErrorHandler().handleCacheClearError(ex, cache);
115                }
116        }
117
118}