发表于: 2018-01-14 21:51:17
1 676
今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin)
对任务5的代码增加了缓存
这是主页在线学生数量和结业学生数量的查询.
package com.service;
import com.bean.GoodStudent;
import com.dao.GoodStudentDao;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
/**
* @author Arike
* Create_at 2017/12/28 16:07
*/
@Service
public class GoodStudentServiceImpl implements IGoodStudentService {
@Autowired
GoodStudentDao goodStudentDao;
@Autowired
MemcachedClient memcachedClient;
@Override
public List<GoodStudent> selectAll() {
try {
if(null==memcachedClient.get("selectAll")){
memcachedClient.set("selectAll", 60 * 60, goodStudentDao.selectAll());
}
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
}
try {
return memcachedClient.get("selectAll");
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
}
return goodStudentDao.selectAll();
}
@Override
public Integer count() {
try {
if(null == memcachedClient.get("count") || !(memcachedClient.get("count") instanceof Integer)){
memcachedClient.set("count",60*60,goodStudentDao.count());
}
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
}
try {
return memcachedClient.get("count");
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
}
return goodStudentDao.count();
}
@Override
public Integer countGood() {
try {
if(null == memcachedClient.get("count")){
memcachedClient.set("count",60*60,goodStudentDao.countGood());
}
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
}
try {
return memcachedClient.get("countGood");
} catch (TimeoutException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (MemcachedException e) {
e.printStackTrace();
}
return goodStudentDao.countGood();
}
}
这是对职业介绍的查询.
在做这个时候我疯狂报一个错误..因为我一开始代码写错了,我在count那里也set的是selectAll的结果,就导致了缓存里存储的不是一个integer,而存进去的是一个ArrayList,就导致return疯狂报错,而我自己又忘了这个事情,我就下意识的认为这个东西是默认的往里面存list..... 后来我加上了一段判断..
if(null == memcachedClient.get("count") || !(memcachedClient.get("count") instanceof Integer))
话说
instanceof
这个关键字是用的真的少,不过他是和==这些是相同的用于判断的,这里就相当于复习并且运用,
instanceof是判断左边的对象是否是右边对象的实例.我这里就相当于判断我查询出来的是否是一个integer,如果不是依然会重新Set.
另外我对登陆页面也做了拦截,用户如果已经登陆,禁止访问登陆页面,这点咱们官网就有点水了,并没有做,也就是我们登陆之后还能登陆.
很不人性化..
我对自己的页面做了拦截.
<mvc:interceptor>
<mvc:mapping path="/u/**"/>
<!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 -->
<bean class="com.interceptor.MyInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/l/**"/>
<!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 -->
<bean class="com.interceptor.LoginInterceptor"/>
</mvc:interceptor>
多配置了一个拦截器
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
Cookie cookie = WebUtils.getCookie(httpServletRequest, "key");
if(cookie !=null){
if (cookie.getValue().equals(httpServletRequest.getSession().getAttribute("key"))) { httpServletResponse.setHeader("refresh","0,/u/jnshu");
return false;
}
}
return true;
}
访问登陆页面的时候会验证是否已经存在登陆了的session.
另外分享一个东西就是tomcat容器关闭之后,生命周期未到的session会被序列化的输出到文件.
存储在tomcat的work下对象的项目里,重启之后会再把这个session读取出去.
明天计划的事情:(一定要写非常细致的内容)
小课堂,还有Redis的学习.
遇到的问题:(遇到什么困难,怎么解决的)
上面已经说了,自己造的bug是真的牛逼
收获:(通过今天的学习,学到了什么知识)
学会了使用instanceof,对session的生命周期有了更深一步的了解
评论