JAVA2PHP的memcached访问

注意点一:服务器的配置顺序不同的客户端一定要一样,比如php配置的是serverB,serverC,serverA,那么java里也应该这样配置

注意点二:使用的hash算法必须一样,一般都是new crc32,java客户端的配置为pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);

注意点三:java客户端必须把mc中的value当作String来处理,配置方法为: client.setPrimitiveAsString( true )


以下是示例代码:


public class MemcachedTest {
   private final static ResourceBundle serversList = ResourceBundle.getBundle("servers");
   
   public static void main(String args[]) throws InterruptedException {
      
      SockIOPool pool = SockIOPool.getInstance();
      
      Enumeration<String> en = serversList.getKeys();
      List<String> sers = new ArrayList<String>();
      while (en.hasMoreElements()) {
         sers.add(serversList.getString(en.nextElement()));
      }
      
      String [] servers = new String[sers.size()];
      for (int k = 0; k < sers.size(); k++) {
        servers[sers.size() - k - 1] = sers.get(k).trim();
      }
   
      pool.setServers(servers);
      pool.setHashingAlg(SockIOPool.NEW_COMPAT_HASH);
      
      pool.setMinConn(50);
      pool.setMaxConn(150);
      pool.setSocketTO(3000);
      pool.initialize();
      
      MemCachedClient client = new MemCachedClient();
      client.setPrimitiveAsString( true );
//    client.setSanitizeKeys( false );
      
//    client.set("1", "1");
//    client.set("2", "2");
      
      System.out.println(client.get("1"));
      System.out.println(client.get("2"));
      /*if (args != null) {
        for (String itemId : args) {
           System.out.println("___ itemId is " + itemId + " __ the value is " + client.get(itemId.trim()));
        }
      } else {
        System.out.println("++++++++++++ the itemIds is null +++++++++++");
      }
      
      System.out.println("----------------------------- end ------------------------------" + client.get("/test/testAction?book.title=nihao"));*/
//    Thread.sleep(60000);
//    perfomance(client);
   }


你可能感兴趣的:(java,thread,算法,PHP,memcached)