java连接使用Memcached服务

  • Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。
    1. 下载memcahed链接驱动
      没有的可以在这里下载:https://pan.baidu.com/s/1hsqzHoK
      提取码:6eju
    2. 新建一个java project,将jar文件添加到项目中
    3. 简单的测试
package com.el;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import net.spy.memcached.CASResponse;
import net.spy.memcached.CASValue;
import net.spy.memcached.MemcachedClient;

public class myTestDemo {
    private static MemcachedClient mcc;

    public static void main(String[] args) {
        Init();//初始化连接
        MecachedSet();
        MecachedAdd();
        MemcachedReplace();
        MemcachedAppend();
        MemcachedPrepend();
        MemcachedCAS();
        MemcachedGet();
        MemcachedDelete();
        MemcachedIncrOrDecr();
        ShutDown();
    }
//通过memcached提供的MemcachedClient类我们可以创建一个连接并能很轻松的使用这个高效的内存数据库
    public static void Init() {
        try {
            mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("connect server success!");
    }

    public static void ShutDown() {
        mcc.shutdown();
    }

    public static void MecachedSet() {
        // 存储数据
        Future fo = mcc.set("emample0", 900, "emample0 value");

        // 查看存储状态
        try {
            System.out.println("set status:" + fo.get());
        } catch (InterruptedException | ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 输出值
        System.out.println("test value in cache - " + mcc.get("emample0"));
    }

    public static void MecachedAdd() {
        // 添加数据
        Future f1 = mcc.set("emample1", 900, "emample1 value");

        // 打印状态
        try {
            System.out.println("set status:" + f1.get());
            // 输出
            System.out.println("emample1 value in cache - " + mcc.get("emample1"));

            // 添加
            Future f2 = mcc.add("emample12", 900, "memcached");

            // 打印状态
            System.out.println("add status:" + f2.get());
            // 添加新key
            f2 = mcc.add("codingground", 900, "All Free Compilers");

            // 打印状态
            System.out.println("add status:" + f2.get());
        } catch (InterruptedException | ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 输出
        System.out.println("emample12 value in cache - " + mcc.get("emample12"));
        System.out.println("codingground value in cache - " + mcc.get("codingground"));
    }

    public static void MemcachedReplace() {
        // 添加第一个 key=》value 对
        Future fo = mcc.set("example2", 900, "example2 value");

        // 输出执行 add 方法后的状态
        try {
            System.out.println("add status:" + fo.get());
            // 获取键对应的值
            System.out.println("runoob value in cache - " + mcc.get("example2"));

            // 添加新的 key
            fo = mcc.replace("example2", 900, "example2 Library");

            // 输出执行 set 方法后的状态
            System.out.println("replace status:" + fo.get());
        } catch (InterruptedException | ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 获取键对应的值
        System.out.println("example value in cache - " + mcc.get("example2"));
    }

    public static void MemcachedAppend() {
        // 添加数据
        Future f1 = mcc.set("example3", 900, "example3 value");
        try {
            // 输出执行 set 方法后的状态
            System.out.println("set status:" + f1.get());

            // 获取键对应的值
            System.out.println("runoob value in cache - " + mcc.get("example3"));

            // 对存在的key进行数据添加操作
            Future f2 = mcc.append("example3", "for all");

            // 输出执行 set 方法后的状态
            System.out.println("append status:" + f2.get());
        } catch (InterruptedException | ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 获取键对应的值
        System.out.println("runoob value in cache - " + mcc.get("example3"));
    }

    public static void MemcachedPrepend() {
        // 添加数据
        Future f1 = mcc.set("expample4", 900, "expample4 value");
        try {
            // 输出执行 set 方法后的状态
            System.out.println("set status:" + f1.get());

            // 获取键对应的值
            System.out.println("runoob value in cache - " + mcc.get("expample4"));

            // 对存在的key进行数据添加操作
            Future f2 = mcc.prepend("expample4", "Free ");

            // 输出执行 set 方法后的状态
            System.out.println("prepend status:" + f2.get());
        } catch (InterruptedException | ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 获取键对应的值
        System.out.println("runoob value in cache - " + mcc.get("expample4"));
    }

    public static void MemcachedCAS() {
        // 添加数据
        Future fo = mcc.set("expample5", 900, "expample4 value");
        try {
            // 输出执行 set 方法后的状态
            System.out.println("set status:" + fo.get());

            // 使用 get 方法获取数据
            System.out.println("runoob value in cache - " + mcc.get("expample5"));

            // 通过 gets 方法获取 CAS token(令牌)
            CASValue casValue = mcc.gets("expample5");

            // 输出 CAS token(令牌) 值
            System.out.println("CAS token - " + casValue);

            // 尝试使用cas方法来更新数据
            CASResponse casresp = mcc.cas("expample5", casValue.getCas(), 900, "expample5-Library");

            // 输出 CAS 响应信息
            System.out.println("CAS Response - " + casresp);
        } catch (InterruptedException | ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 输出值
        System.out.println("runoob value in cache - " + mcc.get("expample5"));
    }

    public static void MemcachedGet() {
        // 添加数据
        Future fo = mcc.set("expample6", 900, "expample6 value");
        try {
            // 输出执行 set 方法后的状态
            System.out.println("set status:" + fo.get());

            // 从缓存中获取键为 expample6 的值
            System.out.println("expample6 value in cache - " + mcc.get("expample6"));

            // 通过 gets 方法获取 CAS token(令牌)
            CASValue casValue = mcc.gets("expample6");
            // 输出 CAS token(令牌) 值
            System.out.println("CAS value in cache - " + casValue);
        } catch (InterruptedException | ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void MemcachedDelete() {
        // 添加数据
        Future f1 = mcc.set("expample7", 900, "expample7 value");
        try {
            // 输出执行 set 方法后的状态
            System.out.println("set status:" + f1.get());

            // 获取键对应的值
            System.out.println("expample7 value in cache - " + mcc.get("expample7"));

            // 对存在的key进行数据添加操作
            Future f2 = mcc.delete("expample7");

            // 输出执行 delete 方法后的状态
            System.out.println("delete status:" + f2.get());

            // 获取键对应的值
            System.out.println("expample7 value in cache - " + mcc.get("expample7"));
        } catch (InterruptedException | ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void MemcachedIncrOrDecr() {
        // 添加数字值
        Future fo = mcc.set("number", 900, "1000");
        try {
            // 输出执行 set 方法后的状态
            System.out.println("set status:" + fo.get());

            // 获取键对应的值
            System.out.println("value in cache - " + mcc.get("number"));

            // 自增并输出
            System.out.println("value in cache after increment - " + mcc.incr("number", 111));

            // 自减并输出
            System.out.println("value in cache after decrement - " + mcc.decr("number", 112));
        } catch (InterruptedException | ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ExtraMile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值