Memcached介紹與使用

Memcached介紹與使用

Memcached是什麼?

Memcached是一個免費開源的、高性能的、分佈式內存對象緩存系統。

Memcached是一個基於內存的鍵值對儲存系統,用於儲存小型的任意數據(字符串、對象),比如數據庫查詢、API調用以及頁面渲染的結果。

它由什麼組成的?

  • 客戶端軟件,給出可用的Memcached服務器列表
  • 一個基於客戶端的哈希算法,用於基於“鍵”選擇合適的服務器
  • 服務端軟件,在一個內部的哈希表中儲存鍵值對數據
  • 最近最少使用算法(LRU),決定何時丟棄舊數據(當內存不足)

各類儲存方案對比

在這裏插入圖片描述

Memcached工作流程

在這裏插入圖片描述

  1. 客戶端向Memcached服務器發送請求,如果緩存中有需要的數據,直接返回。
  2. 如果沒有需要的數據,那麼就查詢數據庫,在給客戶端返回數據的同時,把該數據存到Memcached中。
  3. 一旦數據發生變化(數據的修改和刪除),就要實時更新緩存內的數據。

Memcached與Redis比較

memcache、redis原理對比

Memcached命令

Memcached set 命令

Spring Boot 集成 Memcached

安裝Memcached

Linux:

  • 安裝libevent庫

sudo apt-get install libevent ibevent-dev (Ubuntu/Debian)

yum install libevent libevent-devel (Redhat/Fedora/Centos)

  • 安裝Memcached

sudo apt-get install memcached (Ubuntu/Debian)

yum install memcached (Redhat/Fedora/Centos)

portmaster databases/memcached (FreeBSD)

Windows:

Memcached在1.4.5版本之前可以作爲一個服務安裝,但在之後就取消了這個功能。所以安裝方法分兩塊。

1.4.5之前版本安裝

使用管理員運行以下命令:

f:\memcached.exe -d install (路徑視你的存放路徑而定)

打開和關閉Memcached服務:

f:\memcached.exe -d start (路徑視你的存放路徑而定)

f:\memcached.exe -d stop (路徑視你的存放路徑而定)

如要修改配置,打開regedit.exe,找到"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached"進行修改。

1.4.5之後版本安裝

通過創建計劃任務來進行設置Memcached的windows啓動時自執行:

管理員身份執行:

schtasks /create /sc onstart /tn memcached /tr “c:\memcached.exe”

刪除計劃任務:

schtasks /delete /tn memcached

注意:一定要先把Memcached服務成功打開,下面的程序才能跑通。

導包

Memcached沒有爲Spring Boot開發集成包,所以需要自行引入。

這裏使用Spymemcached。

github : https://github.com/couchbase/spymemcached

Maven:

https://mvnrepository.com/artifact/net.spy/spymemcached

<dependency>
    <groupId>net.spy</groupId>
    <artifactId>spymemcached</artifactId>
    <version>2.12.3</version>
</dependency>

程序中使用

創建一個Runner繼承CommandLineRunner,並在其中初始化Memcached服務,這樣就可以在Spring容器成功啓動之後,第一時間創建可用的Memcached服務。

package com.lbl.spymemcacheddemo;

import net.spy.memcached.MemcachedClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

@Component
public class MyRunner implements CommandLineRunner {

    private MemcachedClient memcachedClient = null;

    @Override
    public void run(String... args) throws Exception {
        try {
            memcachedClient = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public MemcachedClient getMemcachedClient() {
        return memcachedClient;
    }
}

創建測試:

package com.lbl.spymemcacheddemo;

import net.spy.memcached.MemcachedClient;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

import static org.junit.jupiter.api.Assertions.*;
@RunWith(SpringRunner.class)
@SpringBootTest
class MyRunnerTest {

    @Resource
    private MyRunner myRunner;

    @Test
    public void Test() {
        MemcachedClient client = myRunner.getMemcachedClient();
        client.set("test",100,"123456");
        System.out.println("test is "+client.get("testkey").toString());
    }

}

其它遇到的坑

@RunWith註解爆紅

檢查pom.xml文件,發現這一個:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
</dependency>

改爲這樣:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
</dependency>

其它方法:

Springboot測試類之@RunWith註解

CommandLineRunner或者ApplicationRunner接口介紹

CommandLineRunner或者ApplicationRunner接口

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章