Spring Memcache整合

https://github.com/gwhalin/Memcached-Java-Client/downloads

上面下载 jar包

spring 配置文件

XML/HTML code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<? xml  version = "1.0"  encoding = "UTF-8" ?>  
< beans  
     xmlns = "http://www.springframework.org/schema/beans"  
     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"  
     xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >  
   
<!--memcached 客户端***start-->
< bean  id = "memcachedPool"  class = "com.danga.MemCached.SockIOPool"  factory-method = "getInstance"  init-method = "initialize"  destroy-method = "shutDown" >
     < constructor-arg >
         < value >neeaMemcachedPool</ value >
     </ constructor-arg >
     < property  name = "servers" >
         < list >
             < value >127.0.0.1:11211</ value >
             <!--多服务器
              <value>
                 192.168.54.207:12346
             </value>
             -->
         </ list >
     </ property >
     <!--多服务器负载均衡-->
     <!--<property name="weights">
         <value>5,5</value>
     </property>-->
     < property  name = "initConn" >
         < value >20</ value >
     </ property >
     < property  name = "minConn" >
         < value >10</ value >
     </ property >
     < property  name = "maxConn" >
         < value >500</ value >
     </ property >
     < property  name = "maintSleep" >
         < value >30</ value >
     </ property >
     < property  name = "nagle" >
         < value >false</ value >
     </ property >
     < property  name = "maxIdle" >
         < value >6000</ value >
     </ property >
     < property  name = "socketTO" >
         < value >3000</ value >
     </ property >
</ bean >
 
<!--memcached client-->
< bean  id = "memcachedClient"  class = "com.danga.MemCached.MemCachedClient" >
     < constructor-arg >
            < value >neeaMemcachedPool</ value >
     </ constructor-arg >
     < property  name = "compressEnable" >
         < value >true</ value >
     </ property >
     < property  name = "compressThreshold" >
         < value >4096</ value >
     </ property >
</ bean >
  <!--      <bean id="cache" class="com.jrcz.aura.controller.AppXxqansrecordController">  
            <property name="memcached" ref="memcachedClient"/>
     </bean> --> 
</ beans >  

Java code
?
1
2
3
4
5
6
7
8
9
  ApplicationContext ctx= new  FileSystemXmlApplicationContext( "src/spring/applicationContext-memcached.xml" );  
         MemCachedClient mc = (MemCachedClient)ctx.getBean( "memcachedClient" );  
         List list =  new  ArrayList();
         list.add( "123" );
         list.add( "bbb" );
         
         mc.set( "key" , list);  
         List list1 = (List)mc.get( "key" );
         System.out.println(list1.get( 0 ));


也可以单独在文件中调用:

package com.eportal.util;


import java.util.Date;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;


import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;


/**
 * 使用memcached的缓存实用类.
 * 
 * @author 铁木箱子
 * 
 */
public class MemCached {
// 创建全局的唯一实例
protected static MemCachedClient mcc = new MemCachedClient();
 
protected static MemCached memCached = new MemCached();


// 设置与缓存服务器的连接池
static {
// 服务器列表和其权重
String[] servers = { "192.168.190.128:11211" };
Integer[] weights = { 3 };


// 获取socke连接池的实例对象
SockIOPool pool = SockIOPool.getInstance();


// 设置服务器信息
pool.setServers(servers);
pool.setWeights(weights);


// 设置初始连接数、最小和最大连接数以及最大处理时间
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000 * 60 * 60 * 6);


// 设置主线程的睡眠时间
pool.setMaintSleep(30);


// 设置TCP的参数,连接超时等
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);


// 初始化连接池
pool.initialize();


// 压缩设置,超过指定大小(单位为K)的数据都会被压缩
mcc.setCompressEnable(true);
mcc.setCompressThreshold(64 * 1024);
}


/**
* 保护型构造方法,不允许实例化!

*/
protected MemCached() {


}


/**
* 获取唯一实例.

* @return
*/
public static MemCached getInstance() {
return memCached;
}


/**
* 添加一个指定的值到缓存中.

* @param key
* @param value
* @return
*/
public boolean add(String key, Object value) {
return mcc.add(key, value);
}


public boolean add(String key, Object value, Date expiry) {
return mcc.add(key, value, expiry);
}


public boolean replace(String key, Object value) {
return mcc.replace(key, value);
}


public boolean replace(String key, Object value, Date expiry) {
return mcc.replace(key, value, expiry);
}


/**
* 根据指定的关键字获取对象.

* @param key
* @return
*/
public Object get(String key) {
return mcc.get(key);
}
  public static void main(String[] args){  
     
       for(int i=0; i<100; i++){  
           //try{Thread.sleep(2000);}catch(Exception e){}  
        mcc.set("key"+i, "value"+i);  
       }  
       try{Thread.sleep(5000);}catch(Exception e){}  
       for(int i=0; i<100; i++){  
           System.out.println("get "+i+" value "+mcc.get("key"+i));  
       }  
   }
 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值