简介:
利用memcached可以实现缓存服务器,memcached利用TCP将缓存存储在其他服务器中,且memcached是利用内存看空间来保存缓存数据的,减少了不必要的磁盘I/O。另外,memcached在存储区中对于每一个key都维护一个过期时间,一旦达到这个过期时间,memcached便会自动删除该key,方便了我们的过期检查机制的实现,只需在保存缓存数据时指定过期时间即可。
使用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:
1 package spymemcached; 2 3 import java.io.IOException; 4 import java.io.Serializable; 5 import java.net.InetSocketAddress; 6 import java.util.Date; 7 8 import net.spy.memcached.MemcachedClient; 9 10 public class MemcacheUse { 11 private static class MyData implements Serializable { 12 private static final long serialVersionUID = 1L; 13 private long d = new Date().getTime(); 14 15 public String toString() { 16 return "my data [" + d + "]" ; 17 } 18 } 19 20 public static void main(String[] args) throws IOException { 21 MyData myData = new MyData(); 22 MemcachedClient c = new MemcachedClient (new InetSocketAddress( 23 "127.0.0.1", 11211)); 24 // Store a value (async) for one hour 25 c.set( "someKey", 3600, myData); 26 // Retrieve a value (synchronously). 27 MyData myObject = (MyData) c.get( "someKey"); 28 c.shutdown(); 29 System. out.println(myObject.toString()); 30 } 31 }
3)使用Demo,根据需求进行封装的类:
1 package com.practice.cache; 2 3 import java.io.IOException; 4 import java.net.InetSocketAddress; 5 6 import org.springframework.stereotype.Component; 7 8 import net.spy.memcached.MemcachedClient; 9 import net.spy.memcached.internal.OperationFuture; 10 11 @Component 12 public class PhonePtCache { 13 private MemcachedClient client; 14 15 private String hostName = "127.0.0.1"; 16 private int port = 11211; 17 private int time = 3600; 18 19 public PhonePtCache() { 20 try { 21 client = new MemcachedClient(new InetSocketAddress(hostName,port)); 22 } catch (IOException e) { 23 e.printStackTrace(); 24 } 25 } 26 27 public OperationFuture<Boolean> set(String key, Object o) { 28 return client.set(key, time, o); 29 } 30 31 public Object get(String key) { 32 return client.get(key); 33 } 34 35 public OperationFuture<Boolean> delete(String key) { 36 return client.delete(key); 37 } 38 }
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
1 package spymemcached; 2 3 import com.whalin.MemCached.MemCachedClient; 4 import com.whalin.MemCached.SockIOPool; 5 6 public class MemCacheInvoke { 7 protected static MemCachedClient mcc = new MemCachedClient(); 8 static{ 9 // 设置缓存服务器列表,当使用分布式缓存的时,可以指定多个缓存服务器。(这里应该设置为多个不同的服务器) 10 String[] servers = 11 { 12 "127.0.0.1:11211", 13 }; 14 15 // 设置服务器权重 16 Integer[] weights = {3, 2}; 17 // 创建一个Socked连接池实例 18 SockIOPool pool = SockIOPool. getInstance(); 19 // 向连接池设置服务器和权重 20 pool.setServers(servers); 21 pool.setWeights(weights); 22 // set some TCP settings 23 // disable nagle 24 // set the read timeout to 3 secs 25 // and don't set a connect timeout 26 pool.setNagle( false); 27 pool.setSocketTO(3000); 28 pool.setSocketConnectTO(0); 29 // initialize the connection pool 30 pool.initialize(); 31 } 32 33 public static void main(String[] args) { 34 mcc.set("foo" , "This is a test String" ); 35 String bar = mcc.get("foo" ).toString(); 36 System. out.println(">>> " + bar); 37 } 38 }