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工具类:

  1. public class MemcachedUtil {
  2. /**
  3. * memcached客户端单例
  4. */
  5. private static MemCachedClient cachedClient = new MemCachedClient();
  6. /**
  7. * 初始化连接池
  8. */
  9. static {
  10. //获取连接池的实例
  11. SockIOPool pool = SockIOPool.getInstance();
  12. //服务器列表及其权重
  13. String[] servers = {"127.0.0.1:11211"};
  14. Integer[] weights = {3};
  15. //设置服务器信息
  16. pool.setServers(servers);
  17. pool.setWeights(weights);
  18. //设置初始连接数、最小连接数、最大连接数、最大处理时间
  19. pool.setInitConn(10);
  20. pool.setMinConn(10);
  21. pool.setMaxConn(1000);
  22. pool.setMaxIdle(1000*60*60);
  23. //设置连接池守护线程的睡眠时间
  24. pool.setMaintSleep(60);
  25. //设置TCP参数,连接超时
  26. pool.setNagle(false);
  27. pool.setSocketTO(60);
  28. pool.setSocketConnectTO(0);
  29. //初始化并启动连接池
  30. pool.initialize();
  31. //压缩设置,超过指定大小的都压缩
  32. //      cachedClient.setCompressEnable(true);
  33. //      cachedClient.setCompressThreshold(1024*1024);
  34. }
  35. private MemcachedUtil(){
  36. }
  37. public static boolean add(String key, Object value) {
  38. return cachedClient.add(key, value);
  39. }
  40. public static boolean add(String key, Object value, Integer expire) {
  41. return cachedClient.add(key, value, expire);
  42. }
  43. public static boolean put(String key, Object value) {
  44. return cachedClient.set(key, value);
  45. }
  46. public static boolean put(String key, Object value, Integer expire) {
  47. return cachedClient.set(key, value, expire);
  48. }
  49. public static boolean replace(String key, Object value) {
  50. return cachedClient.replace(key, value);
  51. }
  52. public static boolean replace(String key, Object value, Integer expire) {
  53. return cachedClient.replace(key, value, expire);
  54. }
  55. public static Object get(String key) {
  56. return cachedClient.get(key);
  57. }
  58. }

3. 创建需要缓存的对象:

  1. public class UserBean implements Serializable {
  2. private static final long serialVersionUID = 9174194101246733501L;
  3. private String username;
  4. private String password;
  5. public UserBean(String username, String password) {
  6. this.username = username;
  7. this.password = password;
  8. }
  9. public String getUsername() {
  10. return username;
  11. }
  12. public void setUsername(String username) {
  13. this.username = username;
  14. }
  15. public String getPassword() {
  16. return password;
  17. }
  18. public void setPassword(String password) {
  19. this.password = password;
  20. }
  21. @Override
  22. public int hashCode() {
  23. final int prime = 31;
  24. int result = 1;
  25. result = prime * result
  26. + ((password == null) ? 0 : password.hashCode());
  27. result = prime * result
  28. + ((username == null) ? 0 : username.hashCode());
  29. return result;
  30. }
  31. @Override
  32. public boolean equals(Object obj) {
  33. if (this == obj)
  34. return true;
  35. if (obj == null)
  36. return false;
  37. if (getClass() != obj.getClass())
  38. return false;
  39. UserBean other = (UserBean) obj;
  40. if (password == null) {
  41. if (other.password != null)
  42. return false;
  43. } else if (!password.equals(other.password))
  44. return false;
  45. if (username == null) {
  46. if (other.username != null)
  47. return false;
  48. } else if (!username.equals(other.username))
  49. return false;
  50. return true;
  51. }
  52. @Override
  53. public String toString() {
  54. return "username:" + username + ",password:" + password;
  55. }
  56. }

4.创建测试用例:

  1. public class MemcachedUtilTest {
  2. @Test
  3. public void testMemcached() {
  4. MemcachedUtil.put("hello", "world", 60);
  5. String hello = (String) MemcachedUtil.get("hello");
  6. Assert.assertEquals("world", hello);
  7. for(int i = 0; i < 10000000; ++i) {
  8. UserBean userBean = new UserBean("Jason" + i, "123456-" + i);
  9. MemcachedUtil.put("user" + i, userBean, 60);
  10. Object obj = MemcachedUtil.get("user" + i);
  11. Assert.assertEquals(userBean, obj);
  12. }
  13. }
  14. }

5.通过spring注入memcached:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
  7. factory-method="getInstance" init-method="initialize">
  8. <constructor-arg>
  9. <value>neeaMemcachedPool</value>
  10. </constructor-arg>
  11. <property name="servers">
  12. <list>
  13. <value>127.0.0.1:11211</value>
  14. </list>
  15. </property>
  16. <property name="initConn">
  17. <value>20</value>
  18. </property>
  19. <property name="minConn">
  20. <value>10</value>
  21. </property>
  22. <property name="maxConn">
  23. <value>50</value>
  24. </property>
  25. <property name="nagle">
  26. <value>false</value>
  27. </property>
  28. <property name="socketTO">
  29. <value>3000</value>
  30. </property>
  31. </bean>
  32. <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
  33. <constructor-arg>
  34. <value>neeaMemcachedPool</value>
  35. </constructor-arg>
  36. </bean>
  37. </beans>

6.创建测试用例:

    1. public class MemcachedSpringTest {
    2. private MemCachedClient cachedClient;
    3. @Before
    4. public void init() {
    5. ApplicationContext context = new ClassPathXmlApplicationContext("com/loujinhe/config/beans.xml");
    6. cachedClient = (MemCachedClient)context.getBean("memcachedClient");
    7. }
    8. @Test
    9. public void testMemcachedSpring() {
    10. UserBean user = new UserBean("lou", "jason");
    11. cachedClient.set("user", user);
    12. UserBean cachedBean = (UserBean)user;
    13. Assert.assertEquals(user, cachedBean);
    14. }
    15. }

Java使用memcached的更多相关文章

  1. Java操作Memcached

    本文复制其他播客,有好的技术文章希望各位大神能告知... 谢谢. 如何使用Java操作Memcached实例: 代码一: package com.ghj.packageoftool; import j ...

  2. memcached—Java操作Memcached实例

    前面博客介绍了如何在Windows操作系统中安装Memcached,总结一下如何使用Java操作Memcached实例: 代码一: package com.ghj.packageoftool; imp ...

  3. php和java的memcached使用的兼容性问题解决过程

    1. 背景: php 使用memcached客户端设置一个key,java使用java-memcached-client去读,报错如下: ERROR|com.whalin.MemCached.MemC ...

  4. Java操作memcached(一)

    Memcached事实上,两次Hash算法    第一次hash算法被用于定位Memcached示例    第二次hash算法是底部HashMap中间hash算法 Hash算法      1.依据余数 ...

  5. java集成memcached、redis防止缓存穿透

    下载相关jar,安装Memcached,安装教程:http://www.runoob.com/memcached/memcached-install.html spring配置memcached &l ...

  6. Java使用Memcached和Redis简单示例

    package xmq.study.memcached; import java.io.IOException; import java.net.InetSocketAddress; import n ...

  7. Java连接Memcached进行CRUD

    参考这篇博文在本机安装了Memcached 在 Java 中常用的memcached有三个: Memcached Client for Java SpyMemcached XMemcached 这里使 ...

  8. Java 连接 Memcached 服务

    原文:http://www.runoob.com/memcached/java-memcached.html mac下安装和配置Memcached:http://www.pchou.info/open ...

  9. java中memcached

    http://www.oschina.net/code/snippet_250396_9181

随机推荐

  1. Helpers\Cookie

    Helpers\Cookie The Cookie helper has the following methods: Cookie::exists($key); Returns true or fa ...

  2. Authentication

    Authentication Introduction Configuration Storing Passwords Authenticating Users Basic Usage Introdu ...

  3. Upgrade Guide

    Upgrade Guide This guide will point out the key points to be aware of when upgrading to version 3. A ...

  4. Ⅰ.Spring的点点滴滴--序章

    spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架 .net篇(环境为vs2012+Spring.Core.dll) 新建一个控制台 using Spring.Context; ...

  5. DHCP服务自动分配IP地址原理

    转载自:http://blog.csdn.net/lycb_gz/article/details/8499559 DHCP在提供服务时,DHCP客户端是以UDP 68号端口进行数据传输的,而DHCP服 ...

  6. WebKit笔记

    加载网页时执行javascript代码 let mWebView = WKWebView.init(frame: self.view.bounds) self.view.addSubview(mWeb ...

  7. Xcode文件目录选中变成白色, 解决方案

    新版Xcode很不稳定, 有时候被选中文件变成白色, 看着很不舒服, 以前都是毫无办法, 等它自动变回来, 现在有一个解决办法, 点击文件目录上面的选项, 随便切换一个再切换回来, 发现文件目录颜色回 ...

  8. TextView实现跑马灯效果

    网上有很多跑马灯的介绍,有很多跑马灯的代码.或许我的不是最好的,但是应该很容易明白的. 我们先来介绍一个跑马灯的代码 <LinearLayout xmlns:android="http ...

  9. 比之前那个版本更简单的C语言实现的比较大小

    之前那个是输入一堆数据,找最大那个,这次是更简单的版本,求两个数的最大值. #include "stdafx.h" #include <stdio.h> int Get ...

  10. 转载:一句代码改变Swing难看的字体

    Swing 皮肤的一个键值:swing.boldMetal 默认为 true因此造成了默认字体极度难看: 其实一句代码就能解决问题:UIManager.put("swing.boldMeta ...