第八章 · Java客户端(下)(第八章游戏的规则怎么通关)

网友投稿 216 2022-08-04

第八章 · 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 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 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 redisTemplate(RedisConnectionFactory redisConnectionFactory) {

RedisTemplate 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)尽情探索吧。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Redis5.x两种持久化方式以及主从复制配置(redis提供了哪几种持久化方式)
下一篇:Redis的“假事务”与分布式锁(redis分布式锁的问题)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~