ビッグデータ-Memcached

Memcached

Memcacheは、オープンソースの高性能分散メモリオブジェクトキャッシュシステムです。Memcacheはすべてのデータをメモリに保存し、メモリに統合された巨大なハッシュテーブルを維持します。これには、画像、ビデオ、ファイル、データベース検索結果など、あらゆるタイプのデータを保存できます。データをメモリに転送してからメモリから読み取ることで、読み取り速度を大幅に向上

Memcachedをインストールする

(1)libeventをダウンロードしてインストールする

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

(2)memcachedをダウンロードしてインストールする

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

(3)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)memcachedプロセスを表示する

ps -ef | grep memcached

ここに画像の説明を挿入

(5)memcachedを操作する

telnet 192.168.138.130 11211
set key 0 0 4
abcd
get key

ここに画像の説明を挿入

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 {}

ここに画像の説明を挿入

Memcachedのルーティングアルゴリズム

(1)残りのハッシュアルゴリズムを見つける

キーを使用してハッシュ演算を実行し、整数を取得して、残りに従ってルーティングします

利点:データ分散は複数のサーバー間でバランスが取れており、ほとんどのデータニーズに適しています

短所:容量を拡張する必要がある場合、またはダウンタイムがある場合、データが失われます

(2)一貫性のあるハッシュアルゴリズム

131件の元の記事を公開 12 件を獲得 60,000回の閲覧+

おすすめ

転載: blog.csdn.net/JavaDestiny/article/details/98516567