加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

SpringBoot+MyBatis+Redis(二级缓存)

发布时间:2020-12-15 07:14:38 所属栏目:Java 来源:网络整理
导读:应用场景: 保存大数据量,避免重复请求。 ? 一、添加Maven依赖 !-- SpringBoot Boot Redis --dependency groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-data-redis/artifactId/dependency 二、编写Redis相关类 RedisService.java

应用场景:
保存大数据量,避免重复请求。

?

一、添加Maven依赖

<!-- SpringBoot Boot Redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

二、编写Redis相关类

RedisService.java

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@SuppressWarnings(value = { "unchecked",rawtypes" })
@Component
public class RedisService
{
    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * 缓存基本的对象,Integer、String、实体类等
     *
     * @param key 缓存的键值
     * @param value 缓存的值
     */
    public <T> void setCacheObject(final String key,final T value)
    {
        redisTemplate.opsForValue().set(key,value);
    }

    *
     * 缓存基本的对象,Integer、String、实体类等
     *
     * @param key 缓存的键值
     * @param value 缓存的值
     * @param timeout 时间
     * @param timeUnit 时间颗粒度
     *
     * 设置有效时间
     *
     * @param key Redis键
     * @param timeout 超时时间
     * @return true=设置成功;false=设置失败
     public boolean expire(final String key,final long timeout)
    {
        return expire(key,TimeUnit.SECONDS);
    }

    *
     * 设置有效时间
     *
     * @param key Redis键
     * @param timeout 超时时间
     * @param unit 时间单位
     * @return true=设置成功;false=设置失败
      timeout,final TimeUnit unit)
    {
         redisTemplate.expire(key,unit);
    }

    *
     * 获得缓存的基本对象。
     *
     * @param key 缓存键值
     * @return 缓存键值对应的数据
     public <T> T getCacheObject(final String key)
    {
        ValueOperations<String,T> operation = redisTemplate.opsForValue();
        return operation.get(key);
    }

    *
     * 删除单个对象
     *
     * @param key
      boolean deleteObject(final String key)
    {
         redisTemplate.delete(key);
    }

    *
     * 删除集合对象
     *
     * @param collection 多个对象
     * @return
      deleteObject(final Collection collection)
    {
         redisTemplate.delete(collection);
    }

    *
     * 缓存List数据
     *
     * @param key 缓存的键值
     * @param dataList 待缓存的List数据
     * @return 缓存的对象
     long setCacheList(final String key,final List<T> dataList)
    {
        Long count = redisTemplate.opsForList().rightPushAll(key,dataList);
        return count == null ? 0 : count;
    }

    *
     * 获得缓存的list对象
     *
     * @param key 缓存的键值
     * @return 缓存键值对应的数据
     public <T> List<T> getCacheList(final String key)
    {
        return redisTemplate.opsForList().range(key,1)">0,-1);
    }

    *
     * 缓存Set
     *
     * @param key 缓存键值
     * @param dataSet 缓存的数据
     * @return 缓存数据的对象
     long setCacheSet(final String key,final Set<T> dataSet)
    {
        Long count = redisTemplate.opsForSet().add(key,dataSet);
        *
     * 获得缓存的set
     *
     * @param key
     * @return
     public <T> Set<T> getCacheSet(final String key)
    {
         redisTemplate.opsForSet().members(key);
    }

    *
     * 缓存Map
     *
     * @param key
     * @param dataMap
     void setCacheMap(final String key,final Map<String,T> dataMap)
    {
        if (dataMap != null) {
            redisTemplate.opsForHash().putAll(key,dataMap);
        }
    }

    *
     * 获得缓存的Map
     *
     * @param key
     * @return
     public <T> Map<String,1)"> getCacheMap(final String key)
    {
         redisTemplate.opsForHash().entries(key);
    }

    *
     * 往Hash中存入数据
     *
     * @param key Redis键
     * @param hKey Hash键
     * @param value 值
      setCacheMapValue(final String key,final String hKey,final T value)
    {
        redisTemplate.opsForHash().put(key,hKey,1)">*
     * 获取Hash中的数据
     *
     * @param key Redis键
     * @param hKey Hash键
     * @return Hash中的对象
      T getCacheMapValue(final String key,final String hKey)
    {
        HashOperations<String,String,T> opsForHash = redisTemplate.opsForHash();
        return opsForHash.*
     * 获取多个Hash中的数据
     *
     * @param key Redis键
     * @param hKeys Hash键集合
     * @return Hash对象集合
     public <T> List<T> getMultiCacheMapValue(final String key,final Collection<Object> hKeys)
    {
         redisTemplate.opsForHash().multiGet(key,hKeys);
    }

    *
     * 获得缓存的基本对象列表
     * 
     * @param pattern 字符串前缀
     * @return 对象列表
     public Collection<String> keys(final String pattern)
    {
         redisTemplate.keys(pattern);
    }
}

RedisConfig.java

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
@EnableCaching
 RedisConfig extends CachingConfigurerSupport
{
    @Bean
    @SuppressWarnings(value = { deprecation })
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory)
    {
        RedisTemplate<Object,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.);

        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL,JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);

        template.setValueSerializer(serializer);
        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer( StringRedisSerializer());
        template.afterPropertiesSet();
         template;
    }
}

RedisCache.java

import org.apache.ibatis.cache.Cache;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

 RedisCache implements Cache {
    private final ReadWriteLock readWriteLock =  ReentrantReadWriteLock();
    private final String id;
     RedisTemplate redisTemplate;
    redis过期时间
    private static final long EXPIRE_TIME_IN_MINUTES = 30;

     RedisCache(String id) {
        if (id == ) {
            throw new IllegalArgumentException(Cache instance required an ID);
        }
        this.id = id;
    }

    @Override
     String getId() {
         id;
    }

    *
     * Put query result to redis
     *
     * @Param key
     * @Param value
     */
    @Override
     putObject(Object key,Object value) {
        RedisTemplate redisTemplate = getRedisTemplate();
        ValueOperations opsForValue = redisTemplate.opsForValue();
        System.out.println(key + : key);
        System.out.println(key.toString() + : key.toString()out.println(value + : valueout.println(value.toString() + : value.toString());
        opsForValue.(key.toString(),EXPIRE_TIME_IN_MINUTES,TimeUnit.MINUTES);
        System.out.println(结果成功放入缓存 and " + key = n" + key + value = " + value);
        System.out.println(opsForValue.(key.toString()));
    }

    *
     * Get cached query result to redis
     *
     * @Param key
     * @Return
      Object getObject(Object key) {
        RedisTemplate redisTemplate = getRedisTemplate();
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        ValueOperations opsForValue =结果从缓存中获取);
        return opsForValue.(key.toString());
    }

    *
     * Remove cached query result to redis
     *
     * @Param key
     * @Return
      Object removeObject(Object key) {
        RedisTemplate redisTemplate = getRedisTemplate();
        redisTemplate.delete(key);
        System.从缓存中删除return ;
    }

    *
     * Clear this cache instance
      clear() {
        RedisTemplate redisTemplate = getRedisTemplate();
        redisTemplate.execute((RedisCallback) connection -> {
            connection.flushDb();
            ;
        });
        System.清空缓存);
    }

    @Override
    int getSize() {
        Long size = (Long) redisTemplate.execute((RedisCallback) connection -> connection.dbSize());
         size.intValue();
    }

    @Override
     ReadWriteLock getReadWriteLock() {
         readWriteLock;
    }

     RedisTemplate getRedisTemplate() {
        if (redisTemplate == ) {
            redisTemplate = ApplicationContextHolder.getBean(redisTemplate redisTemplate;
    }
}

三、yml配置redis

spring:
  redis:
    host: localhost
    port: 6379
    password:

四、在DAO类添加该注解

@CacheNamespace(implementation = RedisCache.class)

五、实现类或者在Controller加如下代码,键值对保存对应的数据

@Autowired
private RedisService redisService;

?

六、如何确保Redis数据实时更新

我研究了下,通常如下:
1.先删缓存,再更新数据库
2.先写数据库,再删缓存
参考:
Redis缓存如何保证一致性

补充:
Redis 和 Mysql 数据库数据如何保持一致性

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读