zoukankan      html  css  js  c++  java
  • spring与memcached整合[转]

    1, 开始肯定是下载需要的文件了,这里就下载附件里的文件就好,我也是在网上down的,放这好找。然后我们安装一下Memcache服务器,找到解压的memcached-1.2.1-win32,启动cmd ,进入解压目录,输入命令 D:memcached-1.2.6-win32memcached.exe -d install.然后再键入命令'D:memcachedmemcached.exe -d start'启动,这样memcache就会作为windows系统服务在每次开机时启动memcache服务。

    2,下面我们开始在使用java进行配置开发。添加Spring功能。在web.xml中添加配置。

     
    Xml代码  收藏代码
    1. <context-param>      
    2.             <param-name>contextConfigLocation</param-name>      
    3.             <param-value>classpath:/spring/applicationContext-common.xml,  
    4.             classpath:/spring/spring-memcache.xml  
    5.             </param-value>  
    6. </context-param>  

    3,在src下新建spring目录,并新建applicationContext-common.xml和spring-memcache.xml。内容分别如下。

    applicationContext-common.xml

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans  
    3.     xmlns="http://www.springframework.org/schema/beans"  
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.     xmlns:p="http://www.springframework.org/schema/p"  
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
    7.   
    8. <bean  
    9. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    10.     <property name="locations">  
    11.     <list>  
    12.         <value>classpath:memcache.properties</value>  
    13.         <value>classpath:jdbc.properties</value>  
    14.     </list>  
    15.     </property>  
    16. </bean>  
    17. <bean id="springContextHolder" class="com.hxrainbow.crm.util.SpringContextHolder"/>  
    18. </beans>  

    spring-memcache.xml

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans  
    3.     xmlns="http://www.springframework.org/schema/beans"  
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.     xmlns:p="http://www.springframework.org/schema/p"  
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
    7.   
    8. <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance"   
    9.     init-method="initialize"    destroy-method="shutDown">  
    10.         <property name="servers">  
    11.             <list>  
    12.                 <value>${memcache.server}</value>  
    13.             </list>  
    14.         </property>  
    15.         <property name="initConn">  
    16.             <value>${memcache.initConn}</value>  
    17.         </property>  
    18.         <property name="minConn">  
    19.             <value>${memcache.minConn}</value>  
    20.         </property>  
    21.         <property name="maxConn">  
    22.             <value>${memcache.maxConn}</value>  
    23.         </property>  
    24.         <property name="maintSleep">  
    25.             <value>${memcache.maintSleep}</value>  
    26.         </property>  
    27.         <property name="nagle">  
    28.             <value>${memcache.nagle}</value>  
    29.         </property>  
    30.         <property name="socketTO">  
    31.             <value>${memcache.socketTO}</value>  
    32.         </property>  
    33. </bean>  
    34. <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">  
    35. </bean>  
    36. </beans>  

    在配置文件中我们会看到memcache.properties,jdbc.properties和springContextHolder。

    他们的内容分别是:

    Xml代码  收藏代码
    1. memcache.properties  
    2.   
    3. memcache.server=127.0.0.1:11211  
    4. memcache.initConn=20  
    5. memcache.minConn=10  
    6. memcache.maxConn=50  
    7. memcache.maintSleep=3000  
    8. memcache.nagle=false  
    9. memcache.socketTO=3000  
    Xml代码  收藏代码
    1. jdbc.properties                       
    Java代码  收藏代码
    1. import java.util.Map;  
    2.   
    3. import org.springframework.context.ApplicationContext;  
    4. import org.springframework.context.ApplicationContextAware;  
    5.   
    6. /** 
    7.  *  
    8.  * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. 
    9.  **/  
    10.   
    11. public class SpringContextHolder implements ApplicationContextAware {  
    12.   
    13.     private static ApplicationContext applicationContext;  
    14.   
    15.     /** 
    16.      *  
    17.      * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. 
    18.      */  
    19.   
    20.     public void setApplicationContext(ApplicationContext applicationContext) {  
    21.         SpringContextHolder.applicationContext = applicationContext;  
    22.     }  
    23.     /** 
    24.      *  
    25.      * 取得存储在静态变量中的ApplicationContext. 
    26.      */  
    27.     public static ApplicationContext getApplicationContext() {  
    28.         checkApplicationContext();  
    29.         return applicationContext;  
    30.   
    31.     }  
    32.   
    33.     /** 
    34.      *  
    35.      * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. 
    36.      */  
    37.   
    38.     @SuppressWarnings("unchecked")  
    39.     public static <T> T getBean(String name) {  
    40.   
    41.         checkApplicationContext();  
    42.   
    43.         return (T) applicationContext.getBean(name);  
    44.   
    45.     }  
    46.   
    47.     /** 
    48.      *  
    49.      * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. 
    50.      *  
    51.      * 如果有多个Bean符合Class, 取出第一个. 
    52.      */  
    53.   
    54.     @SuppressWarnings({ "unchecked", "rawtypes" })  
    55.     public static <T> T getBean(Class<T> clazz) {  
    56.   
    57.         checkApplicationContext();  
    58.   
    59.         Map beanMaps = applicationContext.getBeansOfType(clazz);  
    60.   
    61.         if (beanMaps != null && !beanMaps.isEmpty()) {  
    62.   
    63.             return (T) beanMaps.values().iterator().next();  
    64.   
    65.         } else {  
    66.   
    67.             return null;  
    68.   
    69.         }  
    70.   
    71.     }  
    72.   
    73.     private static void checkApplicationContext() {  
    74.   
    75.         if (applicationContext == null) {  
    76.   
    77.             throw new IllegalStateException(  
    78.                     "applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");  
    79.   
    80.         }  
    81.   
    82.     }  

     配置文件我们写完了,下面我们开始测试使用。

    做Bean类

    Java代码  收藏代码
    1.  import java.io.Serializable;  
    2.   
    3. public class Bean implements Serializable{  
    4.       
    5.     /** 
    6.      *  
    7.      */  
    8.     private static final long serialVersionUID = 1L;  
    9.   
    10.     private String name;  
    11.       
    12.     private int age;  
    13.   
    14.     public String getName() {  
    15.         return name;  
    16.     }  
    17.   
    18.     public void setName(String name) {  
    19.         this.name = name;  
    20.     }  
    21.   
    22.     public int getAge() {  
    23.         return age;  
    24.     }  
    25.   
    26.     public void setAge(int age) {  
    27.         this.age = age;  
    28.     }  
    29.        
    30.     public String toString() {  
    31.          String bean = "{name:"+this.getName()+",age:"+this.getAge()+"}";  
    32.         return bean;  
    33.     }  
    34.        
    35.   
    36. }  

    做测试使用类:

    Java代码  收藏代码
    1. import java.util.ArrayList;  
    2. import java.util.List;  
    3.   
    4. import org.springframework.context.ApplicationContext;  
    5. import org.springframework.context.support.FileSystemXmlApplicationContext;  
    6.   
    7. import com.danga.MemCached.MemCachedClient;  
    8. import com.danga.MemCached.SockIOPool;  
    9.   
    10. public class MemcacheUtilTest {  
    11.        
    12.    public static void main(String[] args) {  
    13.          
    14.        ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[]{"src/spring/spring-memcache.xml","src/spring/applicationContext-common.xml"});  
    15.          
    16.        SockIOPool s =SpringContextHolder.getBean("memcachedPool");   
    17.        System.out.println("s="+s.getInitConn());  
    18.          
    19.        MemCachedClient mc = (MemCachedClient) ctx.getBean("memcachedClient");  
    20.          
    21.        //开始设值  
    22.        mc.set("name", " string  ");  
    23.        mc.set("int", 5);  
    24.        mc.set("double", 5.5);  
    25.          
    26.        Bean bean = new Bean();  
    27.        bean.setAge(21);  
    28.        bean.setName("名字");  
    29.          
    30.        mc.set("bean", bean);  
    31.          
    32.          
    33.        List<Bean> data = new ArrayList<Bean>();  
    34.        for(int i=0;i<3;i++)  
    35.        {  
    36.            Bean xbean = new Bean();  
    37.            xbean.setAge(i);  
    38.            xbean.setName("test_"+i);  
    39.            data.add(xbean) ;  
    40.        }  
    41.        mc.set("data", data);  
    42.          
    43.       try{  
    44.        Thread.sleep(50);  
    45.          
    46.        //开始取值  
    47.        String name =(String) mc.get("name");  
    48.        int i = (Integer) mc.get("int");  
    49.        double d = (Double) mc.get("double") ;  
    50.        Bean b = (Bean) mc.get("bean") ;  
    51.        data =  (List<Bean>) mc.get("data") ;  
    52.          
    53.        System.out.println("字符串:"+name);  
    54.        System.out.println("数字型:"+i);  
    55.        System.out.println("双精度:"+d);  
    56.        System.out.println("bean  toString :"+b.toString());  
    57.          
    58.        System.out.println("data  toString :"+data.toString());  
    59.          
    60.        //开始删除值  
    61.        System.out.println("开始删除 :》》》》》》》》》");  
    62.        mc.delete("name");  
    63.        mc.delete("int");  
    64.        mc.delete("double");  
    65.        mc.delete("bean");  
    66.           
    67.        String name_d =(String) mc.get("name");  
    68.        int i_d = (Integer) mc.get("int");  
    69.        double d_d = (Double) mc.get("double") ;  
    70.        Bean b_d = (Bean) mc.get("bean") ;  
    71.          
    72.        System.out.println("字符串:"+name_d);  
    73.        System.out.println("数字型:"+i_d);  
    74.        System.out.println("双精度:"+d_d);  
    75.        System.out.println("bean  toString :"+b_d.toString());  
    76.       }catch(Exception e){  
    77.        e.printStackTrace();  
    78.       }  
    79.      }  
    80.   
    81. }  

     运行一下,看看结果吧。具体可参考java_memcached-release_1.6docHOWTO.txt.注意Bean要实现序列化。

  • 相关阅读:
    Vue学习手记01-安装和项目创建
    [Powershell] FTP Download File
    [PowerShell] Backup Folder and Files Across Network
    SSRS 请求并显示SharePoint人员和组字段
    Beta 冲刺 (2/7)
    Beta 冲刺 (1/7)
    BETA 版冲刺前准备
    事后诸葛亮
    Alpha 答辩总结
    α冲刺 (10/10)
  • 原文地址:https://www.cnblogs.com/songjinduo/p/5159512.html
Copyright © 2011-2022 走看看