Memcached de Big Data

Memcached

Memcache é um sistema de cache de objeto de memória distribuída de código aberto e alto desempenho. O Memcache armazena todos os dados na memória e mantém uma enorme tabela Hash unificada na memória, que pode armazenar qualquer tipo de dados, incluindo imagens, vídeos, arquivos e resultados de pesquisa no banco de dados. Transfira dados para a memória e depois leia a partir da memória, melhorando consideravelmente a velocidade de leitura

Instalar Memcached

(1) Baixe e instale o libevent

git clone https://github.com/linxuping/libevent.git
./configure -prefix=/usr/local/libevent
make
make install

(2) Baixe e instale o memcached

tar -xvzf memcached-1.5.16.tar.gz
./configure -prefix=/usr/local/memcached -with-libevent=/usr/local/libevent
make
make install

(3) Iniciar memcached

./memcached -u root -d -m 128 -p 11211
./memcached -u root -d -m 128 -p 11212
./memcached -u root -d -m 128 -p 11213

(4) Ver o processo do memcached

ps -ef | grep memcached

Insira a descrição da imagem aqui

(5) Operar memcached

telnet 192.168.138.130 11211
set key 0 0 4
abcd
get key

Insira a descrição da imagem aqui

Operação Java Memcached

package com.memcache.util;

import net.spy.memcached.MemcachedClient;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author Administrator
 * @version 1.0
 */
public class Memcache {
    private MemcachedClient client;

    /**
     * 连接memcached
     * @throws IOException
     */
    @Before
    public void connectClient() throws IOException {
        client = new MemcachedClient(new InetSocketAddress("192.168.138.130",11211));
    }


    /**
     * 设置key
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @Test
    public void setKey() throws ExecutionException, InterruptedException {
        Future<Boolean> future = client.set("key1", 0, "Hello World");
        if (future.get().booleanValue()){
            client.shutdown();
        }
    }

    @Test
    public void getKey(){
        Object o = client.get("key1");
        System.out.println("取到的值: "+o);
        client.shutdown();
    }

    /**
     * 设置对象的值
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @Test
    public void setObject() throws ExecutionException, InterruptedException {
        Future<Boolean> future = client.set("key2", 0, new Student());
        if (future.get().booleanValue()){
            client.shutdown();
        }
    }

    /**
     * 客户端路由算法
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void connectList() throws IOException, InterruptedException {
        ArrayList<InetSocketAddress> list = new ArrayList<>();
        list.add(new InetSocketAddress("192.168.138.130",11211));
        list.add(new InetSocketAddress("192.168.138.130",11212));
        list.add(new InetSocketAddress("192.168.138.130",11213));

        //连接Memcached
        client = new MemcachedClient(list);

        for (int i = 0; i < 10; i++){
            client.set("key"+i,0,"value"+i);
            Thread.sleep(1000);
        }

        client.shutdown();
    }
}
class Student implements Serializable {}

Insira a descrição da imagem aqui

Algoritmo de roteamento do Memcached

(1) Encontre o restante algoritmo Hash

Use a tecla para executar uma operação de hash para obter um número inteiro e rotear de acordo com o restante

Vantagens: a distribuição de dados é equilibrada entre vários servidores, adequada para a maioria das necessidades de dados

Desvantagens: se você precisar expandir a capacidade ou houver um tempo de inatividade, isso causará perda de dados

(2) Algoritmo de hash consistente

Publicado 131 artigos originais · ganhou 12 · 60.000 visualizações +

Acho que você gosta

Origin blog.csdn.net/JavaDestiny/article/details/98516567
Recomendado
Clasificación