Memcached的配置,SSH项目中的整合(com.whalin),Memcached工具类,Memcached的代码调用...



1 修改pom.xml,添加依赖文件:

<dependency>

    <groupId>com.whalin</groupId>

    <artifactId>Memcached-Java-Client</artifactId>

    <version>3.0.2</version>

</dependency>

2 添加memcached-context.xml,注意要在web.xml中进行配置

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns="http://www.springframework.org/schema/mvc"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"

    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

   

    <!—注意下面的:memcache在使用的时候会用到-->

    <beans:bean id="memcache" class="com.whalin.MemCached.SockIOPool"

       factory-method="getInstance" init-method="initialize" destroy-method="shutDown">

       <beans:constructor-arg>

           <beans:value>memcache</beans:value>

       </beans:constructor-arg>

       <beans:property name="servers">

           <beans:list>

                 <!--服务器地址-->

              <beans:value>172.16.24.27:11211</beans:value>

           </beans:list>

       </beans:property>

         <!--初始化时对每个服务器建立的连接数目-->

       <beans:property name="initConn">

           <beans:value>20</beans:value>

       </beans:property>

         <!--每个服务器建立最小的连接数-->

       <beans:property name="minConn">

           <beans:value>10</beans:value>

       </beans:property>

         <!--每个服务器建立最大的连接数-->

       <beans:property name="maxConn">

           <beans:value>50</beans:value>

       </beans:property>

         <!--自查线程周期进行工作,其每次休眠时间-->

       <beans:property name="maintSleep">

           <beans:value>1000</beans:value>

       </beans:property>

         <!--Socket的参数,如果是true在写数据时不缓冲,立即发送出去-->

       <beans:property name="nagle">

           <beans:value>false</beans:value>

       </beans:property>

         <!--Socket阻塞读取数据的超时时间-->

       <beans:property name="socketTO">

           <beans:value>1000</beans:value>

       </beans:property>


        

<!-- memcached的连接路径出现问题的时候,代码连接的时候时间超时设置 -->


       <beans:property name="socketConnectTO">


           <beans:value>500</beans:value>


       </beans:property>



    </beans:bean>

</beans:beans>

3 web.xml中配置:

4 编写MemcachedUtils,代码如下:

package com.kuman.cartoon.utils; 

 

import java.util.Date;

 

import org.apache.log4j.Logger;

 

import com.whalin.MemCached.MemCachedClient;

 

 

/**

 * @ClassName: MemcachedUtils

 * @Description: Memcached工具类

 * @author

 * @date 2015-8-6

 * 

 */

public class MemcachedUtils {

    private static final Logger logger = Logger.getLogger(MemcachedUtils.class); 

    private static MemCachedClient cachedClient;

    static { 

        if (cachedClient == null)

            //括号中的名称要和配置文件memcached-context.xml中的名称一致

            cachedClient = new MemCachedClient("memcache");

    } 

 

    private MemcachedUtils() {} 

 

    /**

     * 向缓存添加新的键值对。如果键已经存在,则之前的值将被替换。

     * 

     * @param key

     *           

     * @param value

     *           

     * @return

     */ 

    public static boolean set(String key, Object value) { 

        return setExp(key, value, null); 

    } 

 

    /**

     * 向缓存添加新的键值对。如果键已经存在,则之前的值将被替换。

     * 

     * @param key

     *           

     * @param value

     *           

     * @param expire

     *            过期时间 New Date(1000*10):十秒后过期

     * @return

     */ 

    public static boolean set(String key, Object value, Date expire) { 

        return setExp(key, value, expire); 

    } 

 

    /**

     * 向缓存添加新的键值对。如果键已经存在,则之前的值将被替换。

     * 

     * @param key

     *           

     * @param value

     *           

     * @param expire

     *            过期时间 New Date(1000*10):十秒后过期

     * @return

     */ 

    private static boolean setExp(String key, Object value, Date expire) { 

        boolean flag = false; 

        try { 

            flag = cachedClient.set(key, value, expire); 

        } catch (Exception e) { 

            // 记录Memcached日志 

                 logger.error("Memcached set方法报错,key值:" + key + "\r\n"); 

        } 

        return flag; 

    } 

 

    /**

     * 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。

     * 

     * @param key

     *           

     * @param value

     *           

     * @return

     */ 

    public static boolean add(String key, Object value) { 

        return addExp(key, value, null); 

    } 

 

    /**

     * 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。

     * 

     * @param key

     *           

     * @param value

     *           

     * @param expire

     *            过期时间 New Date(1000*10):十秒后过期

     * @return

     */ 

    public static boolean add(String key, Object value, Date expire) { 

        return addExp(key, value, expire); 

    }

 

    /**

     * 仅当缓存中不存在键时,add 命令才会向缓存中添加一个键值对。

     * 

     * @param key

     *           

     * @param value

     *           

     * @param expire

     *            过期时间 New Date(1000*10):十秒后过期

     * @return

     */ 

    private static boolean addExp(String key, Object value, Date expire) { 

        boolean flag = false; 

        try { 

            flag = cachedClient.add(key, value, expire); 

        } catch (Exception e) { 

            // 记录Memcached日志 

            logger.error("Memcached add方法报错,key值:" + key + "\r\n"); 

        } 

        return flag; 

    } 

 

    /**

     * 仅当键已经存在时,replace 命令才会替换缓存中的键。

     * 

     * @param key

     *           

     * @param value

     *           

     * @return

     */ 

    public static boolean replace(String key, Object value) { 

        return replaceExp(key, value, null); 

    } 

 

    /**

     * 仅当键已经存在时,replace 命令才会替换缓存中的键。

     * 

     * @param key

     *           

     * @param value

     *           

     * @param expire

     *            过期时间 New Date(1000*10):十秒后过期

     * @return

     */ 

    public static boolean replace(String key, Object value, Date expire) { 

        return replaceExp(key, value, expire); 

    } 

 

    /**

     * 仅当键已经存在时,replace 命令才会替换缓存中的键。

     * 

     * @param key

     *           

     * @param value

     *           

     * @param expire

     *            过期时间 New Date(1000*10):十秒后过期

     * @return

     */ 

    private static boolean replaceExp(String key, Object value, Date expire) { 

        boolean flag = false; 

        try { 

            flag = cachedClient.replace(key, value, expire); 

        } catch (Exception e) { 

            logger.error("Memcached replace方法报错,key值:" + key + "\r\n"); 

        } 

        return flag; 

    } 

 

    /**

     * get 命令用于检索与之前添加的键值对相关的值。

     * 

     * @param key

     *           

     * @return

     */ 

    public static Object get(String key) { 

        Object obj = null; 

        try { 

            obj = cachedClient.get(key); 

        } catch (Exception e) { 

            logger.error("Memcached get方法报错,key值:" + key + "\r\n"); 

        } 

        return obj; 

    } 

 

    /**

     * 删除 memcached 中的任何现有值。

     * 

     * @param key

     *           

     * @return

     */ 

    public static boolean delete(String key) { 

        return deleteExp(key, null); 

    } 

 

    /**

     * 删除 memcached 中的任何现有值。

     * 

     * @param key

     *           

     * @param expire

     *            过期时间 New Date(1000*10):十秒后过期

     * @return

     */ 

    public static boolean delete(String key, Date expire) { 

        return deleteExp(key, expire); 

    } 

 

    /**

     * 删除 memcached 中的任何现有值。

     * 

     * @param key

     *           

     * @param expire

     *            过期时间 New Date(1000*10):十秒后过期

     * @return

     */ 

    private static boolean deleteExp(String key, Date expire) { 

        boolean flag = false; 

        try { 

            flag = cachedClient.delete(key, expire); 

        } catch (Exception e) { 

            logger.error("Memcached delete方法报错,key值:" + key + "\r\n");

        } 

        return flag; 

    } 

 

    /**

     * 清理缓存中的所有键/值对

     * 

     * @return

     */ 

    public static boolean flashAll() { 

        boolean flag = false; 

        try { 

            flag = cachedClient.flushAll(); 

        } catch (Exception e) { 

            logger.error("Memcached flashAll方法报错\r\n"); 

        } 

        return flag; 

    } 

 

    /*@Test

    public void testMemcachedSpring() { 

        MemcachedUtils.set("aa", "bb", new Date(1000 * 60)); 

        Object obj = MemcachedUtils.get("aa"); 

        System.out.println("***************************"); 

        System.out.println(obj.toString()); 

    }*/

        

}

5 SpringMVC中调用的方式:

@RequestMapping(value = "/toIndex")

         public String toIndex(Model model) {

                 //方法一,这种不建议使用

                 //MemCachedClient memCachedClient = new MemCachedClient("memcache");

              //memCachedClient.set("name", "simple");

              //System.out.println(memCachedClient.get("name"));

            

                //方法二,建议这种

             MemcachedUtils.set("name", "simple");

             String name = (String)MemcachedUtils.get("name");

             System.out.println(name);

                  

             return "/admin/index";

         }

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值