SpringBoot集成Memcached的項目實踐
1、Memcached 介紹
Memcached 是一個高性能的分布式內(nèi)存對象緩存系統(tǒng),用于動態(tài)Web應(yīng)用以減輕數(shù)據(jù)庫負(fù)載。它通過在內(nèi)存中
緩存數(shù)據(jù)和對象來減少讀取數(shù)據(jù)庫的次數(shù),從而提高動態(tài)、數(shù)據(jù)庫驅(qū)動網(wǎng)站的速度。Memcached基于一個存儲
鍵/值對的hashmap。其守護(hù)進(jìn)程(daemon )是用C寫的,但是客戶端可以用任何語言來編寫,并通過
memcached協(xié)議與守護(hù)進(jìn)程通信。
因為 Spring Boot 沒有針對 Memcached 提供對應(yīng)的組建包,因此需要我們自己來集成。官方推出的 Java 客戶端
Spymemcached 是一個比較好的選擇之一。
Spymemcached 最早由 Dustin Sallings 開發(fā),Dustin 后來和別人一起創(chuàng)辦了 Couchbase (原NorthScale),職位
為首席架構(gòu)師。2014 加入 Google。Spymemcached 是一個采用 Java 開發(fā)的異步、單線程的 Memcached 客戶
端, 使用 NIO 實現(xiàn)。Spymemcached 是 Memcached 的一個流行的 Java client 庫,性能表現(xiàn)出色,廣泛應(yīng)用于
Java + Memcached 項目中。
2、Windows下安裝Memcached
官網(wǎng)上并未提供 Memcached 的 Windows 平臺安裝包,我們可以使用以下鏈接來下載,你需要根據(jù)自己的系統(tǒng)平
臺及需要的版本號點擊對應(yīng)的鏈接下載即可:
32位系統(tǒng) 1.2.5版本:
http://static.runoob.com/download/memcached-1.2.5-win32-bin.zip32位系統(tǒng) 1.2.6版本:
http://static.runoob.com/download/memcached-1.2.6-win32-bin.zip32位系統(tǒng) 1.4.4版本:
http://static.runoob.com/download/memcached-win32-1.4.4-14.zip64位系統(tǒng) 1.4.4版本:
http://static.runoob.com/download/memcached-win64-1.4.4-14.zip32位系統(tǒng) 1.4.5版本:
http://static.runoob.com/download/memcached-1.4.5-x86.zip64位系統(tǒng) 1.4.5版本:
http://static.runoob.com/download/memcached-1.4.5-amd64.zip
在 1.4.5 版本以前 memcached 可以作為一個服務(wù)安裝,而在 1.4.5 及之后的版本刪除了該功能。因此我們以下介
紹兩個不同版本 1.4.4 及 1.4.5的不同安裝方法。
2.1 memcached <1.4.5 版本安裝
1、解壓下載的安裝包到指定目錄。
2、在 1.4.5 版本以前 memcached 可以作為一個服務(wù)安裝,使用管理員權(quán)限運行以下命令:
c:\memcached\memcached.exe -d install
3、然后我們可以使用以下命令來啟動和關(guān)閉 memcached 服務(wù):
c:\memcached\memcached.exe -d start c:\memcached\memcached.exe -d stop
4、如果要修改 memcached 的配置項, 可以在命令行中執(zhí)行 regedit.exe 命令打開注冊表并找到
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached 來進(jìn)行修改。
如果要提供 memcached 使用的緩存配置可以修改 ImagePath 為:
"c:\memcached\memcached.exe" -d runservice -m 512
-m 512 意思是設(shè)置 memcached 最大的緩存配置為512M。
此外我們還可以通過使用 memcached.exe -h 命令查看更多的參數(shù)配置。
5、如果我們需要卸載 memcached ,可以使用以下命令:
c:\memcached\memcached.exe -d uninstall
2.2 memcached >= 1.4.5 版本安裝
1、解壓下載的安裝包到指定目錄。
2、在 memcached1.4.5 版本之后,memcached 不能作為服務(wù)來運行,需要使用任務(wù)計劃中來開啟一個普通的
進(jìn)程,在 window 啟動時設(shè)置 memcached自動執(zhí)行。
我們使用管理員身份執(zhí)行以下命令將 memcached 添加來任務(wù)計劃表中:
schtasks /create /sc onstart /tn memcached /tr "'c:\memcached\memcached.exe' -m 512"
注意:-m 512 意思是設(shè)置 memcached 最大的緩存配置為512M。
注意:我們可以通過使用 c:\memcached\memcached.exe -h 命令查看更多的參數(shù)配置。
3、如果需要刪除 memcached 的任務(wù)計劃可以執(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 設(shè)置配置對象
創(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.* 為開通將對應(yīng)的配置文件加載
到屬性中。
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 對應(yīng)的值。
執(zhí)行測試用例 testSetGet ,控制臺輸出內(nèi)容:
*********** 666666
到此這篇關(guān)于SpringBoot集成Memcached的項目實踐的文章就介紹到這了,更多相關(guān)SpringBoot集成Memcached內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot JPA 枚舉Enum類型存入到數(shù)據(jù)庫的操作
這篇文章主要介紹了Springboot JPA 枚舉Enum類型存入到數(shù)據(jù)庫的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
解決多模塊項目中Mybatis的Mapper內(nèi)部方法找不到的問題
這篇文章主要介紹了解決多模塊項目中Mybatis的Mapper內(nèi)部方法找不到的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
java中線程的sleep()方法和yield()方法的區(qū)別
本文主要介紹了java中線程的sleep()方法和yield()方法的區(qū)別,Thread類的sleep()方法使線程休眠指定時間,不釋放鎖,而yield()提示調(diào)度器當(dāng)前線程愿意讓出CPU資源,不保證立即切換線程,感興趣的可以了解一下2024-10-10
Java調(diào)用C++動態(tài)庫超詳細(xì)步驟講解(附源碼)
C語言因其高效和接近硬件的特性,時常會被用在性能要求較高或者需要直接操作硬件的場合,這篇文章主要介紹了Java調(diào)用C++動態(tài)庫的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04
解決Springboot整合shiro時靜態(tài)資源被攔截的問題
這篇文章主要介紹了解決Springboot整合shiro時靜態(tài)資源被攔截的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

