Spring-缓存初步认识

Spring-缓存

简单介绍

  • 缓存是一种介于数据永久存储介质和数据应用之间的数据临时存储介质
  • 缓存有效提高读取速度,加速查询效率

spring使用缓存方式

  • 添加依赖
java 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
  • 添加使用缓存的注解
java 复制代码
package com.ustc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@EnableCaching
public class Quick1Application {

    public static void main(String[] args) {
        SpringApplication.run(Quick1Application.class, args);
    }

}
  • 使用缓存,将当前操作结果写入缓存

这里的cacheSpace 表示缓存控件,然后可以从形参中读取id查询所需要的值

java 复制代码
    @Override
    @Cacheable(value = "cacheSpace",key="#id")
    public tbl_book getById(Integer id) {
        return bookMapper.selectById(id);// 调用Mapper查询
    }

手机验证码生成案例

  • 使用CachePut注解 每次生成新的缓存
java 复制代码
    @Override
    @CachePut(value = "smsCode",key = "#tele")
    public String sendCodeToSMS(String tele) {
        // 当前方法的返回值 进入当前key所对应的缓存中
        String code = codeUtils.generator(tele);
        return code;
    }
  • 校验验证码
java 复制代码
    @Override
    public boolean checkCode(SMSCode smsCode) {
        // 取出内存中的验证码 和传递过来的验证码进行比对 如果相同 返回true
        String code = smsCode.getCode();// 内存加载验证码

        String cacheCode = codeUtils.get(smsCode.getTele());// 传递过来的验证码
        return cacheCode.equals(code);
    }
  • 加密验证码
java 复制代码
package com.ustc.controller.utils;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

@Component
public class CodeUtils {

    private String[] patch = {"00000","0000","000","00","0",""};
    public String generator(String tele){
        int hash = tele.hashCode();// 生成哈希值
        int encryption = 20206666;
        // 第一次加密
        long result = hash ^ encryption;
        long nowTime = System.currentTimeMillis();
        result = result ^ nowTime;
        long code = result % 1000000;
        code = code < 0 ?-code :code;
        String codeStr = code + "";
        int len = codeStr.length();// 计算长度
        return patch[len - 1] + codeStr;
    }

    // 获取传递过来的验证码  从缓存中查询
    @Cacheable(value = "smsCode",key = "#tele")
    public String get(String tele){
        return null;
    }

    public static void main(String[] args) {
        System.out.println(new CodeUtils().generator("15005650262"));
    }
}
相关推荐
一 乐5 小时前
婚纱摄影网站|基于ssm + vue婚纱摄影网站系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端
xiaolyuh1237 小时前
Spring 框架 核心架构设计 深度详解
spring·设计模式·spring 设计模式
期待のcode8 小时前
前后端分离项目 Springboot+vue 在云服务器上的部署
服务器·vue.js·spring boot
ProgramHan8 小时前
Spring Boot 3.2 新特性:虚拟线程的落地实践
java·jvm·spring boot
小北方城市网9 小时前
分布式锁实战指南:从选型到落地,避开 90% 的坑
java·数据库·redis·分布式·python·缓存
源码获取_wx:Fegn08959 小时前
基于 vue智慧养老院系统
开发语言·前端·javascript·vue.js·spring boot·后端·课程设计
独断万古他化10 小时前
【Spring 核心: IoC&DI】从原理到注解使用、注入方式全攻略
java·后端·spring·java-ee
likuolei10 小时前
Spring AI框架完整指南
人工智能·python·spring
毕设源码_郑学姐10 小时前
计算机毕业设计springboot基于HTML5的酒店预订管理系统 基于Spring Boot框架的HTML5酒店预订管理平台设计与实现 HTML5与Spring Boot技术驱动的酒店预订管理系统开
spring boot·后端·课程设计
梵得儿SHI10 小时前
(第四篇)Spring AI 核心技术攻坚:多轮对话与记忆机制,打造有上下文的 AI
java·人工智能·spring·springai生态·上下文丢失问题·三类记忆·智能客服实战案