package com.nuliji.tools.shiro.cache;

import org.apache.shiro.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
 * Created by gaojie on 2017/11/7.
 */
public class RedisCacheProvider implements CacheProvider {
    private static final Logger logger = LoggerFactory.getLogger(RedisCacheProvider.class);
    private RedisManager redisManager;
    // fast lookup by name map
    private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();
    @Override
    public <k, v> Cache<k, v> getCache(String name) {
        logger.debug("get instance of RedisCache,name: " + name);
        Cache c = caches.get(name);
        if (c == null) {
// create a new cache instance
            c = new RedisCache<k, v>(name, redisManager);
// add it to the cache collection
            caches.put(name, c);
        }
        return c;
    }
    public RedisManager getRedisManager() {
        return redisManager;
    }
    public void setRedisManager(RedisManager redisManager) {
        this.redisManager = redisManager;
    }
}
