发表于: 2017-05-07 23:45:06

1 1131


今天完成的事:

1、查资料:缓存Memcached以及缓存策略,memcached的最佳实践方案

参考自:http://blog.csdn.net/scalzdp/article/details/25717531;http://www.cnblogs.com/suredandan/archive/2013/07/21/3204159.html

2、将部分增删改查代码中加入缓存

  public void deleteStudent(int id) {

studentDao.deleteStudent(id);
       if(memcachedUtils.get("student_"+id)!=null){
memcachedUtils.delete("student_"+id);
       }

}

public void updateStudent(Student student) {
studentDao.updateStudent(student);
       memcachedUtils.set("student_"+student.getId(),student);
   }

public Student getStudentById(int id) {
Student student=new Student();
       if(memcachedUtils.get("student_"+id)!=null){
student= (Student) this.memcachedUtils.get("student_"+id);
           System.out.println("本次操作是在缓存中获取");
       }else {
student=studentDao.getStudentById(id);
           this.memcachedUtils.add("student_"+id,student);
           System.out.println("本次操作是在数据库中获取");
       }
return  student;
//     return studentDao.getStudentById(id);
   }
public List<Student> getAllStudent() {
//获取学生列表的ID的集合
       List<Integer> intList=studentDao.getStudentIdList();
       List<Student> studentList=new ArrayList<Student>();
       Student student;
       //遍历id集合
       for (Integer id:intList){

if(memcachedUtils.get("student_"+id)!=null){ //如果缓存中存在
     student= (Student) memcachedUtils.get("student_"+id);   //从缓存中获取student
       System.out.println("从缓存中获取StudentID="+id);
       studentList.add(student);   //将获取的student存入studentList
  }else { //如果缓存中不存在
      student=studentDao.getStudentById(id);            //从数据库中获取Student
        System.out.println("从数据库中获取StudentID="+id);
        memcachedUtils.add("student_"+id,student);   //将数据库中获取的student存入缓存中
      studentList.add(student);           //并将student添加到studentList
      }
}
return studentList;                 //返回studentList
//        return studentDao.getAllStudent();
   }

jmeter 测showList接口线程 10000 8秒  循环1次软件未响应(前面测4秒有0.32%错误,jmeter却没问题可惜没截图)

明天计划的事:继续压测,学习redis

收获:

memcached能接受的key的最大长度是250字符

memcached最大能存储1MB的单个item。如果需要被缓存的数据大于1MB,可以考虑在客户端压缩或拆分到多个key中。等

问题:

不知道什么时候我本地memcached控制台窗口中输入指令都变成乱码了(还没去找解决办法,明天找找看)

总结:好好学习


返回列表 返回列表
评论

    分享到