课程表

Memcached 基础

Memcached 存储命令

Memcached 查找命令

Memcached 统计命令

Memcached 实例

工具箱
速查手册

Java 连接 Memcached

当前位置:免费教程 » 数据库/运维 » Memcached

使用 Java 程序连接 Memcached,需要在你的 classpath 中添加 Memcached jar 包。

本站 jar 包下载地址:spymemcached-2.10.3.jar

Google Code jar 包下载地址:spymemcached-2.10.3.jar(需要翻墙)。

以下程序假定 Memcached 服务的主机为 127.0.0.1,端口为 11211。

连接实例

Java 连接 Memcached

  1. import net.spy.memcached.MemcachedClient;
  2. import java.net.*;
  3.  
  4.  
  5. public class MemcachedJava {
  6. public static void main(String[] args) {
  7. try{
  8. // 本地连接 Memcached 服务
  9. MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  10. System.out.println("Connection to server sucessful.");
  11. // 关闭连接
  12. mcc.shutdown();
  13. }catch(Exception ex){
  14. System.out.println( ex.getMessage() );
  15. }
  16. }
  17. }

该程序中我们使用 InetSocketAddress 连接 IP 为 127.0.0.1 端口 为 11211 的 memcached 服务。

执行以上代码,如果连接成功会输出以下信息:

  1. Connection to server successful.

set 操作实例

以下使用 java.util.concurrent.Future 来存储数据

  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Future;
  3.  
  4. import net.spy.memcached.MemcachedClient;
  5.  
  6. public class MemcachedJava {
  7. public static void main(String[] args) {
  8. try{
  9. // 连接本地的 Memcached 服务
  10. MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  11. System.out.println("Connection to server sucessful.");
  12. // 存储数据
  13. Future fo = mcc.set("W3xue", 900, "Free Education");
  14. // 查看存储状态
  15. System.out.println("set status:" + fo.get());
  16. // 输出值
  17. System.out.println("W3xue value in cache - " + mcc.get("W3xue"));
  18.  
  19. // 关闭连接
  20. mcc.shutdown();
  21. }catch(Exception ex){
  22. System.out.println( ex.getMessage() );
  23. }
  24. }
  25. }

执行程序,输出结果为:

  1. Connection to server successful.
  2. set status:true
  3. W3xue value in cache - Free Education

add 操作实例

  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Future;
  3.  
  4. import net.spy.memcached.MemcachedClient;
  5.  
  6. public class MemcachedJava {
  7. public static void main(String[] args) {
  8. try{
  9. // 连接本地的 Memcached 服务
  10. MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  11. System.out.println("Connection to server sucessful.");
  12.  
  13. // 添加数据
  14. Future fo = mcc.set("W3xue", 900, "Free Education");
  15.  
  16. // 打印状态
  17. System.out.println("set status:" + fo.get());
  18.  
  19. // 输出
  20. System.out.println("W3xue value in cache - " + mcc.get("W3xue"));
  21.  
  22. // 添加
  23. Future fo = mcc.add("W3xue", 900, "memcached");
  24.  
  25. // 打印状态
  26. System.out.println("add status:" + fo.get());
  27.  
  28. // 添加新key
  29. fo = mcc.add("codingground", 900, "All Free Compilers");
  30.  
  31. // 打印状态
  32. System.out.println("add status:" + fo.get());
  33. // 输出
  34. System.out.println("codingground value in cache - " + mcc.get("codingground"));
  35.  
  36. // 关闭连接
  37. mcc.shutdown();
  38. }catch(Exception ex){
  39. System.out.println(ex.getMessage());
  40. }
  41. }
  42. }

replace 操作实例

  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Future;
  3.  
  4. import net.spy.memcached.MemcachedClient;
  5.  
  6. public class MemcachedJava {
  7. public static void main(String[] args) {
  8. try {
  9. //连接本地的 Memcached 服务
  10. MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  11. System.out.println("Connection to server sucessful.");
  12.  
  13. // 添加第一个 key=》value 对
  14. Future fo = mcc.set("W3xue", 900, "Free Education");
  15.  
  16. // 输出执行 add 方法后的状态
  17. System.out.println("add status:" + fo.get());
  18.  
  19. // 获取键对应的值
  20. System.out.println("W3xue value in cache - " + mcc.get("W3xue"));
  21.  
  22. // 添加新的 key
  23. fo = mcc.replace("W3xue", 900, "Largest Tutorials' Library");
  24.  
  25. // 输出执行 set 方法后的状态
  26. System.out.println("replace status:" + fo.get());
  27.  
  28. // 获取键对应的值
  29. System.out.println("W3xue value in cache - " + mcc.get("W3xue"));
  30.  
  31. // 关闭连接
  32. mcc.shutdown();
  33. }catch(Exception ex){
  34. System.out.println( ex.getMessage() );
  35. }
  36. }
  37. }

append 操作实例

  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Future;
  3.  
  4. import net.spy.memcached.MemcachedClient;
  5.  
  6. public class MemcachedJava {
  7. public static void main(String[] args) {
  8. try{
  9. // 连接本地的 Memcached 服务
  10. MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  11. System.out.println("Connection to server sucessful.");
  12.  
  13. // 添加数据
  14. Future fo = mcc.set("W3xue", 900, "Free Education");
  15.  
  16. // 输出执行 set 方法后的状态
  17. System.out.println("set status:" + fo.get());
  18.  
  19. // 获取键对应的值
  20. System.out.println("W3xue value in cache - " + mcc.get("W3xue"));
  21.  
  22. // 对存在的key进行数据添加操作
  23. Future fo = mcc.append("W3xue", 900, " for All");
  24.  
  25. // 输出执行 set 方法后的状态
  26. System.out.println("append status:" + fo.get());
  27. // 获取键对应的值
  28. System.out.println("W3xue value in cache - " + mcc.get("codingground"));
  29.  
  30. // 关闭连接
  31. mcc.shutdown();
  32. }catch(Exception ex)
  33. System.out.println(ex.getMessage());
  34. }
  35. }

prepend 操作实例

  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Future;
  3.  
  4. import net.spy.memcached.MemcachedClient;
  5.  
  6. public class MemcachedJava {
  7. public static void main(String[] args) {
  8. try{
  9. // 连接本地的 Memcached 服务
  10. MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  11. System.out.println("Connection to server sucessful.");
  12.  
  13. // 添加数据
  14. Future fo = mcc.set("W3xue", 900, "Education for All");
  15.  
  16. // 输出执行 set 方法后的状态
  17. System.out.println("set status:" + fo.get());
  18.  
  19. // 获取键对应的值
  20. System.out.println("W3xue value in cache - " + mcc.get("W3xue"));
  21.  
  22. // 对存在的key进行数据添加操作
  23. Future fo = mcc.prepend("W3xue", 900, "Free ");
  24.  
  25. // 输出执行 set 方法后的状态
  26. System.out.println("prepend status:" + fo.get());
  27. // 获取键对应的值
  28. System.out.println("W3xue value in cache - " + mcc.get("codingground"));
  29.  
  30. // 关闭连接
  31. mcc.shutdown();
  32. }catch(Exception ex)
  33. System.out.println(ex.getMessage());
  34. }
  35. }

CAS 操作实例

  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Future;
  3.  
  4. import net.spy.memcached.CASValue;
  5. import net.spy.memcached.CASResponse;
  6. import net.spy.memcached.MemcachedClient;
  7.  
  8. public class MemcachedJava {
  9. public static void main(String[] args) {
  10. try{
  11. // 连接本地的 Memcached 服务
  12. MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  13. System.out.println("Connection to server sucessful.");
  14.  
  15. // 添加数据
  16. Future fo = mcc.set("W3xue", 900, "Free Education");
  17.  
  18. // 输出执行 set 方法后的状态
  19. System.out.println("set status:" + fo.get());
  20. // 使用 get 方法获取数据
  21. System.out.println("W3xue value in cache - " + mcc.get("W3xue"));
  22.  
  23. // 通过 gets 方法获取 CAS token(令牌)
  24. CASValue casValue = mcc.gets("W3xue");
  25.  
  26. // 输出 CAS token(令牌) 值
  27. System.out.println("CAS token - " + casValue);
  28.  
  29. // 尝试使用cas方法来更新数据
  30. CASResponse casresp = mcc.cas("W3xue", casValue.getCas(), 900, "Largest Tutorials-Library");
  31. // 输出 CAS 响应信息
  32. System.out.println("CAS Response - " + casresp);
  33.  
  34. // 输出值
  35. System.out.println("W3xue value in cache - " + mcc.get("W3xue"));
  36.  
  37. // 关闭连接
  38. mcc.shutdown();
  39. }catch(Exception ex)
  40. System.out.println(ex.getMessage());
  41. }
  42. }

get 操作实例

  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Future;
  3.  
  4. import net.spy.memcached.MemcachedClient;
  5.  
  6. public class MemcachedJava {
  7. public static void main(String[] args) {
  8. try{
  9. // 连接本地的 Memcached 服务
  10. MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  11. System.out.println("Connection to server sucessful.");
  12.  
  13. // 添加数据
  14. Future fo = mcc.set("W3xue", 900, "Free Education");
  15.  
  16. // 输出执行 set 方法后的状态
  17. System.out.println("set status:" + fo.get());
  18.  
  19. // 使用 get 方法获取数据
  20. System.out.println("W3xue value in cache - " + mcc.get("W3xue"));
  21.  
  22. // 关闭连接
  23. mcc.shutdown();
  24. }catch(Exception ex)
  25. System.out.println(ex.getMessage());
  26. }
  27. }

gets 操作实例、CAS

  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Future;
  3.  
  4. import net.spy.memcached.CASValue;
  5. import net.spy.memcached.CASResponse;
  6. import net.spy.memcached.MemcachedClient;
  7.  
  8. public class MemcachedJava {
  9. public static void main(String[] args) {
  10. try{
  11. // 连接本地的 Memcached 服务
  12. MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  13. System.out.println("Connection to server sucessful.");
  14.  
  15. // 添加数据
  16. Future fo = mcc.set("W3xue", 900, "Free Education");
  17.  
  18. // 输出执行 set 方法后的状态
  19. System.out.println("set status:" + fo.get());
  20. // 从缓存中获取键为 W3xue 的值
  21. System.out.println("W3xue value in cache - " + mcc.get("W3xue"));
  22.  
  23. // 通过 gets 方法获取 CAS token(令牌)
  24. CASValue casValue = mcc.gets("W3xue");
  25.  
  26. // 输出 CAS token(令牌) 值
  27. System.out.println("CAS value in cache - " + casValue);
  28.  
  29. // 关闭连接
  30. mcc.shutdown();
  31. }catch(Exception ex)
  32. System.out.println(ex.getMessage());
  33. }
  34. }

delete 操作实例

  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Future;
  3.  
  4. import net.spy.memcached.MemcachedClient;
  5.  
  6. public class MemcachedJava {
  7. public static void main(String[] args) {
  8. try{
  9. // 连接本地的 Memcached 服务
  10. MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  11. System.out.println("Connection to server sucessful.");
  12.  
  13. // 添加数据
  14. Future fo = mcc.set("W3xue", 900, "World's largest online tutorials library");
  15.  
  16. // 输出执行 set 方法后的状态
  17. System.out.println("set status:" + fo.get());
  18.  
  19. // 获取键对应的值
  20. System.out.println("W3xue value in cache - " + mcc.get("W3xue"));
  21.  
  22. // 对存在的key进行数据添加操作
  23. Future fo = mcc.delete("W3xue");
  24.  
  25. // 输出执行 delete 方法后的状态
  26. System.out.println("delete status:" + fo.get());
  27.  
  28. // 获取键对应的值
  29. System.out.println("W3xue value in cache - " + mcc.get("codingground"));
  30.  
  31. // 关闭连接
  32. mcc.shutdown();
  33. }catch(Exception ex)
  34. System.out.println(ex.getMessage());
  35. }
  36. }

Incr/Decr 操作实例

  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Future;
  3.  
  4. import net.spy.memcached.MemcachedClient;
  5.  
  6. public class MemcachedJava {
  7. public static void main(String[] args) {
  8. try{
  9. // 连接本地的 Memcached 服务
  10. MemcachedClient mcc = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
  11. System.out.println("Connection to server sucessful.");
  12.  
  13. // 添加数字值
  14. Future fo = mcc.set("number", 900, "1000");
  15.  
  16. // 输出执行 set 方法后的状态
  17. System.out.println("set status:" + fo.get());
  18.  
  19. // 获取键对应的值
  20. System.out.println("value in cache - " + mcc.get("number"));
  21.  
  22. // 自增并输出
  23. System.out.println("value in cache after increment - " + mcc.incr("number", 111));
  24.  
  25. // 自减并输出
  26. System.out.println("value in cache after decrement - " + mcc.decr("number", 112));
  27.  
  28. // 关闭连接
  29. mcc.shutdown();
  30. }catch(Exception ex)
  31. System.out.println(ex.getMessage());
  32. }
  33. }
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号