示例#1
0
 @Override
 public void afterPropertiesSet() throws Exception {
   if (!inited && enable) {
     try {
       ConnectionFactoryBuilder cfBuidler = new ConnectionFactoryBuilder();
       cfBuidler.setProtocol(Protocol.BINARY);
       cfBuidler.setOpTimeout(3000);
       cfBuidler.setTimeoutExceptionThreshold(3000);
       if (auth) {
         cfBuidler.setAuthDescriptor(
             new AuthDescriptor(
                 new String[] {"PLAIN"}, new PlainCallbackHandler(username, password)));
       }
       client = new MemcachedClient(cfBuidler.build(), AddrUtil.getAddresses(host + ":" + port));
       inited = true;
       logger.info("Memcached Client inited...");
     } catch (IOException e) {
       logger.error("Memcached client initial exception:", e);
       e.printStackTrace();
     }
   }
 }
  public static net.spy.memcached.MemcachedClient createSpyMemcachedClient(MemcacheConfig config) {
    try {
      ConnectionFactoryBuilder factoryBuilder = new ConnectionFactoryBuilder();

      net.spy.memcached.transcoders.SerializingTranscoder transcoder =
          new net.spy.memcached.transcoders.SerializingTranscoder();
      transcoder.setCompressionThreshold(config.getCompressionThreshold());

      factoryBuilder.setProtocol(ConnectionFactoryBuilder.Protocol.BINARY);
      factoryBuilder.setTranscoder(transcoder);
      factoryBuilder.setOpTimeout(config.getOpTimeout());
      factoryBuilder.setTimeoutExceptionThreshold(5);
      factoryBuilder.setLocatorType(ConnectionFactoryBuilder.Locator.CONSISTENT);
      factoryBuilder.setFailureMode(FailureMode.Redistribute);
      factoryBuilder.setUseNagleAlgorithm(false);
      factoryBuilder.setHashAlg(DefaultHashAlgorithm.KETAMA_HASH);

      return new net.spy.memcached.MemcachedClient(
          factoryBuilder.build(), AddrUtil.getAddresses(config.getServers()));
    } catch (Exception ex) {
      log.error("Failed to create SpyMemcachedClient", ex);
    }
    return null;
  }