spring boot & redis单机缓存

一、简介

同Ehcache 一样,classpath 下存在redis 配置,就默认使用RedisCacheManager 作为缓存提供者。

二、流程

1.新建spring boot Web 项目,pom.xml 添加依赖

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

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
	<exclusions>
		<exclusion>
			<groupId>io.lettuce</groupId>		
			<artifactId>lettuce.core</artifactId>		
		</exclusion>
	</exclusions>
</dependency>

<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
</dependency>

2.配置application.yml

spring:
  redis:
    host: 192.168.243.133
  cache:
    redis:
      time-to-live: 180s
      

3.User.java

package com.vincent.po;

import java.io.Serializable;

public class User implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + "]";
	}

}

4.UserService.java

package com.vincent.service;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.vincent.po.User;

@Service
public class UserService {
	@Cacheable(cacheNames = {"default_cache"},key = "#id")
	public User get(String id) {
		User user = new User();
		user.setName("Vincent");
		user.setAge(26);
		System.out.println("cache method...");
		return user;
	}
	
	@CachePut(cacheNames = {"default_cache"},key = "#id")
	public User update(String id){
		
		User user = new User();
		user.setName("Vincent-" + id);
		user.setAge(26);
		System.out.println("update method...");
		return user;
	}
	
	@CacheEvict(cacheNames = {"default_cache"},key = "#id")
	public void delete(String id) {
		System.out.println("delete method...");
	}
}

5.TestController.java

package com.vincent.controller;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.vincent.po.User;
import com.vincent.service.UserService;

@RestController
public class TestController {
	
	@Autowired 
	private UserService userService;
	
	@GetMapping("/test/add")
	public User add(String id,HttpServletRequest request) {
		return userService.get(id);
	}
	
	@GetMapping("/test/update")
	public User update(String id) {
		return userService.update(id);
	}

	
	@GetMapping("/test/del")
	public void del(String id) {
		userService.delete(id);
	}
	
}

6.@SpringBootApplication 修饰的类添加@EnableCaching 注解

三、测试

1.访问 http://localhost:8080/test/add?id=test-cache

在这里插入图片描述

查看redis 数据信息
在这里插入图片描述

2.访问 http://localhost:8080/test/update?id=test-cache 修改缓存数据

在这里插入图片描述
在这里插入图片描述

3.访问 http://localhost:8080/test/del?id=test-cache 删除缓存数据
在这里插入图片描述

四、总结

1.redis 中的key 都有一个前缀,默认是"缓存名::"

2.redis 中的缓存信息非常容易配置过期时间(application.yml 中配置spring.cache.redis.time-to-live )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 可以通过集成Spring Data Redis来添加Redis缓存Spring Data RedisSpring提供的一个用于操作Redis的开源库。要添加Redis缓存,可以按照以下几个步骤进行操作。 第一步,添加依赖: 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 这将自动引入Spring Data Redis及其相关依赖。 第二步,配置Redis连接信息: 在application.properties(或application.yml)文件中配置Redis连接信息,如下所示: ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.database=0 ``` 可以根据实际情况修改Redis的主机名、端口号、密码和数据库索引。 第三步,使用Redis缓存: 在需要使用Redis缓存的地方,可以使用Spring提供的注解进行缓存操作。例如,可以使用`@Cacheable`注解标记一个方法,表示该方法的结果将被缓存: ```java @Cacheable("myCache") public String getValue(String key) { // 从数据库或其他数据源中获取数据 return value; } ``` 在上面的例子中,`myCache`是缓存的名称,可以根据实际需要进行命名。当调用`getValue`方法时,如果缓存中已经有对应的数据,则直接从缓存中获取数据,否则会执行方法体内的代码,并将结果缓存起来。 需要注意的是,为了使`@Cacheable`注解生效,还需要在启动类上添加`@EnableCaching`注解,以启用缓存功能。 通过以上步骤,就可以在Spring Boot应用中添加Redis缓存,并使用Redis作为数据的缓存存储。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值