content: package io.github.xxyopen.novel.resource.manager.redis;
  
  import io.github.xxyopen.novel.common.constant.CacheConsts;
  import io.github.xxyopen.novel.resource.util.ImgVerifyCodeUtils;
  import lombok.RequiredArgsConstructor;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.data.redis.core.StringRedisTemplate;
  import org.springframework.stereotype.Component;
  
  import java.io.IOException;
  import java.time.Duration;
  import java.util.Objects;
  
  /**
   * 验证码 管理类
   *
   * @author xiongxiaoyang
   * @date 2022/5/12
   */
  @Component
  @RequiredArgsConstructor
  @Slf4j
  public class VerifyCodeManager {
  
      private final StringRedisTemplate stringRedisTemplate;
  
      /**
       * 生成图形验证码，并放入 Redis 中
       */
      public String genImgVerifyCode(String sessionId) throws IOException {
          String verifyCode = ImgVerifyCodeUtils.getRandomVerifyCode(4);
          String img = ImgVerifyCodeUtils.genVerifyCodeImg(verifyCode);
          stringRedisTemplate.opsForValue().set(CacheConsts.IMG_VERIFY_CODE_CACHE_KEY + sessionId,
              verifyCode, Duration.ofMinutes(5));
          return img;
      }
  
  }
  