/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.helper;

import com.ifish.util.IfishUtil;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * redis缓存服务器所有接口方法
 *
 * @author Administrator
 */
@Component
public class RedisHelper implements RedisHelperI {

    @Autowired
    private StringRedisTemplate redis;

    /*
    *获取缓存值
     */
    @Override
    public String getRedis(String key) {
        try {
            String result = redis.opsForValue().get(key.toLowerCase());
            return result;
        } catch (Exception e) {
            return null;
        }
    }

    /*
    *设置缓存(默认时间为1天)
     */
    @Override
    public void setRedis(String key, String value) {
        if (value == null) {
            return;
        }
        try {
            redis.opsForValue().set(key.toLowerCase(), value, IfishUtil.CacheTime_DAYS, TimeUnit.DAYS);
        } catch (Exception e) {
        }
    }

    /*设置自定义时效缓存
    *key 缓存名称，value缓存值（string）,time(保存时间),unit(时间类型)
     */
    @Override
    public void setRedis(String key, String value, int time, TimeUnit unit) {
        if (value == null) {
            return;
        }
        try {
            redis.opsForValue().set(key.toLowerCase(), value, time, unit);
        } catch (Exception e) {
        }
    }

    /*
    *设置按天时效缓存
     */
    @Override
    public void setRedisToDAYS(String key, String value) {
        if (value == null) {
            return;
        }
        try {
            redis.opsForValue().set(key.toLowerCase(), value, IfishUtil.CacheTime_DAYS, TimeUnit.DAYS);
        } catch (Exception e) {
        }
    }

    /*
    *设置按时时效缓存
     */
    @Override
    public void setRedisToHOURS(String key, String value) {
        if (value == null) {
            return;
        }
        try {
            redis.opsForValue().set(key.toLowerCase(), value, IfishUtil.CacheTime_HOURS, TimeUnit.HOURS);
        } catch (Exception e) {
        }
    }

    /*
    *设置按分时效缓存
     */
    @Override
    public void setRedisToMINUTES(String key, String value) {
        if (value == null) {
            return;
        }
        try {
            redis.opsForValue().set(key.toLowerCase(), value, IfishUtil.CacheTime_MINUTES, TimeUnit.MINUTES);
        } catch (Exception e) {
        }
    }

    /*
    *设置按秒时效缓存
     */
    @Override
    public void setRedisToSECONDS(String key, String value) {
        if (value == null) {
            return;
        }
        try {
            redis.opsForValue().set(key.toLowerCase(), value, IfishUtil.CacheTime_SECONDS, TimeUnit.SECONDS);
        } catch (Exception e) {
        }
    }

    /*
    *删除缓存值
     */
    @Override
    public void deleteRedis(String key) {
        try {
            redis.delete(key.toLowerCase());
        } catch (Exception e) {
        }
    }

    /*
    *删除带前缀的批量缓存
    *tagkey 缓存前缀
    *如省份缓存 province:10，前缀为province
     */
    @Override
    public void delRedisByTagKey(String tagkey) {
        try {
            Set<String> set = redis.keys(tagkey.toLowerCase() + "*");
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                String keyStr = it.next();
                redis.delete(keyStr);
            }
        } catch (Exception e) {
        }
    }

    /*
    * 删除当前缓存数据库所有的key
    * 排除系数设置缓存
     */
    @Override
    public void deleteDBRedis() {
        try {
            Set<String> set = redis.keys("*");
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                String keyStr = it.next();
                //排除系数设置缓存
                if (!keyStr.contains("recommendsetting:")) {
                    redis.delete(keyStr);
                }
            }
        } catch (Exception e) {
        }
    }
}
