springboot中redis的简单操作

简单示例:

application.properties:

## Redis数据库索引(使用的数据库(0-15),默认为0)
spring.redis.database=0

## 连接URL,将覆盖主机,端口和密码(用户将被忽略),
## 例如:redis://user:password@example.com:6379
spring.redis.url=
## Redis服务器主机: 127.0.0.1
spring.redis.host=localhost
## Redis服务器连接密码(默认为空)
spring.redis.password=
## Redis服务器连接端口
spring.redis.port=6379

## 启用SSL支持
spring.redis.ssl=false

## 连接超时时间(毫秒)
spring.redis.timeout=0
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0

## cluster	[ˈklʌstə(r)] 集群
spring.redis.cluster.nodes=
spring.redis.cluster.max-redirects=

## sentinel	[ˈsentɪnl] 哨兵
spring.redis.sentinel.master=
spring.redis.sentinel.nodes=

## lettuce	[ˈletɪs]   莴苣、生菜
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
spring.redis.lettuce.shutdown-timeout=100

pom.xml:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

定义redis的操作类

@Repository
public class RedisDao {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public void setKey(String key, String value) {
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        valueOperations.set(key, value, 1, TimeUnit.MINUTES);//1分钟过期
    }

    public String getValue(String key) {
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        return valueOperations.get(key);
    }
}

测试:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {

    @Test
    public void contextLoads() {
    }

    public static Logger logger = LoggerFactory.getLogger(DemoApplicationTests.class);

    @Autowired
    RedisDao redisDao;

    @Test
    public void testRedis() {
        redisDao.setKey("name", "xq");
        redisDao.setKey("age", "18");
        logger.info(redisDao.getValue("name"));
        logger.info(redisDao.getValue("age"));
    }

}

其他

StringRedisTemplate 与 RedisTemplate关系

public class StringRedisTemplate extends RedisTemplate<String, String> {

RedisAutoConfiguration

@Configuration
@ConditionalOnClass(RedisOperations.class)
@EnableConfigurationProperties(RedisProperties.class)
@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
public class RedisAutoConfiguration {

	@Bean
	@ConditionalOnMissingBean(name = "redisTemplate")
	public RedisTemplate<Object, Object> redisTemplate(
			RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

	@Bean
	@ConditionalOnMissingBean
	public StringRedisTemplate stringRedisTemplate(
			RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
		StringRedisTemplate template = new StringRedisTemplate();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}

}

@ConditionalOnClass :
该注解的参数对应的类必须存在,否则不解析该注解修饰的配置类;

@ConditionalOnMissingBean :
该注解表示,如果存在它修饰的类的bean,则不需要再创建这个bean;
可以给该注解传入参数例如@ConditionOnMissingBean(name = “example”),
这个表示如果name为“example”的bean存在,这该注解修饰的代码块不执行。
判断是否执行初始化代码,即如果用户已经创建了bean,则相关的初始化代码不再执行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot使用Redis有多种方式,下面是一个简单的示例: 首先,确保在`pom.xml`文件添加Redis的依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 然后,在`application.properties`(或`application.yml`)文件配置Redis连接信息: ```properties spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= ``` 接下来,我们可以使用Spring Data Redis提供的`RedisTemplate`进行Redis操作。例如,存储和获取一个字符串值: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; @Component public class RedisExample { @Autowired private RedisTemplate<String, String> redisTemplate; public void setValue(String key, String value) { redisTemplate.opsForValue().set(key, value); } public String getValue(String key) { return redisTemplate.opsForValue().get(key); } } ``` 上面的示例,我们使用了`RedisTemplate`来执行Redis操作。通过自动注入,我们可以在Spring Boot应用程序的其他类使用`RedisExample`类。在其他地方,我们可以通过调用`setValue`方法来存储一个键值对,通过调用`getValue`方法来获取对应的值。 这只是RedisSpring Boot的基本用法示例,你可以根据自己的需求进行更复杂的操作,如存储对象、使用Hash等。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值