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.jcache;
018
019import java.util.concurrent.Callable;
020
021import javax.cache.Cache;
022import javax.cache.processor.EntryProcessor;
023import javax.cache.processor.EntryProcessorException;
024import javax.cache.processor.MutableEntry;
025
026import org.springframework.cache.support.AbstractValueAdaptingCache;
027import org.springframework.lang.Nullable;
028import org.springframework.util.Assert;
029
030/**
031 * {@link org.springframework.cache.Cache} implementation on top of a
032 * {@link Cache javax.cache.Cache} instance.
033 *
034 * <p>Note: This class has been updated for JCache 1.0, as of Spring 4.0.
035 *
036 * @author Juergen Hoeller
037 * @author Stephane Nicoll
038 * @since 3.2
039 * @see JCacheCacheManager
040 */
041public class JCacheCache extends AbstractValueAdaptingCache {
042
043        private final Cache<Object, Object> cache;
044
045
046        /**
047         * Create a {@code JCacheCache} instance.
048         * @param jcache backing JCache Cache instance
049         */
050        public JCacheCache(Cache<Object, Object> jcache) {
051                this(jcache, true);
052        }
053
054        /**
055         * Create a {@code JCacheCache} instance.
056         * @param jcache backing JCache Cache instance
057         * @param allowNullValues whether to accept and convert null values for this cache
058         */
059        public JCacheCache(Cache<Object, Object> jcache, boolean allowNullValues) {
060                super(allowNullValues);
061                Assert.notNull(jcache, "Cache must not be null");
062                this.cache = jcache;
063        }
064
065
066        @Override
067        public final String getName() {
068                return this.cache.getName();
069        }
070
071        @Override
072        public final Cache<Object, Object> getNativeCache() {
073                return this.cache;
074        }
075
076        @Override
077        @Nullable
078        protected Object lookup(Object key) {
079                return this.cache.get(key);
080        }
081
082        @Override
083        @Nullable
084        public <T> T get(Object key, Callable<T> valueLoader) {
085                try {
086                        return this.cache.invoke(key, new ValueLoaderEntryProcessor<T>(), valueLoader);
087                }
088                catch (EntryProcessorException ex) {
089                        throw new ValueRetrievalException(key, valueLoader, ex.getCause());
090                }
091        }
092
093        @Override
094        public void put(Object key, @Nullable Object value) {
095                this.cache.put(key, toStoreValue(value));
096        }
097
098        @Override
099        @Nullable
100        public ValueWrapper putIfAbsent(Object key, @Nullable Object value) {
101                boolean set = this.cache.putIfAbsent(key, toStoreValue(value));
102                return (set ? null : get(key));
103        }
104
105        @Override
106        public void evict(Object key) {
107                this.cache.remove(key);
108        }
109
110        @Override
111        public boolean evictIfPresent(Object key) {
112                return this.cache.remove(key);
113        }
114
115        @Override
116        public void clear() {
117                this.cache.removeAll();
118        }
119
120        @Override
121        public boolean invalidate() {
122                boolean notEmpty = this.cache.iterator().hasNext();
123                this.cache.removeAll();
124                return notEmpty;
125        }
126
127
128        private class ValueLoaderEntryProcessor<T> implements EntryProcessor<Object, Object, T> {
129
130                @SuppressWarnings("unchecked")
131                @Override
132                @Nullable
133                public T process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
134                        Callable<T> valueLoader = (Callable<T>) arguments[0];
135                        if (entry.exists()) {
136                                return (T) fromStoreValue(entry.getValue());
137                        }
138                        else {
139                                T value;
140                                try {
141                                        value = valueLoader.call();
142                                }
143                                catch (Exception ex) {
144                                        throw new EntryProcessorException("Value loader '" + valueLoader + "' failed " +
145                                                        "to compute value for key '" + entry.getKey() + "'", ex);
146                                }
147                                entry.setValue(toStoreValue(value));
148                                return value;
149                        }
150                }
151        }
152
153}