首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Data Redis

Spring Data Redis

作者头像
用户1148394
发布2020-03-02 11:12:55
4940
发布2020-03-02 11:12:55
举报
文章被收录于专栏:余林丰余林丰

《Redis5.x入门教程》目录

  • 第一章 · 准备工作
  • 第二章 · 数据类型
  • 第三章 · ​命令
  • 第四章 ​· 配置
  • 第五章 · Java客户端(上)
  • 第六章 · 事务
  • 第七章 · 分布式锁
  • 第八章 · Java客户端(下)

第八章 · Java客户端(下)

有关本章节源码:https://github.com/yu-linfeng/redis5.x_tutorial/tree/master/code/spring-data-redis

Java客户端(上)章节中我们使用了redis的Java客户端的第三方开源框架——Jedis,但目前Java应用已经被Spring(Spring Boot)统治了大半江山,就连一些数据连接操作的封装Spring也不放过,这其中也不乏有redis的封装——Spring Data Redis。关于Spring Data Redis的官方介绍:https://spring.io/projects/spring-data-redis

使用Spring Data Redis后,你会发现一切变得如此简单,只需要配置文件即可做到开箱即用

我们通过IDEA中的Spring Initializer创建Spring Boot工程,并选择Spring Data Redis,主要步骤入下图所示:

第一步,创建工程,选择Spring Initializr

第二步,选择SpringBoot的依赖NoSQL -> Spring Data Redis

创建好后,我们通过ymal格式的配置文件application.yml配置相关配置项。

spring:
  redis:
    host: 127.0.0.1
    port: 6379

Spring Data Redis中操作redis的最关键的类是RedisTemplate,了解过Spring Data的朋友应该很熟悉~Template后缀,我们在配置好application.yml后直接写一个测试类体验一下,什么是开箱即用:

package com.coderbuff.springdataredis;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootTest
public class SpringDataRedisApplicationTests {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    void testDefaultRestTemplate() {
        redisTemplate.opsForValue().set("default_redis_template", "1");
    }
}

什么是开箱即用,这就是开箱即用。我们没有再写任何的类,只需要两行配置即可使用。

通常情况下,我们喜欢“封装”,喜欢把redisTemplate.opsForValue().set这样的操作封装成工具类redisUtil.set,所以我们封装一下RedisTemplate,顺带熟悉它的API。

package com.coderbuff.springdataredis.util;

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 okevin
 * @date 2020/2/18 14:34
 */
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 字符串类型写入操作
     * @param key key值
     * @param value value值
     */
    public void set(String key, String value) {
        this.redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 可设置过期时间字符串类型写入操作
     * @param key key值
     * @param value value值
     * @param expire 过期时间
     * @param timeUnit 过期时间单位
     */
    public void set(String key, String value, Long expire, TimeUnit timeUnit) {
        this.redisTemplate.opsForValue().set(key, value, expire, timeUnit);
    }

    /**
     * 字符串类型读取操作
     * @param key key值
     * @return value值
     */
    public String get(String key) {
        return (String) this.redisTemplate.opsForValue().get(key);
    }
}

编写测试类:

package com.coderbuff.springdataredis.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @author okevin
 * @date 2020/2/18 23:14
 */
@SpringBootTest
public class RedisUtilTests {

    @Autowired
    private RedisUtil redisUtil;

    @Test
    public void testSet() {
        redisUtil.set("redis_util", "1");
        Assertions.assertEquals("1", redisUtil.get("redis_util"));
    }
}

实际上,真正要把Spring Data Redis用好,还可以做以下工作:

  • 把redis作为Spring的缓存管理

注意本文使用的是SpringBoot2.x与SpringBoot1.x有一定的区别。

package com.coderbuff.springdataredis.config;

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.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * redis配置
 * @author okevin
 * @date 2020/2/18 14:20
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    /**
     * 使用redis作为spring的缓存管理工具
     * 注意:springboot2.x与springboot1.x此处的区别较大
     * 在springboot1.x中,要使用redis的缓存管理工具为以下代码:
     *
     * public CacheManager cacheManager(RedisTemplate redisTemplate) {
     *     RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
     *     return redisCacheManager;
     * }
     *
     * @param redisConnectionFactory redis连接工厂
     * @return redis缓存管理
     */
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheManager redisCacheManager = RedisCacheManager.create(redisConnectionFactory);
        return redisCacheManager;
    }
}
  • 尽管SpringBoot已经为我们构造好了RedisTemplate,但我们可能还是需要定制化注入RedisTemplate

我们可以定制RedisTemplate,例如序列化的方式等,当然这些都不是必须的:

package com.coderbuff.springdataredis.config;

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.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * redis配置
 * @author okevin
 * @date 2020/2/18 14:20
 */
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    //省略上面的cacheManager注入

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);     //配置连接工厂

        /*使用Jackson序列化和反序列化key、value值,默认使用JDK的序列化方式
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);  //指定要序列化的域,ALL表示所有字段、以及set/get方法,ANY是都有包括修饰符private和public
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);   //指定序列化输入的类型,NON_FINAL表示必须是非final修饰的类型
        jacksonSeial.setObjectMapper(om);

        //以下数据类型通过jackson序列化
        redisTemplate.setValueSerializer(jacksonSeial);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jacksonSeial);
        */
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

想要学习更多Spring Data Redis,就请打开官网(https://spring.io/projects/spring-data-redis)尽情探索吧

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第八章 · Java客户端(下)
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档