JavaWeb_瑞吉外卖_项目优化Day10-Spring Cache

Git管理现有项目

  • 提交步骤:
    1. 生成空仓库, 不要加任何文件
    2. 右键项目, add整个项目
    3. commit和push, push时添加远程仓库地址
  • 新建分支步骤:
    1. 在本地新建一个分支
    2. push

环境搭建

maven坐标

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

yml配置文件

spring:
  redis:
    host: localhost
    port: 6379
    # password:
    database: 0

配置类

@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();

        //默认的Key序列化器为:JdkSerializationRedisSerializer
        redisTemplate.setKeySerializer(new StringRedisSerializer()); // key序列化
        //redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // value序列化
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}

缓存短信验证码

  • 注入redis对象
    @Autowired
    private RedisTemplate redisTemplate;
    
  • 将验证码保存到redis, 有效期5分钟
    redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
    
  • 获取验证码, 登录成功后删除验证码
    // 从redis中获取保存的验证码
    Object codeInSession = redisTemplate.opsForValue().get(phone);
    // 登录成功则删除验证码
    redisTemplate.delete(phone);
    

缓存菜品数据

  • 获取缓存数据
    List<DishDto> dishDtoList = null;
    // 动态构造key
    String key = "dish_"+dish.getCategoryId()+"_"+dish.getStatus();
    
    // 先从redis中获取缓存数据
    dishDtoList = (List<DishDto>) redisTemplate.opsForValue().get(key);
    if(dishDtoList != null){
        // 如果存在, 直接返回, 无需查询数据库
        return R.success(dishDtoList);
    }
    
    ... 
    
    // 将查询到的菜品数据缓存到redis
    redisTemplate.opsForValue().set(key, dishDtoList);
    
  • 删除缓存数据
    // 清理所有菜品的缓存数据
    // Set keys = redisTemplate.keys("dish_*");
    // redisTemplate.delete(keys);
    
    // 清理某个分类下的菜品缓存数据
    String key = "dish_"+dishDto.getCategoryId()+"_1";
    redisTemplate.delete(key);
    

Spring Cache

介绍

  • Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能。
  • Spring Cache提供了一层抽象,底层可以切换不同的cache实现。具体就是通过CacheManager接口来统一不同的缓存技术。
  • CacheManager是Spring提供的各种缓存技术抽象接口。
  • 针对不同的缓存技术需要实现不同的CacheManager:
    CacheManager描述
    EhCacheCacheManager使用EhCache作为缓存技术
    GuavaCacheManager使用Google的GuavaCache作为缓存技术
    RedisCacheManager使用Redis作为缓存技术

常用注解

注解功能
@EnableCaching开启缓存注解功能
@Cacheable在方法执行前spring先查看缓存中是否有数据, 如果有数据, 则直接返回缓存数据; 若没有数据, 调用方法并将方法返回值放到缓存中
@CachePut将方法的返回值放到缓存中
@CacheEvict将一条或多条数据从缓存中删除
  • 在SpringBoot项目中, 使用缓存技术只需要在项目中导入相关缓存技术的依赖包, 并在启动类上使用@EnableCaching开启换成支持即可
  • 例如, 使用Redis作为缓存技术, 只需要导入Spring data Redis的maven坐标即可

使用方式

maven依赖

<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>
</dependency>

设置缓存有效期

cache:
  redis:
    time-to-live: 1800000 # 设置缓存有效期 

注解

@CachePut(value = "userCache", key = "#user.id")
@CacheEvict(value = "userCache", key = "#user.id")
@Cacheable(value = "userCache", key = "#id", unless = "#result == null")
@Cacheable(value = "userCache", key = "#user.id+'_'+#user.name", unless = "#result == null")

注: @Cacheable的condition属性不能使用#result

缓存套餐数据

@CacheEvict(value = "setmealCache", allEntries = true)
public R<String> save(@RequestBody SetmealDto setmealDto)
@Cacheable(value = "setmealCache", key = "#setmeal.categoryId+'_'+#setmeal.status")
public R<List<Setmeal>> list(Setmeal setmeal)

来源

黑马程序员. 瑞吉外卖项目

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Java EE 项目的目录结构可以根据具体的需求进行灵活设计,但一般情况下,推荐使用以下的标准目录结构: ``` project ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ └── example │ │ │ ├── controller │ │ │ ├── dao │ │ │ ├── entity │ │ │ ├── service │ │ │ └── util │ │ ├── resources │ │ │ ├── mapper │ │ │ └── db.properties │ │ └── webapp │ │ ├── WEB-INF │ │ │ ├── classes │ │ │ ├── lib │ │ │ └── web.xml │ │ ├── css │ │ ├── js │ │ ├── images │ │ └── index.jsp │ └── test │ ├── java │ └── resources ├── target ├── pom.xml └── README.md ``` 其中,各个目录的作用如下: - `src/main/java`:存放项目Java 源代码,按照包名分层,一般包括 `controller`、`dao`、`entity`、`service` 和 `util` 等包; - `src/main/resources`:存放项目的配置文件和资源文件,一般包括数据库连接配置文件 `db.properties`、MyBatis 的 mapper 文件等; - `src/main/webapp`:存放 Web 应用的 Web 资源,包括 JSP 页面、CSS 样式表、JavaScript 脚本等; - `src/test/java`:存放项目的测试代码; - `src/test/resources`:存放测试代码所需要的资源文件; - `target`:存放编译后的 .class 文件、打包后的 .war 文件等; - `pom.xml`:Maven 项目管理工具的配置文件; - `README.md`:项目说明文件。 以上是一种常见的 Java EE 项目目录结构,但并不是唯一的标准。在实际开发中,可以根据项目的具体需求进行合理的调整和修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y_cen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值