memcache的使用

使用memcache的两种实现jar包:

1、使用net.py.spymemcached:

  1)在pom.xml中添加对net.py.spymemcached的依赖;

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

  2)使用Demo:

package spymemcached;

import java.io.IOException;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.util.Date;

import net.spy.memcached.MemcachedClient;

public class MemcacheUse {
        private static class MyData implements Serializable {
               private static final long serialVersionUID = 1L;
               private long d = new Date().getTime();

               public String toString() {
                      return "my data [" + d + "]" ;
              }
       }

        public static void main(String[] args) throws IOException {
              MyData myData = new MyData();
               MemcachedClient c = new MemcachedClient (new InetSocketAddress(
                            "127.0.0.1", 11211));
               // Store a value (async) for one hour
              c.set( "someKey", 3600, myData);
               // Retrieve a value (synchronously).
              MyData myObject = (MyData) c.get( "someKey");
              c.shutdown();
              System. out.println(myObject.toString());
       }
}

 3)使用Demo,根据需求进行封装的类:

package com.practice.cache;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.springframework.stereotype.Component;

import net.spy.memcached.MemcachedClient;
import net.spy.memcached.internal.OperationFuture;

@Component
public class PhonePtCache {
	private MemcachedClient client;

	private String hostName = "127.0.0.1";
	private int port = 11211;
	private int time = 3600;

	public PhonePtCache() {
		try {
			client = new MemcachedClient(new InetSocketAddress(hostName,port));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public OperationFuture<Boolean> set(String key, Object o) {
		return client.set(key, time, o);
	}

	public Object get(String key) {
		return client.get(key);
	}

	public OperationFuture<Boolean> delete(String key) {
		return client.delete(key);
	}
}

 

2、使用com.whalin.Memcached-Java-Client

  1)在pom.xml中添加依赖:

<dependency>
       <groupId>com.whalin</groupId>
       <artifactId>Memcached-Java-Client</artifactId>
       <version>3.0.2</version>
</dependency>

   2)使用Demo

package spymemcached;

import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;

public class MemCacheInvoke {
    protected static MemCachedClient mcc = new MemCachedClient();
    static{
       // 设置缓存服务器列表,当使用分布式缓存的时,可以指定多个缓存服务器。(这里应该设置为多个不同的服务器)
        String[] servers =
                {
                        "127.0.0.1:11211",
                };

        // 设置服务器权重
        Integer[] weights = {3, 2};
        // 创建一个Socked连接池实例
        SockIOPool pool = SockIOPool. getInstance();
      // 向连接池设置服务器和权重
        pool.setServers(servers);
        pool.setWeights(weights);
        // set some TCP settings
        // disable nagle
        // set the read timeout to 3 secs
        // and don't set a connect timeout
        pool.setNagle( false);
        pool.setSocketTO(3000);
        pool.setSocketConnectTO(0);
       // initialize the connection pool
        pool.initialize();
    }

    public static void main(String[] args) {
        mcc.set("foo" , "This is a test String" );
        String bar = mcc.get("foo" ).toString();
        System. out.println(">>> " + bar);
    }
}

 

 

你可能感兴趣的:(memcache)