java连接使用Memcached服务
来源:互联网 发布:linux ed2k wget 编辑:程序博客网 时间:2024/06/05 09:03
- Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。
- 下载memcahed链接驱动
没有的可以在这里下载:https://pan.baidu.com/s/1hsqzHoK
提取码:6eju - 新建一个java project,将jar文件添加到项目中
- 简单的测试
- 下载memcahed链接驱动
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
- java连接使用Memcached服务
- Java 连接 Memcached 服务
- Java 连接 Memcached 服务
- Java连接并使用Memcached
- Java连接MemCached服务器
- Java连接MemCached服务器
- Java连接MemCached服务器
- java连接memcached
- Memcached Client java连接Memcached集群环境
- java 代码调用memcached服务
- Java连接Redis服务,Jedis的使用
- memcached连接及使用实例
- Java使用Memcached
- Java使用Memcached
- Java使用memcached
- Java使用memcached
- Java使用memcached
- Java使用memcached
- jQuery-jQuery选择器汇总
- 15个高级Java多线程面试题及回答
- phpcms v9的wap单页添加方法
- 集合
- web.xml文件初始化过程
- java连接使用Memcached服务
- 如何移植gdb到freescale i.MX5?
- Spring声明式事务管理详解
- std 临界区
- lnmp环境搭建 --非集成
- NOIP复赛复习(十六)预处理与前缀和
- LINUX Centos 安装JDK遇到的问题总结
- 解决JS库中$符号的冲突问题
- 线程