package com.zswl.dataservice.service.base; import com.zswl.dataservice.utils.exception.ServiceException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class RedisService { @Autowired private StringRedisTemplate template; public void addExpireToken(String loginName,String token,long tokenExpirePeriod) { try { String[] tokens=token.split("\\."); //template.boundValueOps(tokenKey).set(loginName, 10000, TimeUnit.MILLISECONDS); template.opsForValue().set(tokens[1],loginName, tokenExpirePeriod, TimeUnit.MILLISECONDS); } catch(Exception e) { throw new ServiceException("系统故障,请联系服务商"); } } public void removeExpireToken(String loginName,String token) { try { String[] tokens=token.split("\\."); //template.boundValueOps(tokenKey).set(loginName, 10000, TimeUnit.MILLISECONDS); template.delete(tokens[1]); } catch(Exception e) { throw new ServiceException("系统故障,请联系服务商"); } } public boolean verifyExpireJwtToken(String loginName,String jwtToken) { try { String[] tokens=jwtToken.split("\\."); //template.boundValueOps(tokenKey).set(loginName, 10000, TimeUnit.MILLISECONDS); return template.hasKey(tokens[1]); } catch(Exception e) { throw new ServiceException("系统故障,请联系服务商"); } } public boolean verifyExpireCode(String code) { try { //template.boundValueOps(tokenKey).set(loginName, 10000, TimeUnit.MILLISECONDS); return template.hasKey(code); } catch(Exception e) { throw new ServiceException("系统故障,请联系服务商"); } } public void setValue(String key, String value, long expirePeriod){ try { template.opsForValue().set(key, value, expirePeriod, TimeUnit.MILLISECONDS); } catch(Exception e) { throw new ServiceException("系统故障,请联系服务商"); } } public String getValue(String key){ try { return template.opsForValue().get(key); } catch(Exception e) { throw new ServiceException("系统故障,请联系服务商"); } } public void removeValue(String key){ try { template.delete(key); } catch(Exception e) { throw new ServiceException("系统故障,请联系服务商"); } } }