spring boot整合cache使用memcached 优化将配置信息放入 application中管理

上文 spring boot整合cache使用memcached

我们简单做了个 spring boot 整合cache 使用 memcached 缓存的案例

但 我们是将地址这类信息 放在了config 目录下的一个 配置类中了

这样 可维护性肯定是很低的

其实 memcached 是有一系列配置的

我们还是正确将 配置信息 写进 application 中

首先 我们在 config 目录下再创建一个类 名字随意 开心就好 我这里取名叫 XMemcachedProperties

参考代码如下

java 复制代码
package com.example.webdom.config;

import org.springframework.stereotype.Component;

@Component

public class XMemcachedProperties {
    //服务地址
    private String servers;
    //连接池数量
    private int poolsize;
    //超时时间
    private long opTimeout;

    public String getServers() {
        return servers;
    }

    public int getPoolsize() {
        return poolsize;
    }

    public long getOpTimeout() {
        return opTimeout;
    }

    public void setServers(String servers) {
        this.servers = servers;
    }

    public void setPoolsize(int poolsize) {
        this.poolsize = poolsize;
    }

    public void setOpTimeout(long opTimeout) {
        this.opTimeout = opTimeout;
    }
}

这里 我们定义了三个属性 然后分别定义了 get set函数 然后 Component 将它写为一个bean

因为只有bean外面才能拿到 然后 get set 方便外面处理它的属性

这三个值 对应 我们 memcached 的 服务器地址 连接池数量 超时时间

然后在application中配置这三个属性

javascript 复制代码
memcached:
  servers: localhost:11211
  poolsize: 10
  opTimeout: 3000

这相当于我们自己自定义的属性 我们后面要去类里读 这个是之前讲过的东西了

这里 我们给 XMemcachedProperties 类加上 ConfigurationProperties prefix值指向 我们 application的memcached属性

然后 我们封装这些属性 是为了给我们之前的 XMemcachedconfig 用的

先将它条件装配进来

getServers 肯定是放到 XMemcachedClientBuilder 中

然后 其他几个属性 我们 memcachedclientBuilder 有非常多的set属性

我们这里要的是 setConnectionPoolSize 连接池数量 setOpTimeout 超时时间

参考代码如下

java 复制代码
package com.example.webdom.config;

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 net.rubyeye.xmemcached.MemcachedClient;

import java.io.IOException;

@Configuration
public class XMemcachedconfig {

    @Autowired
    private XMemcachedProperties XMemcachedProperties;

    @Bean
    public MemcachedClient getMemcachedclient() throws IOException {
        XMemcachedClientBuilder memcachedclientBuilder = new XMemcachedClientBuilder(XMemcachedProperties.getServers());
        memcachedclientBuilder.setConnectionPoolSize(XMemcachedProperties.getPoolsize());
        memcachedclientBuilder.setOpTimeout(XMemcachedProperties.getOpTimeout());
        MemcachedClient memcachedclient = memcachedclientBuilder.build();
        return memcachedclient;
    }
}

memcachedclientBuilder里面有哪些set 你都可以自己去看 里面可以配的东西多了去了

然后 我们运行项目

对我们运行是没有任何影响的

相关推荐
就是帅我不改19 分钟前
10万QPS压垮系统?老司机一招线程池优化,让性能飞起来!
后端·面试·github
uzong19 分钟前
系统稳定性保障:研发规约V1.0
后端
Ray6620 分钟前
log4j2.xml配置文件详解
后端
Frank_zhou23 分钟前
Easy-Es 架构设计详解
后端·elasticsearch
狗头大军之江苏分军32 分钟前
Meta万人裁员亲历者自述:小扎尝到了降本的甜头
前端·后端·github
Rysxt_1 小时前
Spring Boot Gateway 教程:从入门到精通
spring boot·网关·gateway
Jagger_1 小时前
SonarQube:提升代码质量的前后端解决方案
前端·后端·ai编程
草履虫建模1 小时前
在 RuoYi 中接入 3D「园区驾驶舱」:Vue2 + Three.js + Nginx
运维·开发语言·javascript·spring boot·nginx·spring cloud·微服务
在逃牛马1 小时前
【Uni-App+SSM 宠物项目实战】Day6:MP 实体类与 Mapper 生成
后端
remaindertime1 小时前
(九)Spring Cloud Alibaba 2023.x:微服务接口文档统一管理与聚合
后端·spring cloud·微服务