spring-data-redis RedisTemplateUtils工具类
程序员文章站
2022-07-13 08:54:40
...
package com.lh.core.core.util; import java.util.Arrays; import java.util.concurrent.TimeUnit; import org.springframework.data.redis.core.RedisTemplate; import com.rd.ifaes.common.dict.ExpireTime; import com.rd.ifaes.common.util.SpringContextHolder; /** * RedisTemplate操作工具类 * * @author lh * @version 3.0 * @since 2016-8-29 * */ public final class RedisTemplateUtils { private static RedisTemplate<String, Object> redisTemplate = SpringContextHolder.getBean("redisTemplate"); /** * 写入缓存 * * @param key * @param value * @param expire */ public static void set(final String key, final Object value, final ExpireTime expire) { redisTemplate.opsForValue().set(key, value, expire.getTime(), TimeUnit.SECONDS); } /** * 读取缓存 * * @param key * @param clazz * @return */ @SuppressWarnings("unchecked") public static <T> T get(final String key, Class<T> clazz) { return (T) redisTemplate.boundValueOps(key).get(); } /** * 读取缓存 * @param key * @return */ public static Object getObj(final String key){ return redisTemplate.boundValueOps(key).get(); } /** * 删除,根据key精确匹配 * * @param key */ public static void del(final String... key) { redisTemplate.delete(Arrays.asList(key)); } /** * 批量删除,根据key模糊匹配 * * @param pattern */ public static void delpn(final String... pattern) { for (String kp : pattern) { redisTemplate.delete(redisTemplate.keys(kp + "*")); } } /** * key是否存在 * * @param key */ public static boolean exists(final String key) { return redisTemplate.hasKey(key); } }