Springboot redis 缓存使用示例 | RedisTemplate

Posted 洛阳泰山

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot redis 缓存使用示例 | RedisTemplate相关的知识,希望对你有一定的参考价值。

RedisUtil  工具类代码


import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

public class RedisUtil 
    private static final Logger log = LoggerFactory.getLogger(RedisUtil.class);
    private static final String SUCCESS = "OK";
    private static final long UN_EXPIRE = -1L;
    private final RedisTemplate<String, Object> redisTemplate;

    public RedisUtil(RedisTemplate<String, Object> redisTemplate) 
        Assert.notNull(redisTemplate, "redisTemplate is null");
        this.redisTemplate = redisTemplate;
    

    public void set(String key, Object value) 
        this.redisTemplate.opsForValue().set(key, value);
    

    public void setEx(String key, Object value, Duration timeout) 
        this.redisTemplate.opsForValue().set(key, value, timeout);
    

    public void setEx(String key, Object value, Long seconds) 
        this.redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
    

    @Nullable
    public <T> T get(String key) 
        return this.redisTemplate.opsForValue().get(key);
    

    public <T> T get(String key, Class<T> clazz) 
        try 
            if (StringUtils.isEmpty(key)) 
                return null;
             else 
                Object obj = this.redisTemplate.opsForValue().get(key);
                return obj;
            
         catch (Exception var5) 
            log.error("redisUtil get", var5);
            return null;
        
    

    @Nullable
    public <T> T get(String key, Supplier<T> loader) 
        T value = this.get(key);
        if (Objects.isNull(value)) 
            return value;
         else 
            value = loader.get();
            if (Objects.nonNull(value)) 
                this.set(key, value);
            

            return value;
        
    

    public Boolean del(String key) 
        return this.redisTemplate.delete(key);
    

    public Long del(String... keys) 
        return this.del((Collection)Arrays.asList(keys));
    

    public Long del(Collection<String> keys) 
        return this.redisTemplate.delete(keys);
    

    public Set<String> keys(String pattern) 
        return this.redisTemplate.keys(pattern);
    

    public void multiSet(Map<String, Object> kvMap) 
        this.redisTemplate.opsForValue().multiSet(kvMap);
    

    public List<Object> multiGet(String... keys) 
        return this.multiGet((Collection)Arrays.asList(keys));
    

    public List<Object> multiGet(Collection<String> keys) 
        return this.redisTemplate.opsForValue().multiGet(new HashSet(keys));
    

    public Long decr(String key) 
        return this.redisTemplate.opsForValue().decrement(key);
    

    public Long decrBy(String key, long longValue) 
        return this.redisTemplate.opsForValue().decrement(key, longValue);
    

    public Long incr(String key) 
        return this.redisTemplate.opsForValue().increment(key);
    

    public Long incrBy(String key, long longValue) 
        return this.redisTemplate.opsForValue().increment(key, longValue);
    

    public Long getCounter(String key) 
        return (Long)this.redisTemplate.opsForValue().get(key);
    

    public Boolean exists(String key) 
        return this.redisTemplate.hasKey(key);
    

    public String randomKey() 
        return (String)this.redisTemplate.randomKey();
    

    public void rename(String oldkey, String newkey) 
        this.redisTemplate.rename(oldkey, newkey);
    

    public Boolean move(String key, int dbIndex) 
        return this.redisTemplate.move(key, dbIndex);
    

    public Boolean expire(String key, long seconds) 
        return this.redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
    

    public Boolean expire(String key, Duration timeout) 
        return this.expire(key, timeout.getSeconds());
    

    public Boolean expireAt(String key, Date date) 
        return this.redisTemplate.expireAt(key, date);
    

    public Boolean expireAt(String key, long unixTime) 
        return this.expireAt(key, new Date(unixTime));
    

    public Boolean pexpire(String key, long milliseconds) 
        return this.redisTemplate.expire(key, milliseconds, TimeUnit.MILLISECONDS);
    

    public <T> T getSet(String key, Object value) 
        return this.redisTemplate.opsForValue().getAndSet(key, value);
    

    public Boolean persist(String key) 
        return this.redisTemplate.persist(key);
    

    public String type(String key) 
        if (StringUtil.isBlank(key)) 
            return "";
         else 
            DataType dataType = this.redisTemplate.type(key);
            return Objects.nonNull(dataType) ? dataType.code() : "";
        
    

    public Long ttl(String key) 
        return this.redisTemplate.getExpire(key);
    

    public Long pttl(String key) 
        return this.redisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
    

    public void hSet(String key, Object field, Object value) 
        this.redisTemplate.opsForHash().put(key, field, value);
    

    public void hMset(String key, Map<? extends Object, ? extends Object> hash) 
        this.redisTemplate.opsForHash().putAll(key, hash);
    

    public <T> T hGet(String key, Object field) 
        return this.redisTemplate.opsForHash().get(key, field);
    

    public <T> T hGet(String key, Serializable item, Class<T> clazz) 
        T t = null;
        if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(item)) 
            Object obj = this.redisTemplate.opsForHash().get(key, item);
            if (obj == null) 
                return null;
             else 
                try 
                    t = obj;
                 catch (Exception var7) 
                    log.error("RedisUtil.hget", var7);
                

                return t;
            
         else 
            return null;
        
    

    public <T> T hGet(String key, Serializable item, Class<T> clazz, Supplier<T> supplier) 
        if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(item)) 
            T t = this.redisTemplate.opsForHash().get(key, item);
            if (Objects.isNull(t)) 
                t = supplier.get();
                if (Objects.nonNull(t)) 
                    this.hSet(key, item, t);
                
            

            return t;
         else 
            return null;
        
    

    public <T> T hGet(String key, Serializable item, Supplier<T> supplier) 
        if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(item)) 
            T t = this.redisTemplate.opsForHash().get(key, item);
            if (Objects.isNull(t)) 
                t = supplier.get();
                if (Objects.nonNull(t)) 
                    this.hSet(key, item, t);
                
            

            return t;
         else 
            return null;
        
    

    public List hmGet(String key, Object... fields) 
        return this.hmGet(key, (Collection)Arrays.asList(fields));
    

    public List hmGet(String key, Collection<Object> hashKeys) 
        return this.redisTemplate.opsForHash().multiGet(key, hashKeys);
    

    public Long hDel(String key, Object... fields) 
        return this.redisTemplate.opsForHash().delete(key, fields);
    

    public Long hDel(String key, Collection<Object> fields) 
        return this.redisTemplate.opsForHash().delete(key, new Object[]fields);
    

    public Boolean hExists(String key, Object field) 
        return this.redisTemplate.opsForHash().hasKey(key, field);
    

    public List hVals(String key) 
        return this.redisTemplate.opsForHash().values(key);
    

    public Set<Object> hKeys(String key) 
        return this.redisTemplate.opsForHash().keys(key);
    

    public Long hLen(String key) 
        return this.redisTemplate.opsForHash().size(key);
    

    public Long hIncrBy(String key, Object field, long value) 
        return this.redisTemplate.opsForHash().increment(key, field, value);
    

    public Double hIncrByFloat(String key, Object field, double value) 
        return this.redisTemplate.opsForHash().increment(key, field, value);
    

    public <T> T lIndex(String key, long index) 
        return this.redisTemplate.opsForList().index(key, index);
    

    public Long lLen(String key) 
        return this.redisTemplate.opsForList().size(key);
    

    public <T> T lPop(String key) 
        return this.redisTemplate.opsForList().leftPop(key);
    

    public Long lPush(String key, Object... values) 
        return this.redisTemplate.opsForList().leftPush(key, values);
    

    public void lSet(String key, long index, Object value) 
        this.redisTemplate.opsForList().set(key, index, value);
    

    public Long lDel(String key, long count, Object value) 
        return this.redisTemplate.opsForList().remove(key, count, value);
    

    public Long lDelOne(String key, Object value) 
        return StringUtil.isBlank(key) ? null : this.lDel(key, 1L, value);
    

    public Long lDelAll(String key, Object value) 
        return this.lDel(key, 0L, value);
    

    public List lRange(String key, long start, long end) 
        return this.redisTemplate.opsForList().range(key, start, end);
    

    public void lTrim(String key, long start, long end) 
        this.redisTemplate.opsForList().trim(key, start, end);
    

    public <T> T rPop(String key) 
        return this.redisTemplate.opsForList().rightPop(key);
    

    public Long rPush(String key, Object... values) 
        return this.redisTemplate.opsForList().rightPush(key, values);
    

    public <T> T rPopLPush(String srcKey, String dstKey) 
        return this.redisTemplate.opsForList().rightPopAndLeftPush(srcKey, dstKey);
    

    public Long sAdd(String key, Object... members) 
        return this.redisTemplate.opsForSet().add(key, members);
    

    public <T> T sPop(String key) 
        return StringUtil.isBlank(key) ? null : this.redisTemplate.opsForSet().pop(key);
    

    public Set sMembers(String key) 
        return StringUtil.isBlank(key) ? Collections.emptySet() : this.redisTemplate.opsForSet().members(key);
    

    public <T> Set<T> sMembers(String key, Class<T> clazz) 
        if (StringUtil.isBlank(key)) 
            return Collections.emptySet();
         else 
            Set<?> set = this.redisTemplate.opsForSet().members(key);
            return CollectionUtil.isEmpty(set) ? Collections.emptySet() : (Set)set.stream().map((item) -> 
                return item;
            ).collect(Collectors.toSet());
        
    

    public boolean sIsMember(String key, Object member) 
        return !StringUtil.isBlank(key) && !Objects.isNull(member) ? this.redisTemplate.opsForSet().isMember(key, member) : false;
    

    public Set sInter(String key, String otherKey) 
        return this.redisTemplate.opsForSet().intersect(key, otherKey);
    

    public Set sInter(String key, Collection<String> otherKeys) 
        return this.redisTemplate.opsForSet().intersect(key, otherKeys);
    

    public <T> T sRandMember(String key) 
        return this.redisTemplate.opsForSet().randomMember(key);
    

    public Set sUnion(String key, String otherKey) 
        return this.redisTemplate.opsForSet().union(key, otherKey);
    

    public Set sUnion(String key, Collection<String> otherKeys) 
        return this.redisTemplate.opsForSet().union(key, otherKeys);
    

    public Set sDiff(String key, String otherKey) 
        return this.redisTemplate.opsForSet().difference(key, otherKey);
    

    public Set sDiff(String key, Collection<String> otherKeys) 
        return this.redisTemplate.opsForSet().difference(key, otherKeys);
    

    public boolean hCover(String key, Map<? extends Serializable, Object> map) 
        if (!StringUtil.isBlank(key) && !Objects.isNull(map)) 
            String result = (String)this.redisTemplate.execute((connection) -> 
                try 
                    connection.multi();
                    StringRedisSerializer srs = (StringRedisSerializer)this.redisTemplate.getKeySerializer();
                    byte[] rawKey = srs.serialize(key);
                    if (Objects.isNull(rawKey)) 
                        return null;
                    

                    connection.del(new byte[][]rawKey);
                    Map<byte[], byte[]> hashes = new LinkedHashMap(map.size());
                    GenericJackson2JsonRedisSerializer hashKeySerializer = (GenericJackson2JsonRedisSerializer)this.redisTemplate.getHashKeySerializer();
                    GenericJackson2JsonRedisSerializer hashValueSerializer = (GenericJackson2JsonRedisSerializer)this.redisTemplate.getHashValueSerializer();
                    Iterator var9 = map.entrySet().iterator();

                    while(var9.hasNext()) 
                        Entry<? extends Serializable, Object> entry = (Entry)var9.next();
                        hashes.put(hashKeySerializer.serialize(entry.getKey()), hashValueSerializer.serialize(entry.getValue()));
                    

                    connection.hMSet(rawKey, hashes);
                    connection.exec();
                 catch (SerializationException var11) 
                    connection.discard();
                

                return null;
            );
            return Objects.equals("OK", result);
         else 
            return false;
        
    

    public void hdel(String key, Object... item) 
        this.redisTemplate.opsForHash().delete(key, item);
    

    public <T, HK, HV> boolean hSetBatch(List<T> list, Function<T, String> keyFunction, Function<T, HK> hashKeyFunction, Function<T, HV> hashValueFunction) 
        RedisConnection connection = null;

        boolean var7;
        try 
            if (!CollectionUtils.isEmpty(list) && !Objects.isNull(keyFunction) && !Objects.isNull(hashKeyFunction) && !Objects.isNull(hashValueFunction)) 
                connection = ((RedisConnectionFactory)Objects.requireNonNull(this.redisTemplate.getConnectionFactory())).getConnection();
                StringRedisSerializer srs = (StringRedisSerializer)this.redisTemplate.getKeySerializer();
                GenericJackson2JsonRedisSerializer hashKeySerializer = (GenericJackson2JsonRedisSerializer)this.redisTemplate.getHashKeySerializer();
                GenericJackson2JsonRedisSerializer hashValueSerializer = (GenericJackson2JsonRedisSerializer)this.redisTemplate.getHashValueSerializer();
                connection.openPipeline();
                Iterator var9 = list.iterator();

                while(var9.hasNext()) 
                    T t = var9.next();
                    connection.hSet((byte[])Objects.requireNonNull(srs.serialize((String)keyFunction.apply(t))), (byte[])Objects.requireNonNull(hashKeySerializer.serialize(hashKeyFunction.apply(t))), (byte[])Objects.requireNonNull(hashValueSerializer.serialize(hashValueFunction.apply(t))));
                

                connection.closePipeline();
                boolean var18 = true;
                return var18;
            

            boolean var6 = false;
            return var6;
         catch (Exception var14) 
            var7 = false;
         finally 
            if (Objects.nonNull(connection) && !connection.isClosed()) 
                if (connection.isPipelined()) 
                    connection.closePipeline();
                

                RedisConnectionUtils.releaseConnection(connection, this.redisTemplate.getConnectionFactory());
            

        

        return var7;
    

    public boolean hDelBatch(List<String> keys, Object item) 
        RedisConnection connection = ((RedisConnectionFactory)Objects.requireNonNull(this.redisTemplate.getConnectionFactory())).getConnection();
        StringRedisSerializer srs = (StringRedisSerializer)this.redisTemplate.getKeySerializer();
        GenericJackson2JsonRedisSerializer hashKeySerializer = (GenericJackson2JsonRedisSerializer)this.redisTemplate.getHashKeySerializer();
        connection.openPipeline();
        Iterator var6 = keys.iterator();

        while(var6.hasNext()) 
            String key = (String)var6.next();
            connection.hDel((byte[])Objects.requireNonNull(srs.serialize(key)), new byte[][]hashKeySerializer.serialize(item));
        

        connection.closePipeline();
        return true;
    

    public boolean setNxPx(String key, String val, long expire) 
        if (StringUtil.isAnyBlank(new CharSequence[]key, val)) 
            return false;
         else 
            try 
                RedisConnection conn = this.redisTemplate.getConnectionFactory().getConnection();
                byte[] rawKeys = key.getBytes(StandardCharsets.UTF_8);
                byte[] rawValues = val.getBytes(StandardCharsets.UTF_8);
                return conn.set(rawKeys, rawValues, Expiration.from(expire, TimeUnit.MILLISECONDS), SetOption.SET_IF_ABSENT);
             catch (Exception var8) 
                log.error("set nx px error ", var8);
                return false;
            
        
    

    public <HK, HV> Map<HK, HV> hgetAll(String key, Class<HK> hkClazz, Class<HV> hvClazz) 
        try 
            Map<Object, Object> data = this.redisTemplate.opsForHash().entries(key);
            if (CollectionUtils.isEmpty(data)) 
                return Collections.emptyMap();
             else 
                Map<HK, HV> result = new LinkedHashMap();
                Iterator var6 = data.entrySet().iterator();

                while(var6.hasNext()) 
                    Entry<Object, Object> entry = (Entry)var6.next();
                    result.put(entry.getKey(), entry.getValue());
                

                return result;
            
         catch (Exception var8) 
            return Collections.emptyMap();
        
    

    public <T> Map<T, T> hgetAll(String key, Class<T> clazz) 
        try 
            Map<Object, Object> data = this.redisTemplate.opsForHash().entries(key);
            if (CollectionUtils.isEmpty(data)) 
                return Collections.emptyMap();
             else 
                Map<T, T> result = new LinkedHashMap();
                Iterator var5 = data.entrySet().iterator();

                while(var5.hasNext()) 
                    Entry<Object, Object> entry = (Entry)var5.next();
                    result.put(entry.getKey(), entry.getValue());
                

                return result;
            
         catch (Exception var7) 
            return Collections.emptyMap();
        
    

    public boolean setNxPx(String key, String val) 
        return this.setNxPx(key, val, -1L);
    

    public <E> List<E> lGetAll(String key, Class<E> clazz) 
        if (StringUtil.isBlank(key)) 
            return Collections.emptyList();
         else 
            try 
                long size = this.redisTemplate.opsForList().size(key);
                if (size <= 0L) 
                    return Collections.emptyList();
                 else 
                    List<?> list = this.redisTemplate.opsForList().range(key, 0L, size);
                    return CollectionUtil.isEmpty(list) ? Collections.emptyList() : (List)list.stream().map((item) -> 
                        return item;
                    ).collect(Collectors.toList());
                
             catch (Exception var6) 
                log.error("RedisUtil.lGet", var6);
                return null;
            
        
    

    public RedisTemplate<String, Object> getRedisTemplate() 
        return this.redisTemplate;
    

示例一  用户信息缓存


import org.springblade.core.redis.cache.RedisUtil;
import org.springblade.core.tool.utils.MapUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.gis.modules.user.entity.User;
import org.springblade.gis.modules.user.service.UserService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.springblade.gis.common.constant.CacheConstant.*;

/**
 * 系统通用缓存(菜单、部门、岗位、角色、租户)
 *
 * @author tarizan
 * @version 1.0
 * @date 2020年09月18日 14:59:03
 * @since JDK1.8
 */
public class SystemCache 

    private static final RedisUtil REDIS_UTIL;
    private static final UserService USER_SERVICE;

    static 
        REDIS_UTIL = SpringUtil.getBean(RedisUtil.class);
        USER_SERVICE = SpringUtil.getBean(UserService.class);
    

    /**
     * 获取用户
     *
     * @param userId 用户id
     * @return
     */
    public static User getUser (Long userId) 
        return REDIS_UTIL.hGet(USER_CACHE, userId, User.class, () -> USER_SERVICE.getById(userId));
    

    /**
     * 获取所有用户
     *
     * @return
     */
    public static Collection<User> getAllUser () 
        return REDIS_UTIL.hgetAll(USER_CACHE, Long.class, User.class).values();
    

    /**
     * 删除用户
     *
     * @param userIds 用户id列表
     * @return
     */
    public static void delUser (List<Long> userIds) 
        REDIS_UTIL.hDel(USER_CACHE, userIds);
        REDIS_UTIL.hDel(USER_NAME_CACHE, userIds);
    

    public static void delAllUser()
        REDIS_UTIL.del(USER_CACHE);
    

    /**
     * 删除一个用户
     *
     * @param userId 用户id
     * @return
     */
    public static void delOneUser (Long userId) 
        REDIS_UTIL.hDel(USER_CACHE, userId);
        REDIS_UTIL.hDel(USER_NAME_CACHE, userId);
    

    /**
     * 保存用户
     *
     * @param user 用户信息
     * @return
     */
    public static void saveUser (User user) 
        REDIS_UTIL.hSet(USER_CACHE, user.getId(), user);
        REDIS_UTIL.hSet(USER_NAME_CACHE, user.getId(), user.getRealName());
    

    public static void initUser () 
        List<User> list = USER_SERVICE.list();
        REDIS_UTIL.hMset(USER_CACHE, (Map) MapUtil.map(User::getId, list));
        REDIS_UTIL.hMset(USER_NAME_CACHE, MapUtil.map(User::getId, User::getRealName, list));
    


redis 图形化展示

示例二  业务数据缓存



import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springblade.core.redis.cache.RedisUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.gis.modules.dynamictable.entity.DynamicEntity;

import java.util.Collection;
import java.util.List;



public class DynamicDataCache 

    private static final RedisUtil REDIS_UTIL;

    static 
        REDIS_UTIL = SpringUtil.getBean(RedisUtil.class);
    

    /**
     * 获取批次数据
     *
     * @return
     */
    public static Collection<DynamicEntity> getData (String batchNo) 
        return  REDIS_UTIL.hgetAll(batchNo,Long.class,DynamicEntity.class).values();
    


    /**
     * 删除批次数据
     *
     * @param batchNo 数据批次号
     * @return
     */
    public static void delData (String batchNo) 
        REDIS_UTIL.del(batchNo);
    

    /**
     * 保存批次数据
     * @param batchNo 批次号
     * @param entities 数据
     * @return
     */
    public static void saveData (String batchNo,List<DynamicEntity> entities) 
        entities.forEach(e->
            REDIS_UTIL.hSet(batchNo, IdWorker.get32UUID(),e);
        );
        REDIS_UTIL.expire(batchNo,180L);
    




以上是关于Springboot redis 缓存使用示例 | RedisTemplate的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot 开启Redis缓存

springboot redis 缓存对象

Springboot学习SpringBoot集成Shiro前后端分离使用redis做缓存个人博客搭建

SpringBoot2.x 整合Redis和使用Redis缓存

SpringBoot项目使用redis缓存

SpringBoot 整合 Redis缓存