当前位置: 首页 > 工具软件 > Spymemcached > 使用案例 >

java spymemcached 简单使用

云捷
2023-12-01

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

2、添加依赖  sbt 或mvn

sbt      
libraryDependencies += "net.spy" % "spymemcached" % "2.12 
maven

<dependency>
    <groupId>net.spy</groupId>
    <artifactId>spymemcached</artifactId>
    <version>2.12.3</version>
</dependency>

3、上代码

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;
    }
设值  MemcachedUtil.getMemcachedClient().set(key, 秒, value);
取值 MemcachedUtil.getMemcachedClient().get(key)

 

 

 

 类似资料: