RedisService.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package com.zswl.dataservice.service.base;
  2. import com.zswl.dataservice.utils.exception.ServiceException;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.data.redis.core.StringRedisTemplate;
  5. import org.springframework.stereotype.Service;
  6. import java.util.concurrent.TimeUnit;
  7. @Service
  8. public class RedisService {
  9. @Autowired
  10. private StringRedisTemplate template;
  11. public void addExpireToken(String loginName,String token,long tokenExpirePeriod)
  12. {
  13. try
  14. {
  15. String[] tokens=token.split("\\.");
  16. //template.boundValueOps(tokenKey).set(loginName, 10000, TimeUnit.MILLISECONDS);
  17. template.opsForValue().set(tokens[1],loginName, tokenExpirePeriod, TimeUnit.MILLISECONDS);
  18. }
  19. catch(Exception e)
  20. {
  21. throw new ServiceException("系统故障,请联系服务商");
  22. }
  23. }
  24. public void removeExpireToken(String loginName,String token)
  25. {
  26. try
  27. {
  28. String[] tokens=token.split("\\.");
  29. //template.boundValueOps(tokenKey).set(loginName, 10000, TimeUnit.MILLISECONDS);
  30. template.delete(tokens[1]);
  31. }
  32. catch(Exception e)
  33. {
  34. throw new ServiceException("系统故障,请联系服务商");
  35. }
  36. }
  37. public boolean verifyExpireJwtToken(String loginName,String jwtToken)
  38. {
  39. try
  40. {
  41. String[] tokens=jwtToken.split("\\.");
  42. //template.boundValueOps(tokenKey).set(loginName, 10000, TimeUnit.MILLISECONDS);
  43. return template.hasKey(tokens[1]);
  44. }
  45. catch(Exception e)
  46. {
  47. throw new ServiceException("系统故障,请联系服务商");
  48. }
  49. }
  50. public boolean verifyExpireCode(String code)
  51. {
  52. try
  53. {
  54. //template.boundValueOps(tokenKey).set(loginName, 10000, TimeUnit.MILLISECONDS);
  55. return template.hasKey(code);
  56. }
  57. catch(Exception e)
  58. {
  59. throw new ServiceException("系统故障,请联系服务商");
  60. }
  61. }
  62. public void setValue(String key, String value, long expirePeriod){
  63. try
  64. {
  65. template.opsForValue().set(key, value, expirePeriod, TimeUnit.MILLISECONDS);
  66. }
  67. catch(Exception e)
  68. {
  69. throw new ServiceException("系统故障,请联系服务商");
  70. }
  71. }
  72. public String getValue(String key){
  73. try
  74. {
  75. return template.opsForValue().get(key);
  76. }
  77. catch(Exception e)
  78. {
  79. throw new ServiceException("系统故障,请联系服务商");
  80. }
  81. }
  82. public void removeValue(String key){
  83. try
  84. {
  85. template.delete(key);
  86. }
  87. catch(Exception e)
  88. {
  89. throw new ServiceException("系统故障,请联系服务商");
  90. }
  91. }
  92. }