Java spymemcached 简单使用

  1. 下载并安装memcached 百度一大把

  2. 添加依赖 sbt 或mvn

    sbt

    1
    libraryDependencies += "net.spy" % "spymemcached" % "2.12

    maven

    1
    2
    3
    4
    5
    <dependency>
    <groupId>net.spy</groupId>
    <artifactId>spymemcached</artifactId>
    <version>2.12.3</version>
    </dependency>
  3. 上代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     public class MemcachedUtil {
    // 日志对象
    private static final play.Logger.ALogger logger = play.Logger.of(MemcachedUtil.class);

    static final String servers = "127.0.0.1:11211";

    public static MemcachedClient mc = null;

    public static MemcachedClient getMemcachedClient(){
    if (mc != null) {
    return mc;
    }
    try{
    mc = new MemcachedClient(new ConnectionFactoryBuilder()
    .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).build(), AddrUtil.getAddresses(servers));
    }catch(Exception ex){
    logger.error(ex.getMessage());
    }

    return mc;
    }
  4. 设值

    1
    2
    MemcachedUtil.getMemcachedClient().set(key, 秒, value);
    MemcachedUtil.getMemcachedClient().get(key)

Java spymemcached 简单使用
http://yoursite.com/post/7af52029.html/
Author
Chase Wang
Posted on
December 20, 2017
Licensed under