发表于: 2017-11-25 15:21:46
1 675
今天完成的事
【继续MemCached】
jar包的问题,Memcache在maven仓库中是没有相应的jar包的,需要我们手动下载,手动导入
文档http://www.cnblogs.com/mycifeng/p/5882509.html
安装成功。
创建memcached工具类
package com.xiuzhen.utils;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
/**
* Created by ${MIND-ZR} on 2017/11/25.
*/
public class MemcachedUtil{
//创建全局的唯一实例
protected static MemCachedClient mcc = new MemCachedClient();
protected static MemcachedUtil memCached = new MemcachedUtil();
//设置与缓存服务器的连接池
static {
//服务器列表和其权重,个人memcached地址和端口号
String[] servers = {"192.168.31.106:11211"};
Integer[] weights = {3};
//获取socke连接池的实例对象
SockIOPool pool = SockIOPool.getInstance();
// 设置服务器信息
pool.setServers( servers );
pool.setWeights( weights );
//设置初始连接数、最小和最大连接数以及最大处理时间
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(100*60*60*6);
//设置主线程的睡眠时间
pool.setMaintSleep(60);
//设置TCP的参数,连接超时等
pool.setNagle(false);
pool.setSocketTO(60);
pool.setSocketConnectTO(0);
//初始化连接池
pool.initialize();
//压缩设置,超过指定大小的都压缩
// cachedClient.setCompressEnable(true);
// cachedClient.setCompressThreshold(1024*1024);
}
/**
* 保护型构造方法,不允许实例化!
*
*/
protected MemcachedUtil()
{
}
/**
* 获取唯一实例.
* @return
*/
public static MemcachedUtil getInstance()
{
return memCached;
}
/**
* 添加一个指定的值到缓存中.
* @param key
* @param value
* @return
*/
public static boolean add(String key,Object value){
return mcc.add(key,value);
}
public static boolean add(String key, Object value, Integer expity){
return mcc.add(key, value, expity);
}
/**
* 新建?一个指定的值到缓存中.
* @param key
* @param value
* @return
*/
public static boolean put(String key,Object value){return mcc.set(key, value);}
public static boolean put(String key,Object value,Integer expity){return mcc.set(key, value, expity);}
/**
* 替换一个指定的值到缓存中.
* @param key
* @param value
* @return
*/
public static boolean replace(String key,Object value){
return mcc.replace(key, value);
}
public static boolean replace(String key,Object value,Integer expity){
return mcc.replace(key, value, expity);
}
/**
* 删除一个指定的值到缓存中.
* @param key
* @return
*/
public static boolean delete(String key){
return mcc.delete(key);
}
/**
* 根据指定的关键字获取对象.
* @param key
* @return
*/
public static Object get(String key){
return mcc.get(key);
}
}
创建需要缓存的对象
package com.xiuzhen.domain;
import org.springframework.stereotype.Service;
import java.io.Serializable;
/**
* Created by ${MIND-ZR} on 2017/11/25.
*/
@Service
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;
}
}
创建测试用例:
import com.danga.MemCached.MemCachedClient;
import com.xiuzhen.domain.UserBean;
import com.xiuzhen.utils.MemcachedUtil;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by ${MIND-ZR} on 2017/11/25.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application.xml")
public class MemcachedSpringTest {
@Test
public void testMemcached(){
MemcachedUtil.put("hello","word",60);
String hello= (String) MemcachedUtil.get("hello");
Assert.assertEquals("word",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);
Assert.assertEquals(userBean, obj);
System.out.println(userBean );
}
测试结果
这里有三个点要注意。
第一、memcached是在服务器端的内存中缓存对象的,不是缓存或硬盘;
第二、memcached的pool可以关联多个server,
String[] servers = {"10.20.185.12:11001","10.20.185.25:11001"};
Integer[] weights = {3,7};
该配置表示30%的缓存在放在第一台服务器,70%的将放在第二台服务器,这样便可以充分利用不同服务器的内存了;
第三、我最困惑的是client是如何得到相应的pool的,后然看了点源码才知道是这样的。client是通过pool的name关联到某个pool的,上面的例子中在SockIOPool pool = SockIOPool.getInstance(); 和MemCachedClient client=new MemCachedClient();虽然都没写poolName,但就是新建了一个”default“的pool,然后client关联到了这个”default“的pool。当然我们在新建这两个对象时可以给定具体的poolName。
我该怎么在新建数据的时候将数据放到缓冲区,并且在下次使用的时候优先从数据库里面判断呢。
用我任务五的账号密码来做这件事
缓存里有的去缓冲里面去,缓存里面没有的去数据库比对,并且放入缓冲。
问了下师兄。POST方法账号密码是没意义的,不如加载主页面来的明显。
恍然大悟。重新来,将我主页面的上的数据放到缓存里。
先做一个类,我来把我的职业数数据类型放到里面。
MemcachedUtil.put("hello", "word", new Date(50));
String hello = (String) MemcachedUtil.get("hello");
System.out.println(hello);
Assert.assertEquals("word", hello);
List<Profession> professionsFout = professionService.getFroutEnd();
System.out.println(professionsFout.get(1));
List<Profession> professionsBack = professionService.getBackEnd();
List<Profession> professionsMobile = professionService.getMobileEnd();
MemcachedUtil.put("professionsFout", professionsFout, new Date(50));
MemcachedUtil.put("professionsBack", professionsBack, new Date(50));
MemcachedUtil.put("professionsMobile", professionsMobile, new Date(50));
List<Profession> abc = (List) MemcachedUtil.get("profess ionsFout");
System.out.println(abc.get(1));
但是MemCached好像不支持List类型,明天改成单个存
遇到的问题
第一个问题
MemcachedUtil.put("user" + i, userBean, 60);
感觉设置的缓存时间无效啊。
我过了60秒再来照这个缓存还能找到。
这是源码
他调用自己的方法了。无敌了。
永不过期的缓存。夭寿了。
明天的计划
任务六
评论