Memcached
Memcache es un sistema de caché de objetos de memoria distribuida de alto rendimiento y código abierto. Memcache almacena todos los datos en la memoria y mantiene una enorme tabla Hash unificada en la memoria, que puede almacenar cualquier tipo de datos, incluidas imágenes, videos, archivos y resultados de búsqueda en bases de datos. Transfiera datos a la memoria y luego lea desde la memoria, mejorando así en gran medida la velocidad de lectura
Instalar Memcached
(1) Descargar e instalar libevent
git clone https://github.com/linxuping/libevent.git
./configure -prefix=/usr/local/libevent
make
make install
(2) Descargue e instale 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 el proceso de memcached
ps -ef | grep memcached
(5) Operar memcached
telnet 192.168.138.130 11211
set key 0 0 4
abcd
get key
Operación 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 {}
Algoritmo de enrutamiento de Memcached
(1) Encuentra el algoritmo Hash restante
Use la tecla para hacer una operación hash para obtener un número entero y enrutar de acuerdo con el resto
Ventajas: la distribución de datos está equilibrada entre múltiples servidores, adecuada para la mayoría de las necesidades de datos
Desventajas: si necesita ampliar la capacidad o hay un tiempo de inactividad, provocará la pérdida de datos