Memcached
Memcache is an open source, high-performance distributed memory object cache system. Memcache stores all data in memory and maintains a unified huge Hash table in memory, which can store any type of data, including images, videos, files, and database search results. Transfer data to memory, and then read from memory, thereby greatly improving the reading speed
Install Memcached
(1) Download and install libevent
git clone https://github.com/linxuping/libevent.git
./configure -prefix=/usr/local/libevent
make
make install
(2) Download and install memcached
tar -xvzf memcached-1.5.16.tar.gz
./configure -prefix=/usr/local/memcached -with-libevent=/usr/local/libevent
make
make install
(3) Start 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) View the memcached process
ps -ef | grep memcached
(5) Operate memcached
telnet 192.168.138.130 11211
set key 0 0 4
abcd
get key
Java operation 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 {}
Memcached's routing algorithm
(1) Find the remainder Hash algorithm
Use the key to do a hash operation to get an integer, and route according to the remainder
Advantages: Data distribution is balanced among multiple servers, suitable for most data needs
Disadvantages: If you need to expand capacity or there is a downtime, it will cause data loss