Memcached入门学习四(spring 集成MemCached)
来源:互联网 发布:json数组格式 编辑:程序博客网 时间:2024/05/22 08:11
使用jar包为:java_memcached-release_2.6.6.jar
在使用maven时,在maven中央仓库中,找不到该jar,需要我们手动上传该jar包。使用nexus 上传即可。
1、 进入仓库页面
2、选择3rd party 点击下方Artifact upload,填写信息,存在jar包,上传即可
上传后,既可以在maven项目中使用该jar包了。
<dependency> <groupId>com.danga</groupId> <artifactId>java-memcached</artifactId> <version>2.6.6</version></dependency>
Spring配置文件如下:
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-lazy-init="false"> <!-- 客户端:java_memcached-release_2.6.3 --> <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" lazy-init="false" destroy-method="shutDown"> <constructor-arg> <value>memcachedPool</value> </constructor-arg> <!-- 可以设置多个memcached服务器 --> <property name="servers"> <list> <value>${memcached.url}</value> </list> </property> <!-- 每个服务器初始连接数 --> <property name="initConn"> <value>${memcached.initConn}</value> </property> <!-- 每个服务器最小连接数 --> <property name="minConn"> <value>${memcached.minConn}</value> </property> <!-- 每个服务器最大连接数 --> <property name="maxConn"> <value>${memcached.maxConn}</value> </property> <!-- 主线程睡眠时间 --> <property name="maintSleep"> <value>${memcached.maintSleep}</value> </property> <!-- TCP/Socket的参数,如果是true在写数据时不缓冲,立即发送出去参数 --> <property name="nagle"> <value>${memcached.nagle}</value> </property> <!-- 连接超时/阻塞读取数据的超时间是 --> <property name="socketTO"> <value>${memcached.socketTO}</value> </property> </bean> <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient"> <constructor-arg> <value>memcachedPool</value> </constructor-arg> </bean></beans>
properties 文件如下:
#memcachedmemcached.url = 127.0.0.1:11211memcached.initConn = 20memcached.minConn = 20memcached.maxConn = 1000memcached.maintSleep = 30000memcached.nagle = falsememcached.socketTO =30000
Memcached 工具类如下:
private Logger logger = LoggerFactory.getLogger(MemCacheUtil.class); @Autowired private MemCachedClient memCachedClient; /** * memcached 缓存设值 * * @description : * @date 2017-11-7 * @author liuc * @param key * key键值 * @param value * 值 * @param expr * 有效时间 * @return * * */ public boolean set(String key, String value, int expr) { // 缓存设值 return memCachedClient.set(key, value, expr); } /** * get 时会存在一个问题: JAVA客户端flags字段填写的都是32,不是32的是无法通过java客户端get出来的 * 所以在通过memcached admin进行数据set时,需要显示指定flags值为32 set demo 32 0 5 12345 * 如此放入缓存后,通过java客户端是可以取出来的。 * * @description : * @date 2017-11-7 * @author liuc * @param key * @return * * */ public String get(String key) { return (String) memCachedClient.get(key); } /** * 添加一个指定的值到缓存中. * * @param key * @param value * @param expr * 有效时间 * @return */ public boolean add(String key, Object value, int expr) { // Calendar ca = Calendar.getInstance(); // ca.add(Calendar.HOUR, 2); if (memCachedClient.get(key) != null) { return memCachedClient.replace(key, value, expr); } else { return memCachedClient.add(key, value, expr); } } /** * 描述 : 替换 * * @param key */ public boolean replace(String key, Object value, int expir) { return memCachedClient.replace(key, value, expir); } public boolean replace(String key, Object value, Date expiry) { try { return memCachedClient.replace(key, value, expiry); } catch (Exception e) { return false; } } /** * 描述 : <删除缓存> * * @param key */ public boolean delete(String key) { try { return memCachedClient.delete(key); } catch (Exception e) { logger.error("delete this key error ,this key is"+key +" and the problem:"+e.getMessage()); return false; } }
阅读全文
0 0
- Memcached入门学习四(spring 集成MemCached)
- Memcached-学习(二)XMemcached&Spring集成
- MemCached的学习--与spring的集成
- memcached和spring集成
- memcached+spring集成
- memcached与spring集成
- memcached+spring集成
- memcached+spring集成
- Spring 集成 MemCached
- memcached和spring集成
- spring集成memcached
- Spring Cache集成memcached
- Spring集成Memcached与Memcached API
- Spring 集成 Memcached 配置(一)
- Spring 集成 Memcached 配置(二)
- simple-spring-memcached 入门
- spring集成memcached示例二
- spring集成memcached示例一
- ROS中几种常用的msg
- 平衡二叉树
- 使用nginx搭建集群tomcat8,redis实现session共享,文件共享问题
- linux常用命令
- VS2017安装ImageWatch
- Memcached入门学习四(spring 集成MemCached)
- Android判断手机是否安装微信(总结)
- 真正从零开始,TensorFlow详细安装入门图文教程!
- 大数加法
- 模拟电路学习总结
- 【总结】Unity:代码动态加载Prefab预设体
- Linux tomcat关闭 出现 java.net.ConnectException: Connection refused
- MFC更改环境目录可执行文件目录下继承的值
- 使用teragen产生数据