com.hazelcast.spring.cache.HazelcastCacheManager Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.spring.cache;
import com.hazelcast.core.DistributedObject;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.internal.util.StringUtil;
import com.hazelcast.map.IMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import static org.springframework.util.Assert.isTrue;
/**
* Spring-related {@link CacheManager} implementation for Hazelcast.
*/
@SuppressWarnings("WeakerAccess")
public class HazelcastCacheManager implements CacheManager {
/**
* Property name for hazelcast spring-cache related properties.
*/
public static final String CACHE_PROP = "hazelcast.spring.cache.prop";
private final ConcurrentMap caches = new ConcurrentHashMap<>();
private HazelcastInstance hazelcastInstance;
/**
* Default cache value retrieval timeout. Apply to all caches.
* Can be overridden setting a cache specific value to readTimeoutMap.
*/
private long defaultReadTimeout;
/**
* Holds cache specific value retrieval timeouts. Override defaultReadTimeout for specified caches.
*/
private final Map readTimeoutMap = new HashMap<>();
public HazelcastCacheManager() {
}
public HazelcastCacheManager(HazelcastInstance hazelcastInstance) {
this.hazelcastInstance = hazelcastInstance;
}
@Override
public Cache getCache(String name) {
Cache cache = caches.get(name);
if (cache == null) {
IMap