Xmemcached 的简单使用

1. 前一篇文章简单说了下如何安装memcached,想到了memcached是分布式的缓存框架,所以自己在公司的3台测试机上安装了下。

2. 关于什么是xmemcached就不多说了,其实就是实现了memcached的java客户端,关于分布式下一篇会稍微详细点介绍,这里只要知道这个分布式其实是客户端的分布式,其实是没有主从节点的概念的。

3. 直接贴代码,看看如何使用

[java] view plain copy
  1. import java.io.IOException;  
  2. import java.util.concurrent.TimeoutException;  
  3.   
  4. import net.rubyeye.xmemcached.CASOperation;  
  5. import net.rubyeye.xmemcached.MemcachedClient;  
  6. import net.rubyeye.xmemcached.MemcachedClientBuilder;  
  7. import net.rubyeye.xmemcached.XMemcachedClientBuilder;  
  8. import net.rubyeye.xmemcached.exception.MemcachedException;  
  9. import net.rubyeye.xmemcached.utils.AddrUtil;  
  10.   
  11. public class Cache {  
  12.   
  13.     private MemcachedClientBuilder builder = null;  
  14.     private MemcachedClient client = null;  
  15.   
  16.     public Cache(String address, int[] weight) {  
  17.         builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(address), weight);  
  18.         builder.setConnectionPoolSize(5);  
  19.         try {  
  20.             client = builder.build();  
  21.         } catch (IOException e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25.   
  26.     public void set(String key, int exp, Object value) {  
  27.         try {  
  28.             if (!client.set(key, exp, value)) {  
  29.                 System.err.println("set error, key is " + key + " value is " + value);  
  30.             }  
  31.         } catch (TimeoutException e) {  
  32.             e.printStackTrace();  
  33.         } catch (InterruptedException e) {  
  34.             e.printStackTrace();  
  35.         } catch (MemcachedException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39.   
  40.     public void delete(String key) {  
  41.         try {  
  42.             client.delete(key);  
  43.         } catch (TimeoutException e) {  
  44.             e.printStackTrace();  
  45.         } catch (InterruptedException e) {  
  46.             e.printStackTrace();  
  47.         } catch (MemcachedException e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.     }  
  51.   
  52.     public void update(String key, final Object value) {  
  53.         try {  
  54.             client.cas(key, 10new CASOperation<Object>() {  
  55.                 public int getMaxTries() {  
  56.                     return 1;  
  57.                 }  
  58.   
  59.                 public Object getNewValue(long currentCAS, Object currentValue) {  
  60.                     return value;  
  61.                 }  
  62.             });  
  63.         } catch (TimeoutException e) {  
  64.             e.printStackTrace();  
  65.         } catch (InterruptedException e) {  
  66.             e.printStackTrace();  
  67.         } catch (MemcachedException e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.     }  
  71.   
  72.     public Object get(String key) {  
  73.         try {  
  74.             return client.get(key);  
  75.         } catch (TimeoutException e) {  
  76.             e.printStackTrace();  
  77.         } catch (InterruptedException e) {  
  78.             e.printStackTrace();  
  79.         } catch (MemcachedException e) {  
  80.             e.printStackTrace();  
  81.         }  
  82.         return null;  
  83.     }  
  84.       
  85.     public void addServer(String server, int port, int weight) {  
  86.         try {  
  87.             client.addServer(server, port, weight);  
  88.         } catch (IOException e) {  
  89.             e.printStackTrace();  
  90.         }  
  91.     }  
  92.       
  93.     public void removeServer(String hostList) {  
  94.         client.removeServer(hostList);  
  95.     }  
  96. }  
这个类做了一个特别简单的封装,包括了增删改查还有动态添加删除节点,其实就是简单得使用了下api,其实api很多,大家可以自己查api

下面是一个测试类

[java] view plain copy
  1. public static void main(String[] args) {  
  2. //      testCRUD();  
  3.         testSerObject();  
  4.     }  
  5.   
  6.     public static void testCRUD() {  
  7.         String address = "xx01:11211 xx02:11211 xx03:11211";  
  8.         Cache cache = new Cache(address, new int[] { 111 });  
  9.         cache.set("hello"10"test");  
  10.         System.out.println(cache.get("hello"));  
  11.         cache.update("hello""ssss");  
  12.         System.out.println(cache.get("hello"));  
  13.         cache.delete("hello");  
  14.         System.out.println(cache.get("hello"));  
  15.     }  
  16.       
  17.     public static void testSerObject() {  
  18.         String address = "arreat00:11211 arreat01:11211 arreat02:11211";  
  19.         Cache cache = new Cache(address, new int[] { 111 });  
  20.         User user = new User();  
  21.         user.setId(1);  
  22.         user.setMobile("13564316073");  
  23.         user.setEmail("[email protected]");  
  24.         user.setPasswd("ttt");  
  25.         user.setUserName("qianjc");  
  26.         List<String> asseat = new ArrayList<String>();  
  27.         asseat.add("asseat0");  
  28.         asseat.add("asseat1");  
  29.           
  30.         Map<String, String> car = new HashMap<String, String>();   
  31.         car.put("car1""val1");  
  32.         car.put("car2""val2");  
  33.           
  34.         user.setAsseat(asseat);  
  35.         user.setCar(car);  
  36.           
  37.         user.setBook(buildBook(1"memcached进阶""smart出版"));  
  38.         user.setBooks(buildBooks());  
  39.   
  40. //      cache.set("user1", 0, user);  
  41.         System.out.println(cache.get("user1"));  
  42.     }  
  43.       
  44.       
  45.     public static Book buildBook(int id, String name, String pulish) {  
  46.         Book book = new Book();  
  47.         book.setId(id);  
  48.         book.setName(name);  
  49.         book.setPulish(pulish);  
  50.         return book;  
  51.     }  
  52.       
  53.     public static List<Book> buildBooks() {  
  54.         List<Book> books = new ArrayList<Book>();  
  55.           
  56.         for (int i = 0; i < 2; ++i) {  
  57.             books.add(buildBook(i, "memcached进阶" + i, "smart出版" + i));  
  58.         }  
  59.         return books;  
  60.     }  
  61. }  
可以看到memcached不仅仅支持简单String,int等类型,只要实现了Serializable接口的java序列化对象都可以



具体怎么使用可以查api,还是比较简单的!!
也可以参考一篇博客:http://blog.csdn.net/ljhabc1982/article/details/6338898

你可能感兴趣的:(Xmemcached 的简单使用)