All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.kduck.core.cache.CacheManagerWrapper Maven / Gradle / Ivy

Go to download

The core of the K-Duck development framework encompasses all the featured components of the framework.

There is a newer version: 2.0.0
Show newest version
package cn.kduck.core.cache;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * @author LiuHG
 */
@Component
public class CacheManagerWrapper {

    private final static String DEFAULT_CACHE_NAME = "default";

    private final CacheManager cacheManager;
    private final CacheExpiredHandler cacheExpiredHandler;

    private ObjectMapper objectMapper = new ObjectMapper();

    @Value("${kduck.cache.cache-name-prefix:}")
    private String cacheNamePrefix;

    @Autowired
    public CacheManagerWrapper(CacheManager cacheManager,CacheExpiredHandler cacheExpiredHandler){
        this.cacheManager = cacheManager;
        this.cacheExpiredHandler = cacheExpiredHandler;
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public CacheWrapper getCache(String name) {
        if(!StringUtils.hasText(name)){
            name = DEFAULT_CACHE_NAME;
        }

        if(cacheNamePrefix.length() > 0){
            name = cacheNamePrefix + "::" + name;
        }

        return new CacheWrapper(cacheManager.getCache(name),objectMapper,cacheExpiredHandler);
    }

    public Collection getCacheNames() {
        return cacheManager.getCacheNames();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy