admin
2021-09-24 f788607ff771a47bc60d6a86e00b3433c40f3d2c
src/main/java/com/yeshi/buwan/util/RedisManager.java
@@ -9,6 +9,7 @@
import com.google.gson.Gson;
import com.yeshi.buwan.vo.video.VideoListResultVO;
import org.yeshi.utils.SerializeUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;
@@ -17,173 +18,248 @@
@Component
public class RedisManager {
   @Resource
   private JedisPool jedisPool;
    @Resource
    private JedisPool jedisPool;
   public Jedis getJedis() {
      Jedis jedis = jedisPool.getResource();
      return jedis;
   }
    public Jedis getJedis() {
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }
   /**
    * 缓存字符串
    *
    * @param key
    * @param value
    */
   private void setString(String key, String value) {
      Jedis jedis = getJedis();
      SetParams params = new SetParams().nx().ex(60);
      jedis.set(key, value, params);
      try {
         jedis.set(key, value);
      } finally {
         jedis.close();
      }
    /**
     * 缓存字符串
     *
     * @param key
     * @param value
     */
    private void setString(String key, String value) {
        Jedis jedis = getJedis();
        SetParams params = new SetParams().nx().ex(60);
        jedis.set(key, value, params);
        try {
            jedis.set(key, value);
        } finally {
            jedis.close();
        }
   }
    }
   /**
    * 删除某个键值
    *
    * @param key
    * @param value
    */
   private void removeKey(String key) {
      Jedis jedis = getJedis();
      try {
         jedis.del(key);
      } finally {
         jedis.close();
      }
    /**
     * 删除某个键值
     *
     * @param key
     * @param value
     */
    private void removeKey(String key) {
        Jedis jedis = getJedis();
        try {
            jedis.del(key);
        } finally {
            jedis.close();
        }
   }
    }
   /**
    * 缓存字符串
    *
    * @param key
    * @param value
    * @param seconds
    *            -缓存时间(s)
    */
   private void setString(String key, String value, int seconds) {
      Jedis jedis = getJedis();
      try {
         jedis.setex(key, seconds, value);
      } finally {
         jedis.close();
      }
   }
    /**
     * 缓存字符串
     *
     * @param key
     * @param value
     * @param seconds -缓存时间(s)
     */
    private void setString(String key, String value, int seconds) {
        Jedis jedis = getJedis();
        try {
            jedis.setex(key, seconds, value);
        } finally {
            jedis.close();
        }
    }
   private String getString(String key) {
      Jedis jedis = getJedis();
      try {
         return jedis.get(key);
      } finally {
         jedis.close();
      }
   }
    private String getString(String key) {
        Jedis jedis = getJedis();
        try {
            return jedis.get(key);
        } finally {
            jedis.close();
        }
    }
   public void increase(String key) {
      Jedis jedis = getJedis();
      try {
         jedis.incr(key);
      } finally {
         jedis.close();
      }
   }
    public void increase(String key) {
        Jedis jedis = getJedis();
        try {
            jedis.incr(key);
        } finally {
            jedis.close();
        }
    }
   public void expire(String key, int seconds) {
      Jedis jedis = getJedis();
      try {
         jedis.expire(key, seconds);
      } finally {
         jedis.close();
      }
   }
   public void cacheCommonString(String key, String value, int seconds) {
      setString(key, value, seconds);
   }
    public void increase(String key, int s) {
        Jedis jedis = getJedis();
        try {
            jedis.incr(key);
            jedis.expire(key, s);
        } finally {
            jedis.close();
        }
    }
   public void cacheCommonString(String key, String value) {
      setString(key, value);
   }
    public void expire(String key, int seconds) {
        Jedis jedis = getJedis();
        try {
            jedis.expire(key, seconds);
        } finally {
            jedis.close();
        }
    }
   public String getCommonString(String key) {
      return getString(key);
   }
    public void cacheCommonString(String key, String value, int seconds) {
        setString(key, value, seconds);
    }
   public void removeCommonString(String key) {
      removeKey(key);
   }
    public void cacheCommonString(String key, String value) {
        setString(key, value);
    }
   /**
    * 保存视频数据
    *
    * @param key
    * @param videoData
    */
   public void saveVideoList(String key, VideoListResultVO videoData) {
      Gson gson = new Gson();
      cacheCommonString(key, gson.toJson(videoData), 60 * 60 * 24);
   }
    public String getCommonString(String key) {
        return getString(key);
    }
   /**
    * 获取视频列表
    *
    * @param key
    * @return
    */
   public VideoListResultVO getVideoList(String key) {
      String st = getCommonString(key);
      if (!StringUtil.isNullOrEmpty(st)) {
         return new Gson().fromJson(st, VideoListResultVO.class);
      }
      return null;
   }
    public void removeCommonString(String key) {
        removeKey(key);
    }
   /**
    * 保存列表
    *
    * @param clazzList
    * @param key
    * @param seconds
    */
   public <T> T saveObjList(List<T> clazzList, String key, Integer seconds) {
      if (clazzList == null)
         return null;
      String value = new Gson().toJson(clazzList);
      if (seconds != null)
         cacheCommonString(key, value, seconds);
      else
         cacheCommonString(key, value);
      return null;
   }
    public void remove(String key) {
        removeKey(key);
    }
   /**
    * 获取对象
    *
    * @param clazz
    * @param key
    * @return
    */
   public Class<?> getObj(Class<?> clazz, String key) {
      String value = getCommonString(key);
      return (Class<?>) new Gson().fromJson(value, clazz);
   }
    /**
     * 保存视频数据
     *
     * @param key
     * @param videoData
     */
    public void saveVideoList(String key, VideoListResultVO videoData) {
        Gson gson = new Gson();
        cacheCommonString(key, gson.toJson(videoData), 60 * 60 * 8);
    }
   /**
    * 获取列表
    *
    * @param clazz
    * @param key
    * @return
    */
   public <T> List<T> getObjList(Class<T> clazz, String key) {
      String value = getCommonString(key);
      return JsonUtil.jsonToList(value, clazz);
   }
    /**
     * 获取视频列表
     *
     * @param key
     * @return
     */
    public VideoListResultVO getVideoList(String key) {
        String st = getCommonString(key);
        if (!StringUtil.isNullOrEmpty(st)) {
            return new Gson().fromJson(st, VideoListResultVO.class);
        }
        return null;
    }
    /**
     * 保存列表
     *
     * @param clazzList
     * @param key
     * @param seconds
     */
    public <T> T saveObjList(List<T> clazzList, String key, Integer seconds) {
        if (clazzList == null)
            return null;
        String value = new Gson().toJson(clazzList);
        if (seconds != null)
            cacheCommonString(key, value, seconds);
        else
            cacheCommonString(key, value);
        return null;
    }
    /**
     * 获取对象
     *
     * @param clazz
     * @param key
     * @return
     */
    public Class<?> getObj(Class<?> clazz, String key) {
        String value = getCommonString(key);
        return (Class<?>) new Gson().fromJson(value, clazz);
    }
    /**
     * 获取列表
     *
     * @param clazz
     * @param key
     * @return
     */
    public <T> List<T> getObjList(Class<T> clazz, String key) {
        String value = getCommonString(key);
        return JsonUtil.jsonToList(value, clazz);
    }
    public boolean isSmsFrequencyLimit(String phone, Integer type) {
        if (type == null) {
            type = 0;
        }
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + type);
        String value = getCommonString(key);
        if (StringUtil.isNullOrEmpty(value))
            return false;
        else
            return true;
    }
    /**
     * 从redis缓存中查询,反序列化
     *
     * @param redisKey
     * @return
     */
    public Object getDataFromRedis(String redisKey) {
        //查询
        Jedis jedis = jedisPool.getResource();
        try {
            byte[] result = jedis.get(redisKey.getBytes());
            //如果查询没有为空
            if (null == result) {
                return null;
            }
            //查询到了,反序列化
            return SerializeUtil.unSerialize(result);
        } finally {
            jedis.close();
        }
    }
    /**
     * 将数据库中查询到的数据放入redis
     *
     * @param redisKey
     * @param obj
     */
    public void setDataToRedis(String redisKey, Object obj, Integer seconds) {
        //序列化
        byte[] bytes = SerializeUtil.serialize(obj);
        SetParams params = null;
        if (seconds != null) {
            params = new SetParams().nx().ex(seconds);
        }
        //存入redis
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.set(redisKey.getBytes(), bytes, params);
        } finally {
            jedis.close();
        }
    }
}