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

com.github.kaizen4j.shiro.cache.ShiroRedisCacheManager Maven / Gradle / Ivy

package com.github.kaizen4j.shiro.cache;

import java.time.Duration;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author liuguowen
 */
public class ShiroRedisCacheManager implements CacheManager {

    private static final Logger logger = LoggerFactory.getLogger(ShiroRedisCacheManager.class);

    public static final long DEFAULT_CACHE_EXPIRE_MILLIS = Duration.ofMinutes(30).toMillis();

    private static final String DEFAULT_CACHE_KEY_PREFIX = "shiro:%s:%s";

    private RedisTemplate redisTemplate;

    private long expireMillis;

    private String appName;

    public ShiroRedisCacheManager(RedisTemplate redisTemplate, String appName, long expireMillis) {
        this.redisTemplate = redisTemplate;
        this.appName = appName;
        this.expireMillis = (expireMillis <= 0) ? DEFAULT_CACHE_EXPIRE_MILLIS : expireMillis;
    }

    @Override
    public  Cache getCache(String s) {
        StringBuilder cacheKeyPrefix = new StringBuilder();
        cacheKeyPrefix.append(appName);
        cacheKeyPrefix.append(":");
        cacheKeyPrefix.append(DEFAULT_CACHE_KEY_PREFIX);
        return new ShiroRedisCache<>(redisTemplate, cacheKeyPrefix.toString(), expireMillis);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy