12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package com.fjhx.config.redis;
- import cn.hutool.extra.spring.SpringUtil;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.core.ValueOperations;
- import java.util.Collection;
- import java.util.concurrent.TimeUnit;
- /**
- * redis缓存 工具类
- **/
- @SuppressWarnings(value = {"unchecked", "rawtypes"})
- public class RedisCache {
- private static final RedisTemplate redisTemplate = SpringUtil.getBean("redisTemplate");
- /**
- * 设置缓存(默认缓存30分钟)
- *
- * @param key 缓存的键值
- * @param value 缓存的值
- */
- public static void set(String key, Object value) {
- redisTemplate.opsForValue().set(key, value);
- }
- /**
- * 设置缓存,并指定过期时间(分钟)
- *
- * @param key 缓存的键值
- * @param value 缓存的值
- * @param timeout 时间
- */
- public static void set(String key, Object value, Integer timeout) {
- set(key, value, timeout, TimeUnit.MINUTES);
- }
- /**
- * 设置缓存
- *
- * @param key 缓存的键值
- * @param value 缓存的值
- * @param timeout 时间
- * @param timeUnit 时间颗粒度
- */
- public static void set(String key, Object value, Integer timeout, TimeUnit timeUnit) {
- redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
- }
- /**
- * 获取缓存
- *
- * @param key 缓存键值
- * @param <T> 放回对象类型
- * @return 缓存键值对应的数据
- */
- public static <T> T get(String key) {
- ValueOperations<String, T> operation = redisTemplate.opsForValue();
- return operation.get(key);
- }
- /**
- * 删除缓存
- *
- * @param key 缓存key
- */
- public static void delete(String key) {
- redisTemplate.delete(key);
- }
- /**
- * 获得缓存的基本对象列表
- *
- * @param pattern 字符串前缀
- * @return 对象列表
- */
- public static Collection<String> keys(final String pattern) {
- return redisTemplate.keys(pattern);
- }
- }
|