springboot使用redisRepository和redistemplate操作redis的过程解析(springboot redistemplate新配置)

网友投稿 306 2022-07-27


目录导入依赖基本配置使用RedisTemplate访问redis使用Redisrepository访问redis实例:

导入依赖

菜单大部分情况下不会出现变化,我们可以将其放入Redis 加快加载速度

org.springframework.boot

spring-boot-starter-data-redis

org.apache.commons

commons-pool2

基本配置

redis:

timeout: 10000ms # 连接超时时间

host: 192.168.10.100 # Redis服务器地址

port: 6379 # Redis服务器端口

database: 0 # 选择哪个库,默认0库

lettuce:

pool:

max-active: 1024 # 最大连接数,默认 8

max-wait: 10000ms # 最大连接阻塞等待时间,单位毫秒,默认 -1

max-idle: 200 # 最大空闲连接,默认 8

min-idle: 5 # 最小空闲连接,默认 0

使用RedisTemplate访问redis

RedisConfig.java

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import

org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import

org.springframework.data.redis.serializer.GenericJackson2jsonRedisSerializer;

import org.springframework.data.redis.serializer.StringRedisSerializer;

/**

* Redis配置类

*

* @author zhoubin

* @since 1.0.0

*/

@Configuration

public class RedisConfig {

@Bean

public RedisTemplate redisTemplate(LettuceConnectionFactory

redisConnectionFactory){

RedisTemplate redisTemplate = new RedisTemplate<>();

//为string类型key设置序列器

redisTemplate.setKeySerializer(new StringRedisSerializer());

//为string类型value设置序列器

redisTemplate.setValueSerializer(new

GenericJackson2JsonRedisSerializer());

//为hash类型key设置序列器

redisTemplate.setHashKeySerializer(new StringRedisSerializer());

//为hash类型value设置序列器

redisTemplate.setHashValueSerializer(new

GenericJackson2JsonRedisSerializer());

redisTemplate.setConnectionFactory(redisConnectionFactory);

return redisTemplate;

}

}

修改菜单方法:MenuServiceImpl.java

/**

* 通过用户id获取菜单列表

*

* @return

*/

@Override

public List

Integer adminId = ((Admin)

SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId();

ValueOperations valueOperations =

redisTemplate.opsForValue();

//查询缓存中是否有数据

List

if (CollectionUtils.isEmpty(menus)){

menus = menuMapper.getMenusByAdminId(adminId);

valueOperations.set("menu_"+adminId,menus);

}

return menus;

}

使用Redisrepository访问redis

需要声明一配置项用于启用Repository以及模板

public class RedisConfig {

@Bean

public LettuceConnectionFactory redisConnectionFactory() {

RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("127.0.0.1", 6567);

redisStandaloneConfiguration.setPassword("password");//如果有密码需要通过这个函数设置密码

return new LettuceConnectionFactory(redisStandaloneConfiguration);

}

@Bean

public JedisConnectionFactory jedisConnectionFactory(){

//如果使用jedis作为客户端也需要声明该bean 使用与上面的类似

}

@Bean

public RedisTemplate,?> redisTemplate(){

RedisTemplate template = new RedisTemplate<>();

RedisSerializer stringSerializer = new StringRedisSerializer();

JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();

template.setConnectionFactory(redisConnectionFactory());

template.setKeySerializer(stringSerializer);

template.setHashKeySerializer(stringSerializer);

template.setValueSerializer(stringSerializer);

template.setHashValueSerializer(jdkSerializationRedisSerializer);

template.setEnableTransactionSupport(true);

template.afterPropertiesSet();

return template;

}

}

实例:

entity

import lombok.Data;

import lombok.experimental.Accessors;

import org.springframework.data.redis.core.RedisHash;

import org.springframework.data.redis.core.index.Indexed;

@Accessors(chain = true)

@Data

@RedisHash(value="Student",timeToLive = 10)

public class stu implements Serializable {

public enum Gender{

MALE,FEMALE

}

private String id;

@Indexed

private String name;

private Gender gender;

//RedisHash注解用于声明该实体将被存储与RedisHash中,如果需要使用repository的数据访问形式,这个注解是必须用到的 timetoLive为实体对象在数据库的有效期

//@Indexed用于标注需要作为查询条件的属性

BqacQadHV}

写接口repository:

@Repository

public interface StuRepository extends CrudRepository {

stu findByName(String name);

//自定义查询方法,使用了@Indexed的name属性查询

}

调用:

@SpringBootTest

public class RedisApplicationTests {

@Autowired

StuRepository stuRepository;

@Test

void testSave(){

stu student = new stu().setId("0002").setName("xiaoming").setGender(stu.Gender.FEMALE);

stuRepository.save(student);//根据id更新或者新增记录

}

@Test

void testFindBy(){

//使用主键查询

assert stuRepository.findById("0002").isPresent();

//根据自定义方法查询

assert stuRepository.findByName("xiaoming") !=null;

}

}


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

上一篇:Java中OAuth2.0第三方授权原理与实战
下一篇:Java全面细致讲解final的使用(java中final是什么)
相关文章

 发表评论

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