RedisService.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * *************************************************
  3. * Copyright (c) 2018 QMTH. All Rights Reserved.
  4. * Created by Deason on 2018-07-31 17:53:48.
  5. * *************************************************
  6. */
  7. package cn.com.qmth.examcloud.app.service;
  8. import cn.com.qmth.examcloud.app.core.exception.ApiException;
  9. import cn.com.qmth.examcloud.app.core.utils.JsonMapper;
  10. import cn.com.qmth.examcloud.app.core.utils.ThreadUtils;
  11. import cn.com.qmth.examcloud.app.model.Constants;
  12. import cn.com.qmth.examcloud.app.model.UserToken;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.data.redis.core.RedisTemplate;
  17. import org.springframework.data.redis.core.StringRedisTemplate;
  18. import org.springframework.stereotype.Service;
  19. import java.util.concurrent.TimeUnit;
  20. import static cn.com.qmth.examcloud.app.model.Constants.APP_SESSION_USER_KEY_PREFIX;
  21. /**
  22. * Redis接口服务类
  23. *
  24. * @author: fengdesheng
  25. * @since: 2018/7/16
  26. */
  27. @Service
  28. public class RedisService {
  29. private static Logger log = LoggerFactory.getLogger(RedisService.class);
  30. @Autowired
  31. private StringRedisTemplate stringRedisTemplate;
  32. @Autowired
  33. private RedisTemplate<Object, Object> redisTemplate;
  34. public void set(String key, String value) {
  35. stringRedisTemplate.opsForValue().set(key, value);
  36. }
  37. public void set(String key, String value, long seconds) {
  38. stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
  39. }
  40. public String get(String key) {
  41. return stringRedisTemplate.opsForValue().get(key);
  42. }
  43. public boolean exist(String key) {
  44. return stringRedisTemplate.hasKey(key);
  45. }
  46. public void delete(String key) {
  47. stringRedisTemplate.delete(key);
  48. }
  49. /**
  50. * 缓存用户登录信息
  51. */
  52. public void cacheUserToken(String key, UserToken value, long seconds) {
  53. if (key == null) {
  54. throw new ApiException("Key must be not null.");
  55. }
  56. this.set(APP_SESSION_USER_KEY_PREFIX + key, new JsonMapper().toJson(value), seconds);
  57. }
  58. /**
  59. * 获取缓存中的用户登录信息
  60. */
  61. public UserToken getUserToken(String key) {
  62. if (key == null) {
  63. throw new ApiException("Key must be not null.");
  64. }
  65. String value = this.get(APP_SESSION_USER_KEY_PREFIX + key);
  66. if (value != null) {
  67. return new JsonMapper().fromJson(value, UserToken.class);
  68. }
  69. return null;
  70. }
  71. /**
  72. * 初始化内部接口鉴权
  73. */
  74. public void initTraceRequest() {
  75. String key = "C_" + ThreadUtils.getTraceID();
  76. Long millis = System.currentTimeMillis();
  77. this.set(key, millis.toString(), 10);
  78. }
  79. /**
  80. * 获取平台端的默认过期时间(秒)
  81. */
  82. public int getSessionTimeout() {
  83. try {
  84. String timeout = this.get(Constants.PLATFORM_SESSION_TIMEOUT_KEY);
  85. if (timeout != null) {
  86. return Integer.parseInt(timeout);
  87. }
  88. } catch (Exception e) {
  89. //ignore
  90. }
  91. return Constants.PLATFORM_SESSION_EXPIRE_TIME;
  92. }
  93. }