| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package com.zskk.pacsonline.utils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Component;
- import java.util.concurrent.TimeUnit;
- /**
- * Redis缓存工具类
- *
- * @author admin
- */
- @Component
- public class RedisCache {
- @Autowired
- private RedisTemplate<String, Object> redisTemplate;
- /**
- * 缓存基本的对象
- *
- * @param key 缓存的键
- * @param value 缓存的值
- */
- public <T> void set(String key, T value) {
- redisTemplate.opsForValue().set(key, value);
- }
- /**
- * 缓存基本的对象,带过期时间
- *
- * @param key 缓存的键
- * @param value 缓存的值
- * @param timeout 时间
- * @param timeUnit 时间单位
- */
- public <T> void set(String key, T value, long timeout, TimeUnit timeUnit) {
- redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
- }
- /**
- * 获得缓存的基本对象
- *
- * @param key 缓存键
- * @return 缓存值
- */
- public <T> T get(String key) {
- return (T) redisTemplate.opsForValue().get(key);
- }
- /**
- * 删除单个对象
- *
- * @param key 缓存键
- */
- public void delete(String key) {
- redisTemplate.delete(key);
- }
- /**
- * 判断key是否存在
- *
- * @param key 缓存键
- * @return true存在 false不存在
- */
- public boolean hasKey(String key) {
- return Boolean.TRUE.equals(redisTemplate.hasKey(key));
- }
- /**
- * 设置有效时间
- *
- * @param key 缓存键
- * @param timeout 超时时间
- * @return true=设置成功;false=设置失败
- */
- public boolean expire(String key, long timeout) {
- return expire(key, timeout, TimeUnit.SECONDS);
- }
- /**
- * 设置有效时间
- *
- * @param key 缓存键
- * @param timeout 超时时间
- * @param timeUnit 时间单位
- * @return true=设置成功;false=设置失败
- */
- public boolean expire(String key, long timeout, TimeUnit timeUnit) {
- return Boolean.TRUE.equals(redisTemplate.expire(key, timeout, timeUnit));
- }
- /**
- * 获取过期时间
- *
- * @param key 缓存键
- * @return 过期时间(秒)
- */
- public long getExpire(String key) {
- Long expire = redisTemplate.getExpire(key, TimeUnit.SECONDS);
- return expire != null ? expire : -1;
- }
- }
|