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 </beans:value>

</beans:property>

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

<beans:property </beans:value>

</beans:property>

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

<beans:property </beans:value>

</beans:property>

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

<beans:property </beans:value>

</beans:property>

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

<beans:property </beans:value>

</beans:property>

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

}




Memcached的配置,SSH项目中的整合(com.whalin),Memcached工具类,Memcached的代码调用的更多相关文章

  1. 2018.12.1 web项目中解决乱码问题的一个工具类

    <!-- 配置一个过滤器 编码格式的过滤器 --> <filter> <filter-name>encodeFilter</filter-name> & ...

  2. java模板模式项目中使用--封装一个http请求工具类

    需要调用http接口的代码继承FundHttpTemplate类,重写getParamData方法,在getParamDate里写调用逻辑. 模板: package com.crb.ocms.fund ...

  3. 初识quartz 并分析 项目中spring整合quartz的配置【原创+转载】

    初识quartz 并分析 项目中spring整合quartz的配置[原创+转载]2018年01月29日 12:08:07 守望dfdfdf 阅读数:114 标签: quartz 更多个人分类: 工具 ...

  4. Maven项目中Spring整合Mybatis

    Maven项目中Spring整合Mybatis 添加jar包依赖 spring需要的jar包依赖 <dependency> <groupId>org.springframewo ...

  5. .NET3.5中JSON用法以及封装JsonUtils工具类

    .NET3.5中JSON用法以及封装JsonUtils工具类  我们讲到JSON的简单使用,现在我们来研究如何进行封装微软提供的JSON基类,达到更加方便.简单.强大且重用性高的效果. 首先创建一个类 ...

  6. hessian在ssh项目中的配置

    一. 在服务端发布一个web项目 1.创建一个动态的web项目,并导入hessian的jar包 2. 在服务端的crm项目中创建接口 package cn.rodge.crm.service;impo ...

  7. 在SSH项目中实现分页效果

    在实现分页的时候,我使用的是数据库下面的User表,实现的效果是通过分页查询 能够将表中的数据分页显示,点击相关的按钮实现:首页.上一页.下一页.末页的显示 1新建一个dynamic web proj ...

  8. SSH 项目中 使用websocket 实现网页聊天功能

    参考文章  :java使用websocket,并且获取HttpSession,源码分析    http://www.cnblogs.com/zhuxiaojie/p/6238826.html 1.在项 ...

  9. 快速将wax配置到项目中进行lua开发

    通过Finder浏览到你保存该项目的文件夹.创建三个新的文件夹:wax.scripts和Classes. 第一:首先,下载源代码的压缩包.Wax放在GitHub上(https://github.com ...

随机推荐

  1. springMVC源码分析--RequestToViewNameTranslator请求到视图名称的转换

    RequestToViewNameTranslator可以在处理器返回的View为空时使用它根据Request获取viewName.RequestToViewNameTranslator提供的实现类只 ...

  2. Goaccess解析nginx日志备忘

    参考 http://nginx.org/en/docs/http/ngx_http_log_module.html?&_ga=1.92028562.949762386.1481787781#l ...

  3. RunLoop总结:RunLoop的应用场景(四)

    今天要介绍的RunLoop使用场景很有意思,在做长期项目,需要跟踪解决用户问题非常有用. 使用RunLoop 监测主线程的卡顿,并将卡顿时的线程堆栈信息保存下来,下次上传到服务器. 参考资料 关于今天 ...

  4. 从嵌入式linux到android应用开发

      时间过得很快,转眼之间已经到新公司一个月了.虽然学到了一些移动开发的知识,但是觉得离我的目标还很远,完全没能达到我想要的水平.以前产品都是自己主导的,需要完成什么,计划什么也是自己主导,现在得从头 ...

  5. python的subprocess:子程序调用(调用执行其他命令);获取子程序脚本当前路径问题

    python当前进程可以调用子进程,子进程可以执行其他命令,如shell,python,java,c... 而调用子进程方法有 os模块 参见:http://blog.csdn.net/longshe ...

  6. Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original

    Android异常:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that cr ...

  7. SDL2源代码分析6:复制到渲染器(SDL_RenderCopy())

    ===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...

  8. UNIX网络编程——网络层:IP

    一.IP数据报格式 IP数据报格式如下: 版本:IP协议版本号,长度为4位,IPv4此字段值为4,IPv6此字段值为6 首部长度:以32位的字为单位,该字段长度为4位,最小值为5,即不带任何选项的IP ...

  9. Oracle Metalink Notes Collection

    INV Note 123456.1 Latest 11i Applications Recommended Patch List Note 568012.1:FAQ: Inventory Standa ...

  10. android下在屏幕适配小总结

    为什么要屏幕适配?为此我就不说了,网上处理方法要么让你用几套不同分辨率的图片,要么写几套布局文件,要么就是在xml中写dip(这个还是可以的),前面两种感觉过程工作量太大了,由加载大图片的优化思想 同 ...