MemCached缓存技术(四)操作MemCached

Java客户端方式-Java API

注:lib目录下面要添加jar包-客户端路由算法:

 

 

 然后手动引入jar包:

 

 

 

 一、插入数据(字符串)

 

****自己操作****

Java代码:

    /**
     * 一、插入数据
     * @throws Exception
     */
    public static void a() throws Exception {
        //一、先建立Memcached的实例,指定服务器的地址和端口号
        MemcachedClient memcachedClient = new MemcachedClient(new InetSocketAddress("192.168.212.111",1121));
        //二、写数据
        //exp:过期时间
        Future<Boolean> future = memcachedClient.set("key2", 10, "Hello World");
        if(future.get().booleanValue()){
            //表示插入成功,关掉该实例
            memcachedClient.shutdown();
        }
    }

接着用Telnet方式读取数据:

get key2

VALUE key2 0 11

Hello World

END

二、查询数据

 

****自己操作****

Java代码

    /**
     * 二、读取数据
     * @throws Exception
     */
    public static void b() throws Exception {
        //一、先建立MemCached的实例,指定服务器的地址和端口号
        MemcachedClient memcachedClient = new MemcachedClient(new InetSocketAddress("192.168.212.111", 1121));
        //二、读数据
        Object object = memcachedClient.get("key1");
        //打印
        System.out.println("取到的值是:"+object);
        memcachedClient.shutdown();
    }

结果:

 

三、插入数据(对象)--自定义内部类

Java代码:

/**
 * 三、插入数据(对象)--自定义内部类
 * @throws Exception
 */
public static void c() throws Exception {
    //一、先建立MemCached的实例,指定服务器的地址和端口号
    MemcachedClient memcachedClient = new MemcachedClient(new InetSocketAddress("192.168.212.111", 1121));
    //二、读数据
    Future<Boolean> future = memcachedClient.set("key3", 10, new Student());
    if(future.get().booleanValue()){
        //表示插入成功,关掉该实例
        memcachedClient.shutdown();
    }
}

在这个Java_class外面自定义一个Student内部类

/**
 * 自定义内部类
 */
class Student {}

执行后发现报错:

Exception in thread "main" java.lang.IllegalArgumentException: Non-serializable object

Caused by: java.io.NotSerializableException: memCached_API.Student

因为:Student没有实现序列化,他就不能在网络中传输。

所以对Student类进行修改:

/**
 * 自定义内部类
 */
class Student implements Serializable {}

再次执行就能成功写入了

四、基于客户端的分布式插入数据 

****自己操作****

Java代码:

/**
 * 四、基于客户端的分布式插入数据
 * @throws Exception
 */
public static void d() throws Exception {
    //一、创建含有服务器IP和端口号的列表
    List<InetSocketAddress> list = new ArrayList<InetSocketAddress>();
    list.add(new InetSocketAddress("192.168.212.111", 1121));
    list.add(new InetSocketAddress("192.168.212.111", 1122));
    list.add(new InetSocketAddress("192.168.212.111", 1123));

    MemcachedClient client = new MemcachedClient(list);
    //二、循环写20次
    for(int i=0;i<20;i++){
        System.out.println("插入数据:"+i);
        client.set("key112"+i,0,"value"+i); //exp=0 :永远不过期
        Thread.sleep(1000); //睡1秒钟
    }
    client.shutdown();//关闭MemCached
}

结果:

 

 

 

现在到192.168.212.111的1121端口上取数据:

 

这就能看出:MemCached的路由算法可将数据均衡地分配到各个MemCached实例上(端口号不同)

五、Java代码整体实现

package memCached_API;

import net.spy.memcached.MemcachedClient;

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

/**
 * @Author : ASUS and xinrong
 * @Version : 2020/8/22 & 1.0
 */
public class MemCached_API_3ge {
    public static void main(String[] args) throws Exception {
        //a();
        //b();
        //c();
        d();
    }

    /**
     * 一、插入数据
     * @throws Exception
     */
    public static void a() throws Exception {
        //一、先建立Memcached的实例,指定服务器的地址和端口号
        MemcachedClient memcachedClient = new MemcachedClient(new InetSocketAddress("192.168.212.111",1121));
        //二、写数据
        //exp:过期时间
        Future<Boolean> future = memcachedClient.set("key2", 10, "Hello World");
        if(future.get().booleanValue()){
            //表示插入成功,关掉该实例
            memcachedClient.shutdown();
        }
    }

    /**
     * 二、读取数据
     * @throws Exception
     */
    public static void b() throws Exception {
        //一、先建立MemCached的实例,指定服务器的地址和端口号
        MemcachedClient memcachedClient = new MemcachedClient(new InetSocketAddress("192.168.212.111", 1121));
        //二、读数据
        Object object = memcachedClient.get("key1");
        //打印
        System.out.println("取到的值是:"+object);
        memcachedClient.shutdown();
    }

    /**
     * 三、插入数据(对象)--自定义内部类
     * @throws Exception
     */
    public static void c() throws Exception {
        //一、先建立MemCached的实例,指定服务器的地址和端口号
        MemcachedClient memcachedClient = new MemcachedClient(new InetSocketAddress("192.168.212.111", 1121));
        //二、读数据
        Future<Boolean> future = memcachedClient.set("key3", 10, new Student());
        if(future.get().booleanValue()){
            //表示插入成功,关掉该实例
            memcachedClient.shutdown();
        }
    }
    /**
     * 四、基于客户端的分布式插入数据
     * @throws Exception
     */
    public static void d() throws Exception {
        //一、创建含有服务器IP和端口号的列表
        List<InetSocketAddress> list = new ArrayList<InetSocketAddress>();
        list.add(new InetSocketAddress("192.168.212.111", 1121));
        list.add(new InetSocketAddress("192.168.212.111", 1122));
        list.add(new InetSocketAddress("192.168.212.111", 1123));

        MemcachedClient client = new MemcachedClient(list);
        //循环写20次
        for(int i=0;i<20;i++){
            System.out.println("插入数据:"+i);
            client.set("key112"+i,0,"value"+i); //exp=0 :永远不过期
            Thread.sleep(1000); //睡1秒钟
        }
        client.shutdown();//关闭MemCached
    }
}
/**
 * 自定义内部类
 */
class Student implements Serializable {}

 

Copyright © 2025 MissRong
Powered by .NET 9.0 on Kubernetes