前言

memcached 是非常简单的缓存组件,是简化版的redis,常用于缓存数据。

准备工作

安装memcached:Memcached 教程

推荐使用docker安装:

docker run --name memcached -p 11211:11211 -d daocloud.io/library/memcached:1.5.1-alpine

如何自定义memcached-spring-boot-starter

一、定义memcached-spring-boot-autoconfigure模块

  1. 创建springboot模块(无需任何组件)

  2. 引入依赖

      <!--memcached-->
        <dependency>
            <groupId>spy</groupId>
            <artifactId>memcached</artifactId>
            <version>2.4rc1</version>
            <scope>compile</scope>
        </dependency>
  1. 编写代码
package com.lcz.memcached.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "memcached")
public class MemcachedProperties {

    /**
     * 多主机配置
     */
    private String[] memcacheds;

    public void setMemcacheds(String[] memcacheds) {
        this.memcacheds = memcacheds;
    }

    public String[] getMemcacheds() {
        return memcacheds;
    }
}
package com.lcz.memcached.config;

import net.spy.memcached.MemcachedClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

@Configuration  //设置为配置类
@EnableConfigurationProperties(MemcachedProperties.class) //获取到配置信息
@ConditionalOnClass(MemcachedClient.class)
public class MemcachedAutoConfiguration {


    @Bean
    @ConditionalOnMissingBean(value=MemcachedClient.class,name="memcachedClient")
    @ConditionalOnProperty(name="memcached.enabled",havingValue = "true",matchIfMissing=true)
    public MemcachedClient memcachedClient(MemcachedProperties memcachedProperties) {
        try {
            String[] memcacheds = memcachedProperties.getMemcacheds();
            if (null != memcacheds && memcacheds.length > 0) {
                List<InetSocketAddress> inetSocketAddresses = new ArrayList<>();
                for (String str : memcacheds) {
                    Assert.notNull(str, "memcacheds config is error.");
                    String[] array = str.split(":");
                    Assert.state(array.length == 2, "memcacheds config is error.");

                    String ip = array[0];
                    String port = array[1];
                    inetSocketAddresses.add(new InetSocketAddress(ip, Integer.parseInt(port)));
                }
                MemcachedClient memcachedClient = new MemcachedClient(inetSocketAddresses);
                return memcachedClient;
            }else{
                return new MemcachedClient(new InetSocketAddress("localhost", 11211));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
  1. 配置扫描META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.lcz.memcached.config.MemcachedAutoConfiguration

在这里插入图片描述
4. 删除多余的文件和代码(如图)
在这里插入图片描述
在这里插入图片描述

二、定义memcached-spring-boot-starter模块

    1. 创建springboot模块(无需任何组件)
  1. 引入依赖,刚刚定义的模块

<dependency>
            <groupId>com.lcz</groupId>
            <artifactId>memcached-spring-boot-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
  1. 删除多余的文件和代码
    在这里插入图片描述
    在这里插入图片描述

三、测试引用

  1. 在其它springboot工程里引用刚刚定义的memcached-spring-boot-starter
<dependency>
            <groupId>com.lcz</groupId>
            <artifactId>memcached-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
  1. 配置application.yml
#memcached 缓存
memcached:
  memcacheds:
    - 106.13.2.249:11211
    - 106.13.2.249:11212
# 是否开启缓存,默认true
#  enabled: true
  1. 注入memcachedClient试一下
    @Autowired
    private MemcachedClient memcachedClient;

在这里插入图片描述
4. 运行
在这里插入图片描述

测试成功,结束。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐