Java关于RedisTemplate的使用分析

这篇文章主要讲解如何使用Redistemplate以及解释部分源码 对于方法的源码或者方法使用此处没有讲解,之前写过太多类似的 (实战再去查看相关函数,会更加明白深层次的含义) 对于Redis的知识原理以及各个方法的使用之前也有写过很多类似的,可看我之前的文章进行参考:

1. 源码 查看对应的 RedisAutoConfiguration 源码信息 // 配置类 @AutoConfiguration // 只匹配指定的类在类路径上 @ConditionalOnClass(RedisOperations.class) // 开启配置累的注解 @EnableConfigurationProperties(RedisProperties.class) @Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class }) public class RedisAutoConfiguration { // 这个注解比较重要,通过Bean装配到spring容器中 @Bean // 只有在beanfactory中没有包含满足指定需求的bean时才匹配 @ConditionalOnMissingBean(name = "redistemplate") // 只有当指定类的bean已经包含在beanfactory中并且可以确定单个候选bean时才匹配,本质上,主要定义的类型自动连接bean就会成功匹配 @ConditionalOnSingleCandidate(RedisConnectionFactory.class) // 看此处两个都是Object,说明key 以及value 都是什么类型都可以 public Redistemplate<Object, Object> redistemplate(RedisConnectionFactory redisConnectionFactory) { Redistemplate<Object, Object> template = new Redistemplate<>(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean @ConditionalOnSingleCandidate(RedisConnectionFactory.class) // 而此处的key value均为string类型,可看下面的代码 public StringRedistemplate stringRedistemplate(RedisConnectionFactory redisConnectionFactory) { return new StringRedistemplate(redisConnectionFactory); } } StringRedistemplate类型的key以及value为String 源码如下: public class StringRedistemplate extends Redistemplate<String, String> { public StringRedistemplate() { // 设置key以及value,hash的key以及value都是string类型 setKeySerializer(RedisSerializer.string()); setValueSerializer(RedisSerializer.string()); setHashKeySerializer(RedisSerializer.string()); setHashValueSerializer(RedisSerializer.string()); } ..... } 对应的Redistemplate的相关方法,大部分都是 org.springframework.data.redis.core.RedisOperations 这个类的相关方法,主要是RedisOperations这个类提供的API,比如设置key,value以及查出value值等(对应几种数据结构)

以下分别五种数据结构如下:

函数

描述

redistemplate.opsForCluster()

操作集群

redistemplate.opsForGeo()

操作地理位置(对于Geo的实际应用,这篇文章末尾有: Redis框架从入门到学精(全) )

redistemplate.opsForHash()

操作Hash

redistemplate.opsForList()

操作List

redistemplate.opsForValue()

操作字符串

redistemplate.opsForSet()

操作set

redistemplate.opsForZSet()

操作有序set

其中StringRedistemplate操作的方法只不过把对应的类型换作为String类型

关于Redistemplate以及StringRedistemplate,两者是不相通的,两者各自调用各自的数据 类似每一种类型都有很多种方法,大致如下:

根据不同的方法设置不同的参数以及查询获取等 2. 示例代码 引入redis的maven包 具体通过: maven 关于redis data的仓库 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.7.3</version> </dependency> redis的相关连接信息 通过yml的配置文件引入:(redis有设置密码的时候还需要设置密码) spring: redis: host: IP password: pwd port: 6379 如果为application.properties的配置文件配置文件这样引入: spring.redis.host=IP # Redis服务器连接端口 spring.redis.port=6379 通过上面这两个步骤已经就可使用RedisTeplate(通过@Autowired注入) (上面的源码已经对RedisTeplate类通过@Bean装载到spring容器中) 示例代码如下: package com.example.demo; 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.StringRedistemplate; import org.springframework.data.redis.core.ValueOperations; @SpringBoottest class DemoApplicationTests { @Autowired StringRedistemplate stringRedistemplate; @Test public void test() { ValueOperations<String, String> ops = stringRedistemplate.opsForValue(); ops.set("name","码农研究僧"); String s = ops.get("name"); System.out.println(s); } } 截图如下:

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...