Java使用memcached
1.加载commons-pool-1.5.6.jar、java_memcached-release_2.6.6.jar、slf4j-api-1.6.1.jar、slf4j-simple-1.6.1.jar
2.创建memcached工具类:
- public class MemcachedUtil {
- /**
- * memcached客户端单例
- */
- private static MemCachedClient cachedClient = new MemCachedClient();
- /**
- * 初始化连接池
- */
- static {
- //获取连接池的实例
- SockIOPool pool = SockIOPool.getInstance();
- //服务器列表及其权重
- String[] servers = {"127.0.0.1:11211"};
- Integer[] weights = {3};
- //设置服务器信息
- pool.setServers(servers);
- pool.setWeights(weights);
- //设置初始连接数、最小连接数、最大连接数、最大处理时间
- pool.setInitConn(10);
- pool.setMinConn(10);
- pool.setMaxConn(1000);
- pool.setMaxIdle(1000*60*60);
- //设置连接池守护线程的睡眠时间
- pool.setMaintSleep(60);
- //设置TCP参数,连接超时
- pool.setNagle(false);
- pool.setSocketTO(60);
- pool.setSocketConnectTO(0);
- //初始化并启动连接池
- pool.initialize();
- //压缩设置,超过指定大小的都压缩
- // cachedClient.setCompressEnable(true);
- // cachedClient.setCompressThreshold(1024*1024);
- }
- private MemcachedUtil(){
- }
- public static boolean add(String key, Object value) {
- return cachedClient.add(key, value);
- }
- public static boolean add(String key, Object value, Integer expire) {
- return cachedClient.add(key, value, expire);
- }
- public static boolean put(String key, Object value) {
- return cachedClient.set(key, value);
- }
- public static boolean put(String key, Object value, Integer expire) {
- return cachedClient.set(key, value, expire);
- }
- public static boolean replace(String key, Object value) {
- return cachedClient.replace(key, value);
- }
- public static boolean replace(String key, Object value, Integer expire) {
- return cachedClient.replace(key, value, expire);
- }
- public static Object get(String key) {
- return cachedClient.get(key);
- }
- }
3. 创建需要缓存的对象:
- public class UserBean implements Serializable {
- private static final long serialVersionUID = 9174194101246733501L;
- private String username;
- private String password;
- public UserBean(String username, String password) {
- this.username = username;
- this.password = password;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result
- + ((password == null) ? 0 : password.hashCode());
- result = prime * result
- + ((username == null) ? 0 : username.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- UserBean other = (UserBean) obj;
- if (password == null) {
- if (other.password != null)
- return false;
- } else if (!password.equals(other.password))
- return false;
- if (username == null) {
- if (other.username != null)
- return false;
- } else if (!username.equals(other.username))
- return false;
- return true;
- }
- @Override
- public String toString() {
- return "username:" + username + ",password:" + password;
- }
- }
4.创建测试用例:
- public class MemcachedUtilTest {
- @Test
- public void testMemcached() {
- MemcachedUtil.put("hello", "world", 60);
- String hello = (String) MemcachedUtil.get("hello");
- Assert.assertEquals("world", hello);
- for(int i = 0; i < 10000000; ++i) {
- UserBean userBean = new UserBean("Jason" + i, "123456-" + i);
- MemcachedUtil.put("user" + i, userBean, 60);
- Object obj = MemcachedUtil.get("user" + i);
- Assert.assertEquals(userBean, obj);
- }
- }
- }
5.通过spring注入memcached:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
- factory-method="getInstance" init-method="initialize">
- <constructor-arg>
- <value>neeaMemcachedPool</value>
- </constructor-arg>
- <property name="servers">
- <list>
- <value>127.0.0.1:11211</value>
- </list>
- </property>
- <property name="initConn">
- <value>20</value>
- </property>
- <property name="minConn">
- <value>10</value>
- </property>
- <property name="maxConn">
- <value>50</value>
- </property>
- <property name="nagle">
- <value>false</value>
- </property>
- <property name="socketTO">
- <value>3000</value>
- </property>
- </bean>
- <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
- <constructor-arg>
- <value>neeaMemcachedPool</value>
- </constructor-arg>
- </bean>
- </beans>
6.创建测试用例:
- public class MemcachedSpringTest {
- private MemCachedClient cachedClient;
- @Before
- public void init() {
- ApplicationContext context = new ClassPathXmlApplicationContext("com/loujinhe/config/beans.xml");
- cachedClient = (MemCachedClient)context.getBean("memcachedClient");
- }
- @Test
- public void testMemcachedSpring() {
- UserBean user = new UserBean("lou", "jason");
- cachedClient.set("user", user);
- UserBean cachedBean = (UserBean)user;
- Assert.assertEquals(user, cachedBean);
- }
- }
Java使用memcached的更多相关文章
- Java操作Memcached
本文复制其他播客,有好的技术文章希望各位大神能告知... 谢谢. 如何使用Java操作Memcached实例: 代码一: package com.ghj.packageoftool; import j ...
- memcached—Java操作Memcached实例
前面博客介绍了如何在Windows操作系统中安装Memcached,总结一下如何使用Java操作Memcached实例: 代码一: package com.ghj.packageoftool; imp ...
- php和java的memcached使用的兼容性问题解决过程
1. 背景: php 使用memcached客户端设置一个key,java使用java-memcached-client去读,报错如下: ERROR|com.whalin.MemCached.MemC ...
- Java操作memcached(一)
Memcached事实上,两次Hash算法 第一次hash算法被用于定位Memcached示例 第二次hash算法是底部HashMap中间hash算法 Hash算法 1.依据余数 ...
- java集成memcached、redis防止缓存穿透
下载相关jar,安装Memcached,安装教程:http://www.runoob.com/memcached/memcached-install.html spring配置memcached &l ...
- Java使用Memcached和Redis简单示例
package xmq.study.memcached; import java.io.IOException; import java.net.InetSocketAddress; import n ...
- Java连接Memcached进行CRUD
参考这篇博文在本机安装了Memcached 在 Java 中常用的memcached有三个: Memcached Client for Java SpyMemcached XMemcached 这里使 ...
- Java 连接 Memcached 服务
原文:http://www.runoob.com/memcached/java-memcached.html mac下安装和配置Memcached:http://www.pchou.info/open ...
- java中memcached
http://www.oschina.net/code/snippet_250396_9181
随机推荐
- C# 之 获取文件名及拓展名
1.用Path类的方法(最常用) string fullPath = @"\WebSite\Default.aspx"; string filename = System.IO.P ...
- iOS之Git的使用
1.登录Git账号,创建一个新的仓库
- IE兼容低版本
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><m ...
- css3渐变、背景、倒影、变形
一.背景切割background-clip 语法:background-clip:border-box | padding-box | content-box: border-box 超出b ...
- ubuntu中下运行asp.net程序
首先在ubuntu下面是不能直接运行VISUAL STUTIO的,必须借助mono开发工具和xsp4.0.那我们就来看一下在ubuntu的电脑中怎么安装这两个工具. 首先安装mono,打开终端,输入a ...
- WebService简单介绍
什么是Web Services 一直没有一个明确的答案,最近听了一个大牛讲WebService,顿时明了了,作个记录免得以后忘了. N年之前,各公司都在炒作SOA,不过现在已经没人再提了,现在都改叫W ...
- JAVA远程执行Shell脚本类
1.java远程执行shell脚本类 package com.test.common.utility; import java.io.IOException; import java.io.Input ...
- ubuntu修改grub背景
grub背景由/etc/grub.d/05_debian_theme定义,修改图片只需要将图片文件放到/boot/grub,d/下即可, 修改颜色只需编辑/boot/grub.d/grub.cfg
- Redis多机集群
Redis集群.网上很多教程,只是按着它的步骤来做只能在单机上跑,而已不有点抗.也不用密码验证 开始: 1:redis集群最少需要要6个服务器端,因此先搞6台虚拟机 我用 centOS-7 mini ...
- java培训(5-8节课)
面向对象: 1.利用面向对象的语法,实现代码的拆分(数据存储:对象的传值). 2.利用面向对象的语法,实现程序的结构处理(继承,多态,接口,抽象类). 3.用面向对象的编程方法,理解实现程序开发的框架 ...