java项目中memcached的用法
使用memcached 转载 http://blog.csdn.net/loujinhe/article/details/8491673
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过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:
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); } }
相关推荐
郗瑞强 2020-08-16
85590296 2020-07-22
jkzyx 2020-06-29
luotuo 2020-06-26
LinuxJob 2020-06-26
ol0 2020-06-26
清溪算法君老号 2020-06-25
86251043 2020-06-13
CSDN0BLOG 2020-06-09
ol0 2020-05-26
andyhuabing 2020-05-22
程序员俱乐部 2020-05-06
83530391 2020-05-05
ol0 2020-05-02
83530391 2020-04-09
85590296 2020-03-25
carolAnn 2020-03-07
大脸猫脸大 2020-03-03
ol0 2020-02-18