发表于: 2017-04-26 23:31:20
2 1556
一、今天完成的事情
一、什么是Memcahe
一般来说我们使用数据库会需要持久化数据,什么是持久化呢,老哥我们坐下来慢慢说。持久化叫啥叫持久转状态、对应的还有一个瞬时状态。说起持久老哥们就会想到啪啪啪,那么打个比方,比如你一直在健身,那么你在行房事的时候是不是就会不自然的持久,那假如你不健身,却又想有相对应的能力,那么可以找外界方法刺激,但是刺激完成之后是不是状态就消失了。这就是持久和瞬时的意思,你把自己比喻成数据,持久化就是通过一些方法(比如数据库-健身)把自己写到硬盘中去,那么是不是你只要没有不小心嗝屁了就会一直有这个能力。这里说了这么多持久化,其实Memcahe是对内存进行操作。
一般需要理解memcache是用来做什么的就行了。怎么在JAVA中使用它,你需要安装windows稳定版
就这个东西
还需要导入jar包
编写一个工具类
package cn.li.task4.util;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import java.util.Date;
public class MemCachedManager {
// 创建全局的唯一实例
protected static MemCachedClient mcc = new MemCachedClient();
protected static MemCachedManager memCachedManager = new MemCachedManager();
// 设置与缓存服务器的连接池
static {
// 服务器列表和其权重
String[] servers = { "127.0.0.1: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(1000 * 60 * 60 * 6);
// 设置主线程的睡眠时间
pool.setMaintSleep(30);
// 设置TCP的参数,连接超时等
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
// 初始化连接池
pool.initialize();
// 压缩设置,超过指定大小(单位为K)的数据都会被压缩
// mcc.setCompressEnable(true);
// mcc.setCompressThreshold(64 * 1024);
}
/**
* 保护型构造方法,不允许实例化!
*
*/
protected MemCachedManager() {
}
/**
* 获取唯一实例.
*
* @return
*/
public static MemCachedManager getInstance() {
return memCachedManager;
}
/**
* 添加一个指定的值到缓存中.
*
* @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, Date expiry) {
return mcc.add(key, value, expiry);
}
public boolean replace(String key, Object value) {
return mcc.replace(key, value);
}
public boolean replace(String key, Object value, Date expiry) {
return mcc.replace(key, value, expiry);
}
public boolean put(String key,Object value){
return mcc.set(key,value);
}
public boolean put(String key, Object value, Date expiry) {
return mcc.set(key, value, expiry);
}
/**
* 根据指定的关键字获取对象.
*
* @param key
* @return
*/
public static Object get(String key) {
return mcc.get(key);
}
public static void main(String[] args) {
MemCachedManager cache = MemCachedManager.getInstance();
cache.add("hello", 234);
System.out.print("get value : " + cache.get("hello"));
}
}
完成测试,简单的网页编写就是写一个实体类调用,显示就行了
@Override
public List<Student> getAllStudents() {
List<Student> listSutdent;
if(MemCachedManager.get("listSutdent")!=null){
listSutdent =(List<Student>) MemCachedManager.get("listSutdent");
logger.debug("从缓存中调用memcache");
System.out.println("从缓存中调用数据");
return listSutdent;
}
listSutdent = dao.getAllStudents();
MemCachedManager.add("listSutdent",listSutdent);
System.out.println("没有从缓存中调用数据,添加到了缓存");
logger.debug("没有从缓从中调用数据");
return listSutdent;
}}
@RequestMapping(value = "/list",method = RequestMethod.GET)
public String getUsers(ModelMap model){
// List<Student> studentList =dao.getAllStudents();
List<Student> studentList = service.getAllStudents();
// modelMap.addAttribute("studenList",studentList);
// modelMap.put("studentList",studentList);
model.put("studentList",studentList);
return "/list";
二、遇到的问题:
我的缓存调用打出的日志一直在添加缓存,而没有从缓存输出过,这一点我很奇怪,
所以数据只是没有调用缓存(改了循环时间,through正常了,没有今天跑的随便破500。。虽然还没太理解)
Label | # Samples | Average | Median | 90% Line | 95% Line | 99% Line | Min | Max | Error % | Throughput | Received KB/sec | Sent KB/sec |
HTTP请求 | 200 | 2 | 2 | 3 | 4 | 5 | 1 | 10 | 0.00% | 41.83225 | 136.45 | 4.94 |
总体 | 200 | 2 | 2 | 3 | 4 | 5 | 1 | 10 | 0.00% | 41.83225 | 136.45 | 4.94 |
HTTP请求 | 400 | 2 | 2 | 4 | 4 | 6 | 1 | 18 | 0.00% | 81.11945 | 264.59 | 9.59 |
总体 | 400 | 2 | 2 | 4 | 4 | 6 | 1 | 18 | 0.00% | 81.11945 | 264.59 | 9.59 |
HTTP请求 | 600 | 2 | 3 | 4 | 4 | 6 | 1 | 10 | 0.00% | 120.91898 | 394.4 | 14.29 |
总体 | 600 | 2 | 3 | 4 | 4 | 6 | 1 | 10 | 0.00% | 120.91898 | 394.4 | 14.29 |
四、收获“
理解memcache的构造和过程挺多的,这里就不列出来了,因为这个问题还没有解决。还在想这个问题
评论