SpringBoot配置第三方专业缓存技术Memcached 下载 安装 整合测试 2024年5000字详解

简介: SpringBoot配置第三方专业缓存技术Memcached 下载 安装 整合测试 2024年5000字详解

Memcached下载和安装

是一个国内使用量还是比较大的技术

打开文件夹

我们需要在命令行窗口启动

注意要以管理员方式运行

先尝试进入指定文件

然后又再次运行

下载

memcached.exe -d install

启动

memcached.exe -d start

停止

memcached.exe -d stop

memcached.exe -d install
memcached.exe -d start
memcached.exe -d stop

我们打开任务管理器 发现成功运行

Memcached缓存技术

问题是springboot提供整合技术

还没有纳入到整合中

需要使用硬编码的方式实现给客户端初始化管理

我们打开idea

首先得导入坐标

<!--        memcached的依赖-->
        <dependency>
            <groupId>com.google.code.maven-play-plugin.spy</groupId>
            <artifactId>memcached</artifactId>
            <version>2.4.2</version>
        </dependency>

因为springboot没有整合

根本没有配置

所以我们直接采取硬编码的形式

找到业务层的实现类

准备书写代码

我们做一个配置类

目的是为了让Mencached被spring容器加载

配置一个客户端对象

然后加载为spring容器的bean

package com.example.demo.config;
 
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.spy.memcached.MemcachedClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.io.IOException;
 
@Configuration
public class XMemcachedConfig {
 
    @Bean
    public MemcachedClient getmemcachedClient() throws IOException {
        //配置服务器端口
        MemcachedClientBuilder memcachedClientBuilder = new XMemcachedClientBuilder("localhost:11211");
        //构建启动
        MemcachedClient memcachedClient=memcachedClientBuilder.build();
        return memcachedClient;
    }
 
}

我们直接进行依赖注入

我们接下来补全业务层的代码

书写完毕

package com.example.demo.service.impl;
 
import com.example.demo.domain.SMSCode;
import com.example.demo.service.SMSCodeService;
import com.example.demo.utils.CodeUtils;
import net.rubyeye.xmemcached.MemcachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
@Service
public class SMSCodeServiceImpl implements SMSCodeService {
 
    @Autowired
    private CodeUtils codeUtils;
 
    @Autowired
    private MemcachedClient memcachedClient;
 
    //以下是Springboot中使用xmemcached
 
    @Override
    public String sendCodeToSMS(String tele) {
        String code=codeUtils.generator(tele);
        try{
            memcachedClient.set(tele,0,code);
        }catch (Exception e){
            e.printStackTrace();
        }
        return code;
    }
 
    @Override
    public boolean checkCode(SMSCode smsCode) {
        String code=null;
        try{
            code=memcachedClient.get(smsCode.getTele().toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return smsCode.getCode().equals(code);
    }
}

我们要去改一下缓存的注释

我们把之前采用的缓存方案全部注释掉

# 专门用来配置的对象datasource
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
      username: root
      password: 123456
  devtools:
    restart:
      # 设置不参与热部署的文件或文件夹
      exclude: static/**,public/**,config/application.yml

启动成功

去postman发起请求测试

我们通过修改客户端的数值

能改变一些设置

如设置缓存失效时间

硬编码

就是手搓客户端对象

然后交给spring容器管理后

在业务层的实现类注入

使用缓存的时候使用set

获取缓存数据的时候使用get

但是我们这边还有个问题

就是在书写客户端的时候

这个数据应该从配置文件里去处理

先写一个类

这个类有成员属性 代表的是各种配置信息

我们需要做的是自定义配置

memcached:
  servers: localhost:11211
  poolSize: 10
  opTimeout: 3000

然后在类里面去读取

package com.example.demo.config;
 
 
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
@Component
@ConfigurationProperties(prefix = "memcached")
@Data
public class XMemcachedProperties {
    private String servers;
    private int poolSize;
    private long opTimeout;
}

这样我们的类就能成功加载

@Component注解又能让这个类被spring容器管理

我们在这边直接注入就行

只不过是数值换了一个地方加载

package com.example.demo.config;
 
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import java.io.IOException;
 
@Configuration
public class    XMemcachedConfig {
 
    @Autowired
    private XMemcachedProperties memcachedProperties;
 
    @Bean
    public MemcachedClient getmemcachedClient() throws IOException {
        //配置服务器端口
        MemcachedClientBuilder memcachedClientBuilder = new XMemcachedClientBuilder(memcachedProperties.getServers());
        //配置数据库连接池最大连接量
        memcachedClientBuilder.setConnectionPoolSize(memcachedProperties.getPoolSize());
        //配置缓存时间
        memcachedClientBuilder.setConnectTimeout(memcachedProperties.getOpTimeout());
        //构建启动
        MemcachedClient memcachedClient=memcachedClientBuilder.build();
        return memcachedClient;
    }
 
}

发起请求

成功

目录
相关文章
|
2天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的高中信息技术课程在线测试系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的高中信息技术课程在线测试系统的详细设计和实现(源码+lw+部署文档+讲解等)
8 0
|
4天前
|
测试技术 数据安全/隐私保护 索引
基于SpringBoot+Vue大学生体质测试管理系统【源码+论文+演示视频+包运行成功】(2)
基于SpringBoot+Vue大学生体质测试管理系统【源码+论文+演示视频+包运行成功】
9 0
|
4天前
|
Java 关系型数据库 MySQL
基于SpringBoot+Vue大学生体质测试管理系统【源码+论文+演示视频+包运行成功】(1)
基于SpringBoot+Vue大学生体质测试管理系统【源码+论文+演示视频+包运行成功】
15 0
|
4天前
|
缓存 监控 Java
Spring Boot中使用Ehcache进行缓存管理
Spring Boot中使用Ehcache进行缓存管理
|
5天前
|
缓存
如何彻底卸载VSCode及其原来的插件配置缓存
如何彻底卸载VSCode及其原来的插件配置缓存
8 0
|
5天前
|
缓存 JavaScript
vue 页面缓存 keep-alive(含配置清除页面缓存 exclude,局部缓存,动态缓存,路由控制缓存 $route.meta.keepAlive)
vue 页面缓存 keep-alive(含配置清除页面缓存 exclude,局部缓存,动态缓存,路由控制缓存 $route.meta.keepAlive)
8 0
|
6天前
|
缓存 NoSQL Java
Spring Boot整合Redis缓存的最佳实践
Spring Boot整合Redis缓存的最佳实践
|
6天前
|
缓存 Java Spring
教程:Spring Boot中集成Memcached的详细步骤
教程:Spring Boot中集成Memcached的详细步骤
|
9天前
|
缓存 NoSQL Java
Spring Boot中的分布式缓存方案
Spring Boot中的分布式缓存方案
|
9天前
|
IDE Java 测试技术
Springboot单元测试步骤
Springboot单元测试步骤
11 0