package com.bootdo.common.redis.shiro;

/**
 * @author bootdo 1992lcg@163.com
 * @version V1.0
 */


import java.util.Set;

import com.bootdo.common.utils.RedisUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 *
 */
@Component
public class RedisManager {
    @Resource
    private RedisUtil redisUtil;
    // 0 - never expire
    private int expire = 0;

    public RedisManager() {

    }
    /**
     * 获得byte[]型的key
     * @param key
     * @return
     */
    private byte[] getByteKey(Object key){
        if(key instanceof String){
            return ((String)key).getBytes();
        }else{
            return SerializeUtils.serialize(key);
        }
    }

    /**
     * get value from redis
     *
     * @param key
     * @return
     */
    public byte[] get(byte[] key) {
        byte[] value = redisUtil.get(key);
        return value;
    }

    /**
     * set
     *
     * @param key
     * @param value
     * @return
     */
    public byte[] set(byte[] key, byte[] value) {
        redisUtil.set(key, value,this.expire);
        if (this.expire != 0) {
            redisUtil.set(key, value,this.expire);
        } else {
            redisUtil.set(key, value);
        }
        return value;
    }

    /**
     * set
     *
     * @param key
     * @param value
     * @param expire
     * @return
     */
    public byte[] set(byte[] key, byte[] value, int expire) {
        redisUtil.set(key, value,expire);
        return value;
    }

    /**
     * del
     *
     * @param key
     */
    public void del(byte[] key) {
        redisUtil.del(key);
    }

    /**
     * flush
     */
    public void flushDB() {
        //do nothing
    }

    /**
     * size
     */
    public Long dbSize() {
        Long dbSize = redisUtil.dbSize();
        return dbSize;
    }

    /**
     * keys
     *
     * @return
     */
    public Set<byte[]> keys(String pattern) {
        Set<byte[]> keys = redisUtil.keys(pattern.getBytes());
        return keys;
    }


    public int getExpire() {
        return expire;
    }

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


}
