江苏网站建设哪家快点,对电子商务网站建设与管理的心得,网络市场调研,福州网站建设招商SpringBoot集成Memcached
1、Memcached 介绍
Memcached 是一个高性能的分布式内存对象缓存系统#xff0c;用于动态Web应用以减轻数据库负载。它通过在内存中
缓存数据和对象来减少读取数据库的次数#xff0c;从而提高动态、数据库驱动网站的速度。Memcached基于一个存储…SpringBoot集成Memcached
1、Memcached 介绍
Memcached 是一个高性能的分布式内存对象缓存系统用于动态Web应用以减轻数据库负载。它通过在内存中
缓存数据和对象来减少读取数据库的次数从而提高动态、数据库驱动网站的速度。Memcached基于一个存储
键/值对的hashmap。其守护进程daemon 是用C写的但是客户端可以用任何语言来编写并通过
memcached协议与守护进程通信。
因为 Spring Boot 没有针对 Memcached 提供对应的组建包因此需要我们自己来集成。官方推出的 Java 客户端
Spymemcached 是一个比较好的选择之一。
Spymemcached 最早由 Dustin Sallings 开发Dustin 后来和别人一起创办了 Couchbase (原NorthScale)职位
为首席架构师。2014 加入 Google。Spymemcached 是一个采用 Java 开发的异步、单线程的 Memcached 客户
端 使用 NIO 实现。Spymemcached 是 Memcached 的一个流行的 Java client 库性能表现出色广泛应用于
Java Memcached 项目中。
2、Windows下安装Memcached
官网上并未提供 Memcached 的 Windows 平台安装包我们可以使用以下链接来下载你需要根据自己的系统平
台及需要的版本号点击对应的链接下载即可 32位系统 1.2.5版本http://static.runoob.com/download/memcached-1.2.5-win32-bin.zip 32位系统 1.2.6版本http://static.runoob.com/download/memcached-1.2.6-win32-bin.zip 32位系统 1.4.4版本http://static.runoob.com/download/memcached-win32-1.4.4-14.zip 64位系统 1.4.4版本http://static.runoob.com/download/memcached-win64-1.4.4-14.zip 32位系统 1.4.5版本http://static.runoob.com/download/memcached-1.4.5-x86.zip 64位系统 1.4.5版本http://static.runoob.com/download/memcached-1.4.5-amd64.zip
在 1.4.5 版本以前 memcached 可以作为一个服务安装而在 1.4.5 及之后的版本删除了该功能。因此我们以下介
绍两个不同版本 1.4.4 及 1.4.5的不同安装方法。
2.1 memcached 1.4.5 版本安装
1、解压下载的安装包到指定目录。
2、在 1.4.5 版本以前 memcached 可以作为一个服务安装使用管理员权限运行以下命令
c:\memcached\memcached.exe -d install3、然后我们可以使用以下命令来启动和关闭 memcached 服务
c:\memcached\memcached.exe -d start
c:\memcached\memcached.exe -d stop4、如果要修改 memcached 的配置项, 可以在命令行中执行 regedit.exe 命令打开注册表并找到
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached 来进行修改。
如果要提供 memcached 使用的缓存配置可以修改 ImagePath 为:
c:\memcached\memcached.exe -d runservice -m 512-m 512 意思是设置 memcached 最大的缓存配置为512M。
此外我们还可以通过使用 memcached.exe -h 命令查看更多的参数配置。
5、如果我们需要卸载 memcached 可以使用以下命令
c:\memcached\memcached.exe -d uninstall2.2 memcached 1.4.5 版本安装
1、解压下载的安装包到指定目录。
2、在 memcached1.4.5 版本之后memcached 不能作为服务来运行需要使用任务计划中来开启一个普通的
进程在 window 启动时设置 memcached自动执行。
我们使用管理员身份执行以下命令将 memcached 添加来任务计划表中
schtasks /create /sc onstart /tn memcached /tr c:\memcached\memcached.exe -m 512注意-m 512 意思是设置 memcached 最大的缓存配置为512M。
注意我们可以通过使用 c:\memcached\memcached.exe -h 命令查看更多的参数配置。
3、如果需要删除 memcached 的任务计划可以执行以下命令
schtasks /delete /tn memcached3、整合Memcached
3.1 pom依赖
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.0.4.RELEASE/versionrelativePath//parentgroupIdcom.example/groupIdartifactIdspring-boot-memcache-spymemcached/artifactIdversion0.0.1-SNAPSHOT/versionnamespring-boot-memcache-spymemcached/namedescriptionspring-boot-memcache-spymemcached/descriptionpackagingjar/packagingpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdnet.spy/groupIdartifactIdspymemcached/artifactIdversion2.12.2/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project3.2 配置文件
memcache.ip127.0.0.1
memcache.port11211分别配置 memcache 的 Ip 地址和 端口。
3.3 设置配置对象
创建 MemcacheSource 接收配置信息
package com.example.config;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Component
ConfigurationProperties(prefix memcache)
public class MemcacheSource {private String ip;private int port;public String getIp() {return ip;}public void setIp(String ip) {this.ip ip;}public int getPort() {return port;}public void setPort(int port) {this.port port;}
}ConfigurationProperties(prefix memcache) 的意思会以 memcache.* 为开通将对应的配置文件加载
到属性中。
3.4 启动初始化 MemcachedClient
利用 CommandLineRunner 在项目启动的时候配置好 MemcachedClient 。
package com.example.config;import net.spy.memcached.MemcachedClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.io.IOException;
import java.net.InetSocketAddress;Component
public class MemcachedRunner implements CommandLineRunner {protected Logger logger LoggerFactory.getLogger(this.getClass());Resourceprivate MemcacheSource memcacheSource;private MemcachedClient client null;Overridepublic void run(String... args) throws Exception {try {client new MemcachedClient(new InetSocketAddress(memcacheSource.getIp(), memcacheSource.getPort()));} catch (IOException e) {logger.error(inint MemcachedClient failed , e);}}public MemcachedClient getClient() {return client;}}3.5 启动类
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}3.6 测试
package com.example;import com.example.config.MemcachedRunner;
import net.spy.memcached.MemcachedClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;RunWith(SpringRunner.class)
SpringBootTest
public class RepositoryTests {Resourceprivate MemcachedRunner memcachedRunner;Testpublic void testSetGet() {MemcachedClient memcachedClient memcachedRunner.getClient();memcachedClient.set(testkey, 1000, 666666);System.out.println(*********** memcachedClient.get(testkey).toString());}}使用中先测试插入一个 key 为 testkey 1000 为过期时间单位为 毫秒最后的 666666 为 key 对应的值。
执行测试用例 testSetGet 控制台输出内容
*********** 666666表明测试成功。