content: package com.ncst.seckill.service.impl;
  
  import com.ncst.seckill.key.KeyPrefix;
  import com.ncst.seckill.service.IRedisService;
  import com.ncst.seckill.util.JsonUtils;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import redis.clients.jedis.Jedis;
  import redis.clients.jedis.JedisPool;
  /**
   * @Date 2020/10/14 12:30
   * @Author by LSY
   * @Description
   */
  @Service
  public class RedisServiceImpl implements IRedisService {
  
      @Autowired
      JedisPool jedisPool;
  
      /**
       * 获取当个对象
       */
      @Override
      public <T> T get(KeyPrefix prefix, String key, Class<T> clazz) {
          Jedis jedis = null;
          try {
              jedis = jedisPool.getResource();
              //生成真正的key
              String realKey = prefix.getPrefix() + key;
              String str = jedis.get(realKey);
              T t = JsonUtils.stringToBean(str, clazz);
              return t;
          } finally {
              returnToPool(jedis);
          }
      }
  
      /**
       * 删除
       * */
      @Override
      public boolean delete(KeyPrefix prefix, String key) {
          Jedis jedis = null;
          try {
              jedis =  jedisPool.getResource();
              //生成真正的key
              String realKey  = prefix.getPrefix() + key;
              long ret =  jedis.del(realKey);
              return ret > 0;
          }finally {
              returnToPool(jedis);
          }
      }
  
      /**
       * 增加值
       * */
      @Override
      public <T> Long incr(KeyPrefix prefix, String key) {
          Jedis jedis = null;
          try {
              jedis =  jedisPool.getResource();
              //生成真正的key
              String realKey  = prefix.getPrefix() + key;
              return  jedis.incr(realKey);
          }finally {
              returnToPool(jedis);
          }
      }
  
      @Override
      public boolean delete(KeyPrefix prefix) {
          Jedis jedis = null;
          try {
              jedis =  jedisPool.getResource();
              //生成真正的key
              String realKey  = prefix.getPrefix() ;
              long ret =  jedis.del(realKey);
              return ret > 0;
          }finally {
              returnToPool(jedis);
          }
      }
  
  
      /**
       * 设置对象
       */
      @Override
      public <T> boolean set(KeyPrefix prefix, String key, T value) {
          Jedis jedis = null;
          try {
              jedis = jedisPool.getResource();
              String str = JsonUtils.beanToString(value);
              if (str == null || str.length() <= 0) {
                  return false;
              }
              //生成真正的key
              String realKey = prefix.getPrefix() + key;
              int seconds = prefix.expireSeconds();
              if (seconds <= 0) {
                  jedis.set(realKey, str);
              } else {
                  jedis.setex(realKey, seconds, str);
              }
              return true;
          } finally {
              returnToPool(jedis);
          }
      }
  
      /**
       * 判断key是否存在
       * */
      @Override
      public <T> boolean exists(KeyPrefix prefix, String key) {
          Jedis jedis = null;
          try {
              jedis =  jedisPool.getResource();
              //生成真正的key
              String realKey  = prefix.getPrefix() + key;
              return  jedis.exists(realKey);
          }finally {
              returnToPool(jedis);
          }
      }
  
      /**
       * 减少值
       * */
      @Override
      public <T> Long decr(KeyPrefix prefix, String key) {
          Jedis jedis = null;
          try {
              jedis =  jedisPool.getResource();
              //生成真正的key
              String realKey  = prefix.getPrefix() + key;
              return  jedis.decr(realKey);
          }finally {
              returnToPool(jedis);
          }
      }
  
      private void returnToPool(Jedis jedis) {
          if (jedis != null) {
              jedis.close();
          }
      }
  
  }
  