memcached与spring整合、测试

来源:互联网 发布:世界杯直播软件 编辑:程序博客网 时间:2024/05/16 07:09
测试代码:
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工具类:
package com.chj.memcached;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
public class MemcachedUtil {
/**

* memcached客户端单例
*/
private static MemCachedClient cachedClient = new MemCachedClient();
/**

* 初始化连接池
*/
static {
// 获取连接池的实例
SockIOPool pool = SockIOPool.getInstance();
// 服务器列表及其权重
String[] servers = { "192.168.2.251:12000", "192.168.2.252:12000" };
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);
}


public static void main(String[] args) {
System.out.println(cachedClient);
}
}

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.创建测试用例:


package com.chj.memcached;
import org.junit.Test;
public class MemcachedUtilTest {
@Test
public void testMemcached1() {
MemcachedUtil.put("hello", "world", 60);
String hello = (String) MemcachedUtil.get("hello");
System.out.println("world".equals(hello));
for (int i = 0; i < 100; ++i) {
UserBean userBean = new UserBean("Jason" + i, "123456-" + i);
MemcachedUtil.put("user" + i, userBean, 60);
Object obj = MemcachedUtil.get("user" + i);
System.out.println(userBean.equals(obj));
}
}

@Test
public void testMemcached2() {
for (int i = 0; i < 100; ++i) {
UserBean obj = (UserBean) MemcachedUtil.get("user" + i);
System.out.println(obj.toString());
}
}
}


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/chj/config/beans.xml");
      cachedClient = (MemCachedClient)context.getBean("memcachedClient");
  }

  @Test
  public void testMemcachedSpring() {
      UserBean user = new UserBean("chj", "derek");
      cachedClient.set("user", user);

      UserBean cachedBean = (UserBean)user;

      System.out.println(userBean.equals(cachedBean ));

  }
}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红米3s开不开机怎么办 皮肤被虫子咬了红肿痒怎么办 微信被骗了1万多怎么办 6个月宝宝吃了纸怎么办 农行k宝扣了50块怎么办 4g流量用的太快怎么办 怀疑老公有外遇最明智的怎么办 咽喉疼怎么办最简单的方法如下 生完孩子后腰疼的厉害怎么办 眼睛进东西了弄不出来怎么办 18k金不给换黄金怎么办 我22岁欠了10万怎么办 1岁宝宝又吐又拉怎么办 月经10天了还没干净怎么办 舌头有异味怎么办是有口臭吗 快8个月羊水破了怎么办 25岁欠了50万债怎么办 28岁血压高150低压110怎么办 苹果6的4g网络慢怎么办 一个月染了6次头怎么办 五0二干在衣服上怎么办 刚怀孕见红了肚子不痛怎么办 我有外遇了老婆不离婚怎么办 套了牙套的牙疼怎么办 我鼻子上有很多螨虫和黑头怎么办 鱼刺卡在喉咙怎么办最有效的办法 脚被蚊子咬了很痒怎么办 好压7z密码忘了怎么办 4g卡显示2g网络怎么办 过塑机把纸吞了怎么办 红米1s开不了机怎么办 跟老婆吵架闹的要离婚该怎么办 充了q币没有到账怎么办 9个月宝宝吃了盐怎么办 红米4x开不了机怎么办 鱼身上有红斑像出血了怎么办 草鱼身上有红斑像出血了怎么办 宝宝屁眼红的破皮了怎么办 孩子身上起红疙瘩很痒怎么办 久而不射,但软了怎么办 盆底综合肌力1级怎么办