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

简介: 1 修改pom.xml,添加依赖文件: <dependency>     <groupId>com.whalin</groupId>     <artifactId>Memcached-Java-Client</artifactId>     <version>3.0.2</versi


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";

         }

 



目录
相关文章
|
1月前
|
消息中间件 安全 Unix
SSH配置多台服务器之间的免密登陆以及登陆别名
SSH配置多台服务器之间的免密登陆以及登陆别名
38 1
|
1月前
|
网络安全
检查使用IP协议远程维护的设备是否配置SSH协议,禁用telnet协议
检查使用IP协议远程维护的设备是否配置SSH协议,禁用telnet协议
13 0
|
4月前
|
Ubuntu 安全 网络安全
百度搜索:蓝易云【Ubuntu系统SSH服务端配置】
现在,你已经成功在Ubuntu系统上配置了SSH服务端。这将允许其他计算机通过SSH协议连接到你的Ubuntu系统,并进行远程管理和操作。请注意,远程访问有安全风险,建议在生产环境中采取必要的安全措施来保护系统。
41 3
|
3月前
|
安全 Shell 网络安全
【Git】TortoiseGit(小乌龟)配置SSH和使用
【Git】TortoiseGit(小乌龟)配置SSH和使用
171 0
|
1天前
|
网络安全
|
26天前
|
网络协议 安全 Shell
配置ssh服务
配置ssh服务
|
1月前
|
Shell 网络安全 开发工具
配置SSH时候,Permission denied问题解决方法
配置SSH时候,Permission denied问题解决方法
45 4
|
2月前
|
安全 Shell 网络安全
ssh配置无密码验证
ssh配置无密码验证要在SSH中配置无密码验证,您需要使用公钥验证【2月更文挑战第18天】
43 1
|
2月前
|
Shell 网络安全 数据安全/隐私保护
配置多个SSH公钥流程
配置多个SSH公钥流程
|
3月前
|
网络安全
github或gitee配置ssh
github或gitee配置ssh
25 0