memcached 在 spring中的使用

memcached的初始化配置:

1、context-datasource.properties  配置文件

#--------------memcache server conf------------
memcache.server=127.0.0.1:11211
memcache.initConn=5
memcache.minConn=5
memcache.maxConn=250
memcache.maintSleep=30
memcache.nagle=false
memcache.socketTO=3000
2、spring-memcache.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<description>redis config</description>
			<value>/WEB-INF/config/context-datasource.properties</value>
		</property>
	</bean>

	<bean id="sockIOPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown">  
        <constructor-arg value="neeaMemcachedPool"/>
        <property name="servers">  
            <list>  
                <value>${memcache.server}</value>  
            </list>  
        </property>  
        <property name="initConn" value="${memcache.initConn}"/>  
        <property name="maxConn" value="${memcache.maxConn}"/> 
        <property name="maintSleep" value="${memcache.maintSleep}"/>
        <property name="nagle" value="${memcache.nagle}"/> 
        <property name="socketTO" value="${memcache.socketTO}"/> 
    </bean>  
  
    <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">  
        <constructor-arg value="neeaMemcachedPool"/> 
        <property name="compressEnable" value="true"/> 
        <property name="compressThreshold" value="4096" />
    </bean>  
</beans>
3、MemcachedUntil.java  工具类的编写

package com.company.until;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import com.danga.MemCached.MemCachedClient;

/**
 * Title MemcachedUntil.java
 * Description Memcach缓存的使用
 * Author 
 * Date 2016-3-1 下午02:51:42
 * Version V1.0
 */
public class MemcachedUntil {
	
	 @Autowired
	 private static MemCachedClient memcachedClient;
	 
	 public static void initMemcachedClient(final MemCachedClient springMemcachedClient){
		 memcachedClient = springMemcachedClient;
	 }	 
	 
     /**
      * @Description 更新缓存信息
      * @param key:键(key)
      * @param exp:过期时间(单位是秒)
      * @param obj:要设置缓存中的对象(value),如果没有则插入,如果有则修改。    
      * @return Future<Boolean>
      * @author 
      * @Date 2016-3-1 下午02:52:58
      */
	 public static boolean set(String key, Object obj ,int exp){
		 return memcachedClient.set(key, obj,new Date(exp*1000));
	 }
	
	 /**
	  * @Description 根据键(key)获取缓存数据信息
	  * @param
	  * @return Object
	  * @author 
	  * @Date 2016-3-1 下午02:59:15
	  */
	 public static Object get(String key){
		 return memcachedClient.get(key);
	 }
	 
	 /**
	  * @Description 根据此key 删除数值
	  * @param
	  * @return boolean
	  * @author 
	  * @Date 2016-3-1 下午03:06:45
	  */
	 public static boolean delete(String key) {    
	        return memcachedClient.delete(key);     
	 }
	 
	 /**
	  * @Description 添加 (key)信息   
	  * @param  
	  * @return boolean  如果key存在,则不存进去,返回false ,
	  *                  如果key不存在,则存进去,返回true
	  * @author
	  * @Date 2016-3-1 下午03:08:02
	  */
	 public static boolean add(String key,Object obj,int exp){  
	       return memcachedClient.add(key, obj,new Date(exp*1000)); 
	 }  
	 
	 /**
	  * @Description 清除缓存
	  * @param
	  * @return boolean
	  * @author 
	  * @Date 2016-3-1 下午03:10:42
	  */
	 public static boolean flush(final MemCachedClient memcachedClient){
		 return  memcachedClient.flushAll();   
	 }
	
	 /**
	  * @Description 替换缓存
	  * @param
	  * @return boolean
	  * @author
	  * @Date 2016-3-1 下午05:57:13
	  */
	 public static boolean replace(String key,Object obj,int exp){
		 return memcachedClient.replace(key, obj, new Date(exp * 1000));
	 }
}
4、系统初始化 memcached 数据

package com.company.init;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.company.until.ConfigureUtil;
import com.company.until.MemcachedUntil;
import com.company.until.RedisUtil;
import com.danga.MemCached.MemCachedClient;

/**
 * Title SystemInitialPoint.java
 * Description 系统初始化点,用于在系统启动后执行的一些初始化操作
 * Author 
 * Date 2016-2-25 下午05:04:05
 * Version V1.0
 */
@Service("systemInitialPoint")
public class SystemInitialPoint implements InitializingBean {

    private final Logger logger = Logger.getLogger(getClass());
    
    @Autowired
    private MemCachedClient memcachedClient;
 

    public void afterPropertiesSet() throws Exception {
        logger.info("========初始化系统参数服务=========================");
        
        logger.info("========初始化Memcached 服务=====================");
        MemcachedUntil.initMemcachedClient(memcachedClient);
    }
}
5、测试方法

	SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
	Object obj = MemcachedUntil.get("Memcached");
	if(obj != null){
	    System.out.println(sf.format(new Date()) + "  query memcached:" + (String)obj);
	}else{
	    String modStr = (Math.random()*100) + "";
	    Boolean bo = MemcachedUntil.set("Memcached", modStr, 10);//10秒钟的有效期
	    System.out.println(sf.format(new Date()) + "  add   memcached:" + modStr);
	    System.out.println(bo);
	}


注意:1、所需jar :java_memcached-release_2.5.3.jar  下载路径:http://pan.baidu.com/s/1mhk85s8

            2、注意:<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">   中是“com.danga.MemCached.MemCachedClient”这个jar,

                  且MemCachedUntil.java 中 导入的jar 也是com.danga.MemCached.MemCachedClient ,不可是net 开始的jar

       初来新手,忘多多指教:联系邮箱:    [email protected]


扫描二维码关注公众号,回复: 2429129 查看本文章

猜你喜欢

转载自blog.csdn.net/ly10265139/article/details/50777388