SpringBoot集成Memcached的項目實踐
1、Memcached 介紹
Memcached 是一個高性能的分布式內存對象緩存系統(tǒng),用于動態(tài)Web應用以減輕數據庫負載。它通過在內存中
緩存數據和對象來減少讀取數據庫的次數,從而提高動態(tài)、數據庫驅動網站的速度。Memcached基于一個存儲
鍵/值對的hashmap。其守護進程(daemon )是用C寫的,但是客戶端可以用任何語言來編寫,并通過
memcached協議與守護進程通信。
因為 Spring Boot 沒有針對 Memcached 提供對應的組建包,因此需要我們自己來集成。官方推出的 Java 客戶端
Spymemcached 是一個比較好的選擇之一。
Spymemcached 最早由 Dustin Sallings 開發(fā),Dustin 后來和別人一起創(chuàng)辦了 Couchbase (原NorthScale),職位
為首席架構師。2014 加入 Google。Spymemcached 是一個采用 Java 開發(fā)的異步、單線程的 Memcached 客戶
端, 使用 NIO 實現。Spymemcached 是 Memcached 的一個流行的 Java client 庫,性能表現出色,廣泛應用于
Java + Memcached 項目中。
2、Windows下安裝Memcached
官網上并未提供 Memcached 的 Windows 平臺安裝包,我們可以使用以下鏈接來下載,你需要根據自己的系統(tǒng)平
臺及需要的版本號點擊對應的鏈接下載即可:
32位系統(tǒng) 1.2.5版本:
http://static.runoob.com/download/memcached-1.2.5-win32-bin.zip
32位系統(tǒng) 1.2.6版本:
http://static.runoob.com/download/memcached-1.2.6-win32-bin.zip
32位系統(tǒng) 1.4.4版本:
http://static.runoob.com/download/memcached-win32-1.4.4-14.zip
64位系統(tǒng) 1.4.4版本:
http://static.runoob.com/download/memcached-win64-1.4.4-14.zip
32位系統(tǒng) 1.4.5版本:
http://static.runoob.com/download/memcached-1.4.5-x86.zip
64位系統(tǒng) 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 install
3、然后我們可以使用以下命令來啟動和關閉 memcached 服務:
c:\memcached\memcached.exe -d start c:\memcached\memcached.exe -d stop
4、如果要修改 memcached 的配置項, 可以在命令行中執(zhí)行 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 uninstall
2.2 memcached >= 1.4.5 版本安裝
1、解壓下載的安裝包到指定目錄。
2、在 memcached1.4.5 版本之后,memcached 不能作為服務來運行,需要使用任務計劃中來開啟一個普通的
進程,在 window 啟動時設置 memcached自動執(zhí)行。
我們使用管理員身份執(zhí)行以下命令將 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 的任務計劃可以執(zhí)行以下命令:
schtasks /delete /tn memcached
3、整合Memcached
3.1 pom依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>spring-boot-memcache-spymemcached</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-memcache-spymemcached</name> <description>spring-boot-memcache-spymemcached</description> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>net.spy</groupId> <artifactId>spymemcached</artifactId> <version>2.12.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.2 配置文件
memcache.ip=127.0.0.1 memcache.port=11211
分別配置 memcache 的 Ip 地址和 端口。
3.3 設置配置對象
創(chuàng)建 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()); @Resource private MemcacheSource memcacheSource; private MemcachedClient client = null; @Override public 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 { @Resource private MemcachedRunner memcachedRunner; @Test public void testSetGet() { MemcachedClient memcachedClient = memcachedRunner.getClient(); memcachedClient.set("testkey", 1000, "666666"); System.out.println("*********** " + memcachedClient.get("testkey").toString()); } }
使用中先測試插入一個 key 為 testkey ,1000 為過期時間單位為 毫秒,最后的 666666 為 key 對應的值。
執(zhí)行測試用例 testSetGet ,控制臺輸出內容:
*********** 666666
到此這篇關于SpringBoot集成Memcached的項目實踐的文章就介紹到這了,更多相關SpringBoot集成Memcached內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決cmd執(zhí)行javac報錯:不是內部或外部命令,也不是可運行的程序
剛接觸JAVA的新手可能就不知道怎么解決'JAVAC'不是內部命令或外部命令,這篇文章主要給大家介紹了關于解決cmd執(zhí)行javac報錯:不是內部或外部命令,也不是可運行的程序的相關資料,需要的朋友可以參考下2023-11-11SpringBoot+Spring Security+JWT實現RESTful Api權限控制的方法
這篇文章主要介紹了SpringBoot+Spring Security+JWT實現RESTful Api權限控制的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03淺析Java迭代器Iterator和Iterable的區(qū)別
Java語言中,Iterator和Iterable都是用來遍歷集合類數據結構的接口,雖然它們有很多相似的地方,但在具體實現中卻有著一些不同之處,本文將詳細分析它們的區(qū)別,并提供相應的代碼示例,需要的朋友可以參考下2023-07-07