Spring中Redis存取数据示例
1.导入StringRedisTemplate类
import org.springframework.data.redis.core.StringRedisTemplate;
2.自动装配
@Autowired private StringRedisTemplate stringRedisTemplate;
3.存数据(设置5分钟过期)
String token = UUID.randomUUID().toString(); String key = RedisPrefixConstants.WORK_ORDER_EXCEL_TOKEN_PREFIX + token; stringRedisTemplate.opsForValue().set(key, workId, 60 * 5, TimeUnit.SECONDS);
4.取数据
String key = RedisPrefixConstants.WORK_ORDER_EXCEL_TOKEN_PREFIX + token; String wordIdCache = stringRedisTemplate.opsForValue().get(key); if (wordIdCache == null || !wordIdCache.equals(workId)) { throw new com.cmit.kapok.base.core.ServiceException("token已失效"); } //todo 暂时放开限制,测试安卓无法下载问题 stringRedisTemplate.expire(key,1,TimeUnit.MINUTES); //stringRedisTemplate.delete(key);