java memcached缓存怎么测试_Spring Boot:使用Memcached缓存

综合概述

Memcached是一个自由开源的,高性能,分布式内存对象缓存系统。Memcached基于内存的key-value存储,用来存储小块的任意数据,这些数据可以是数据库调用、API调用或者是页面渲染的结果。通过Memcached缓存数据库查询结果,可以有效地减少数据库访问次数,进而提高动态Web应用的速度。虽然Memcached的守护进程是用C写的,但是客户端可以用任何语言来编写,并通过Memcached协议与守护进程进行通信。

因为Spring Boot暂时还没有提供 Memcached相关的支持包,因此需要我们通过集成第三方提供的Memcached客户端来实现。Spymemcached是官方推出的一个Memcached  Java客户端,使用NIO实现,异步、单线程,在性能上表现出色,广泛应用于Java + Memcached项目中。

实现案例

接下来,我们就用一个简单的案例来说明在Spring Boot中如何使用Memcached缓存技术。

首先,需要安装Memcached,教程很多,这里不再赘述。可以参考:Memcached安装教程。

生成项目模板

为方便我们初始化项目,Spring Boot给我们提供一个项目模板生成网站。

2.  根据页面提示,选择构建工具,开发语言,项目信息等。

68c10ce8f42686623141d8c84b784093.png

3.  点击 Generate the project,生成项目模板,生成之后会将压缩包下载到本地。

4.  使用IDE导入项目,我这里使用Eclipse,通过导入Maven项目的方式导入。

9576ab80d8a4c04fa94286443b3818fe.png

添加相关依赖

清理掉不需要的测试类及测试依赖,添加 Maven 相关依赖,这里需要添加上web、swagger和spymemcached的依赖,Swagger是为了方便接口测试。

对于spymemcached的支持,其实只要如下这个依赖包就可以了。

net.spy

spymemcached

2.12.3

完整的POM文件内容如下。

pom.xml

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.5.RELEASE

com.louis.springboot

demo

0.0.1-SNAPSHOT

demo

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-web

io.springfox

springfox-swagger2

2.9.2

io.springfox

springfox-swagger-ui

2.9.2

net.spy

spymemcached

2.12.3

org.springframework.boot

spring-boot-maven-plugin

添加相关配置

1.添加swagger 配置

添加一个swagger 配置类,在工程下新建 config 包并添加一个 SwaggerConfig 配置类,除了常规配置外,加了一个令牌属性,可以在接口调用的时候传递令牌。

SwaggerConfig.java

package com.louis.springboot.demo.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.builders.PathSelectors;

import springfox.documentation.builders.RequestHandlerSelectors;

import springfox.documentation.service.ApiInfo;

import springfox.documentation.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration

@EnableSwagger2public classSwaggerConfig {

@BeanpublicDocket createRestApi(){return newDocket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())

.select()

.apis(RequestHandlerSelectors.any())

.paths(PathSelectors.any()).build();

}privateApiInfo apiInfo(){return newApiInfoBuilder()

.title("Swagger API Doc")

.description("This is a restful api document of Swagger.")

.version("1.0")

.build();

}

}

2.在配置文件添加memcache的主机端口信息

application.properties

memcache.ip=127.0.0.1memcache.port=11211

3.添加一个MemcacheConfig配置类,读取主机端口并构造一个MemcachedClient。

MemcacheConfig.java

package com.louis.springboot.demo.config;

import java.io.IOException;

import java.net.InetSocketAddress;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import net.spy.memcached.MemcachedClient;

@Configurationpublic classMemcacheConfig {

@Value("${memcache.ip}")privateString ip;

@Value("${memcache.port}")private intport;

@BeanpublicMemcachedClient getClient() {

MemcachedClient memcachedClient= null;try{

memcachedClient= new MemcachedClient(newInetSocketAddress(ip, port));

}catch(IOException e) {

e.printStackTrace();

}returnmemcachedClient;

}

}

编写业务接口

编写一个业务控制器,通过MemcachedClient实现对缓存的设置和读取。

MemcacheController.java

package com.louis.springboot.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import net.spy.memcached.MemcachedClient;

import net.spy.memcached.internal.OperationFuture;

@RestControllerpublic classMemcacheController {

@AutowiredprivateMemcachedClient memcachedClient;

@GetMapping("/memcache")publicString memcache() throws InterruptedException {//放入缓存, 如下参数key为name,值为louis,过期时间为5000,单位为毫秒

OperationFuture flag = memcachedClient.set("name", 5000, "louis");//取出缓存

Object value = memcachedClient.get("name");

System.out.println(value);//多线程睡眠5秒,让

Thread.sleep(5000);

value= memcachedClient.get("name");

System.out.println(value);return "success";

}

}

编译运行测试

1.  右键项目 -> Run as -> Maven install,开始执行Maven构建,第一次会下载Maven依赖,可能需要点时间,如果出现如下信息,就说明项目编译打包成功了。

6f5cfff60b299d72049572f9c6a2b8bf.png

2.  右键文件 DemoApplication.java -> Run as -> Java Application,开始启动应用,当出现如下信息的时候,就说明应用启动成功了,默认启动端口是8080。

bea9456e4ba32338f66d33da90e0e43a.png

d644a5be7d420d6a17d5fa2fc5ae88fd.png

4.调用memcache接口,测试缓存存取操作,查看控制台输出结果。

louisnull

写入数据时设置name=louis,过期时间为5秒,第一次获取name结果为louis,在睡眠5秒之后第二次获取name时,因为过期返回null。

相关导航

源码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值