net.rubyeye.xmemcached.MemcachedClientBuilder Java Examples

The following examples show how to use net.rubyeye.xmemcached.MemcachedClientBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XMemcachedCacheManagerFactory.java    From memcached-spring-boot with Apache License 2.0 6 votes vote down vote up
@Override
IMemcachedClient memcachedClient() throws IOException {
    final List<InetSocketAddress> servers = properties.getServers();
    final MemcachedCacheProperties.Provider provider = properties.getProvider();
    final MemcachedCacheProperties.Protocol protocol = properties.getProtocol();

    final MemcachedClientBuilder builder = MemcachedCacheProperties.Provider.AWS.equals(provider) ?
            new AWSElasticCacheClientBuilder(servers) : new XMemcachedClientBuilder(servers);

    builder.setOpTimeout(properties.getOperationTimeout().toMillis());
    builder.setCommandFactory(MemcachedCacheProperties.Protocol.BINARY.equals(protocol) ?
            new BinaryCommandFactory() : new TextCommandFactory());

    if (properties.getHashStrategy() != MemcachedCacheProperties.HashStrategy.STANDARD) {
        builder.setSessionLocator(getSessionLocator(properties.getHashStrategy()));
    }

    return new XMemcachedClient(builder.build());
}
 
Example #2
Source File: MemcacheServlet.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
    ServletException {
  String addr =
      System.getenv().containsKey("GAE_MEMCACHE_HOST")
          ? System.getenv("GAE_MEMCACHE_HOST") : "localhost";
  String port =
      System.getenv().containsKey("GAE_MEMCACHE_HOST")
          ? System.getenv("GAE_MEMCACHE_PORT") : "11211";
  String key = "count";
  MemcachedClientBuilder builder = new XMemcachedClientBuilder(
      AddrUtil.getAddresses(addr + ":" + port));
  MemcachedClient client = builder.build();
  long count = 0L;
  try {
    count = client.incr(key, 1L, 0L);
  } catch (TimeoutException | InterruptedException | MemcachedException e) {
    throw new ServletException("Memcache error", e);
  }
  resp.setContentType("text/plain");
  resp.getWriter().print("Value is " + count + "\n");
}
 
Example #3
Source File: XmemcachedFactory.java    From pippo with Apache License 2.0 6 votes vote down vote up
/**
 * Create a memcached client with params.
 *
 * @param hosts whitespace separated host or IP addresses and port numbers
 * of the form "host:port host2:port hostN:portN"
 * @param protocol opcional, BINARY or TEXT
 * @param user opcional, user name o null
 * @param pass opcional, password o null
 * @param authMechanisms opcional, CRAM-MD5 and/or PLAIN
 * @return memcached client
 */
public static MemcachedClient create(
        String hosts,
        CommandFactory protocol,
        String user,
        String pass,
        String[] authMechanisms) {
    MemcachedClient client = null;
    try {
        MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(hosts));
        builder.setCommandFactory(protocol);
        if (isNotNullOrEmpty(user)) {
            builder.addAuthInfo(
                    AddrUtil.getAddresses(hosts).get(0),
                    new AuthInfo(
                            new PlainCallbackHandler(user, pass),
                            authMechanisms));
        }
        client = builder.build();
    } catch (IOException ex) {
        log.error("An error occurred when creating the MemcachedClient.", ex);
        throw new PippoRuntimeException(ex);
    }
    return client;
}
 
Example #4
Source File: MemcachedConfig.java    From ChengFeng1.5 with MIT License 5 votes vote down vote up
@Bean
public MemcachedClient getMemcachedClient() {
    MemcachedClient memcachedClient = null;
    try {
        MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.
                getAddresses(memcachedProperties.getServers()));
        builder.setConnectionPoolSize(memcachedProperties.getPoolSize());
        builder.setOpTimeout(memcachedProperties.getOpTimeout());
        memcachedClient = builder.build();
    } catch (IOException e) {
        log.error("inint MemcachedClient failed ",e);
    }
    return memcachedClient;
}
 
Example #5
Source File: XmemcachedCacheProvider.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public void start(Properties props) {

    long ct = System.currentTimeMillis();

    String servers = props.getProperty("servers", "127.0.0.1:11211");
    String username = props.getProperty("username", "");
    String password = props.getProperty("password", "");
    MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(servers));
    builder.setCommandFactory(new BinaryCommandFactory());
    boolean needAuth = username != null && password != null && username.trim().length() > 0 && password.trim().length() > 0;
    if(needAuth)
        builder.addAuthInfo(AddrUtil.getOneAddress(servers), AuthInfo.typical(username, password));

    builder.setConnectionPoolSize(Integer.valueOf(props.getProperty("connectionPoolSize", "10")));
    builder.setConnectTimeout(Long.valueOf(props.getProperty("connectTimeout", "1000")));
    builder.setHealSessionInterval(Long.valueOf(props.getProperty("healSessionInterval", "1000")));
    builder.setMaxQueuedNoReplyOperations(Integer.valueOf(props.getProperty("maxQueuedNoReplyOperations", "100")));
    builder.setOpTimeout(Long.valueOf(props.getProperty("opTimeout", "100")));
    builder.setSanitizeKeys("true".equalsIgnoreCase(props.getProperty("sanitizeKeys", "false")));

    try {
        client = builder.build();

        log.info("Memcached client starts with servers({}),auth({}),pool-size({}),time({}ms)",
                servers,
                needAuth,
                builder.getConfiguration().getSelectorPoolSize(),
                System.currentTimeMillis() - ct
        );
    } catch (IOException e) {
        log.error("Failed to connect to memcached", e);
    }
}
 
Example #6
Source File: MemcachedConfig.java    From SpringMVC-Project with MIT License 5 votes vote down vote up
@Bean
public MemcachedClient memcachedClient() throws IOException {
    MemcachedClientBuilder builder =
            new XMemcachedClientBuilder(AddrUtil.
                    getAddresses(address), new int[]{1});
    //存储的数据使用JSON格式,兼容不同客户端,如果是单一客户端可以不需要
    Transcoder JSON_TRANSCODER = new JSONTranscoder();
    builder.setTranscoder(JSON_TRANSCODER);

    return builder.build();
}