package com.nuliji.tools.shiro.cache;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
 * Created by gaojie on 2017/11/7.
 */
public class RedisManager {
    private RedisConnectionFactory factory;
    private static final Logger logger = LoggerFactory.getLogger(RedisManager.class);
    /**
     * 有效期
     */
    private int expire;
    public RedisManager() {
    }
    /**
     * get value from redis
     * @param key
     * @return
     */
    public byte[] get(byte[] key) {
        byte[] value = null;
        RedisConnection redis = factory.getConnection();
        try {
//            logger.debug("get redis:{}", new String(key));
            value = redis.get(key);
        } finally {
            redis.close();
        }
        return value;
    }
    /**
     * set
     * @param key
     * @param value
     * @return
     */
    public byte[] set(byte[] key, byte[] value) {
        RedisConnection redis = factory.getConnection();
        try {
//            logger.debug("set redis:{}", new String(key));
            redis.set(key, value);
            if (this.expire != 0) {
                redis.expire(key, this.expire);
            }
        } finally {
            redis.close();
        }
        return value;
    }
    /**
     * set
     * @param key
     * @param value
     * @param expire
     * @return
     */
    public byte[] set(byte[] key, byte[] value, int expire) {
        RedisConnection redis = factory.getConnection();
        try {
//            logger.debug("set redis expire:{}", new String(key));
            redis.set(key, value);
            if (expire != 0) {
//            logger.debug("set redis expire:{}", expire);
                redis.expire(key, expire);
            }
        } finally {
            redis.close();
        }
        return value;
    }
    /**
     * del
     * @param key
     */
    public void del(byte[] key) {
        RedisConnection redis = factory.getConnection();
        try {
//            logger.debug("delete redis:{}", new String(key));
            redis.del(key);
        } finally {
            redis.close();
        }
    }
    /**
     * flush
     */
    public void flushDb() {
        RedisConnection redis = factory.getConnection();
        try {
            if(!redis.isClosed()){
                logger.debug("flushDb redis");
                redis.flushDb();
            }
        } finally {
            redis.close();
        }
    }
    /**
     * size
     */
    public Long dbSize() {
        Long dbSize = 0L;
        RedisConnection redis = factory.getConnection();
        try {
            dbSize = redis.dbSize();
        } finally {
            redis.close();
        }
        return dbSize;
    }
    /**
     * keys
     * @param pattern
     * @return
     */
    public Set<byte[]> keys(String pattern) {
        Set<byte[]> keys = null;
        RedisConnection redis = factory.getConnection();
        try {
//            logger.debug("keys redis:{}", pattern);
            keys = redis.keys(pattern.getBytes());
        } finally {
            redis.close();
        }
        return keys;
    }

    public void setFactory(RedisConnectionFactory factory) {
        this.factory = factory;
    }

    public void setExpire(int expire) {
        this.expire = expire;
    }
}
