案例需求
1. 提供index.html页面,页面中有一个省份 下拉列表
2. 当 页面加载完成后 发送ajax请求,加载所有省份
分析
代码
未使用优化前:
service层
1 2 3 4 5 6 7 8
| public class ProvinceServiceImpl implements ProvinceService { private ProvinceDao dao = new ProvinceDaoImpl();
@Override public List<Province> findAll() { return dao.findAll(); }
|
dao层
1 2 3 4 5 6 7 8 9 10 11 12 13
| private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Override public List<Province> findAll() {
String sql = "select * from province"; List<Province> list = template.query(sql, new BeanPropertyRowMapper<Province>(Province.class)); System.out.println(list); return list; }
|
Redis优化
- 省份的数据不是经常发生变化的,每一次访问都去访问数据库就就提高了访问频率,会给数据库增加压力;利用缓存的思想(Redis缓存)。
service层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| package com.web.service.impl;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.web.Jedis.util.JedisPoolUtils; import com.web.dao.ProvinceDao; import com.web.dao.impl.ProvinceDaoImpl; import com.web.domain.Province; import com.web.service.ProvinceService; import redis.clients.jedis.Jedis;
import java.util.List;
public class ProvinceServiceImpl implements ProvinceService { private ProvinceDao dao = new ProvinceDaoImpl();
@Override public List<Province> findAll() { return dao.findAll(); }
@Override public String findAllJson() { Jedis jedis = JedisPoolUtils.getJedis(); String provice_json = jedis.get("province");
if (provice_json == null||provice_json.length()==0) { System.out.println("redis中没有数据,查询数据库。。。"); List<Province> ps = dao.findAll(); ObjectMapper mapper = new ObjectMapper(); try { provice_json = mapper.writeValueAsString(ps); } catch (JsonProcessingException e) { e.printStackTrace(); } jedis.set("province",provice_json); jedis.close();
}else { System.out.println("redis中有数据,查询缓存"); }
return provice_json; }
}
|
Servlet层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.web.web.servlet;
import com.web.service.ProvinceService; import com.web.service.impl.ProvinceServiceImpl;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;
@WebServlet(urlPatterns = "/findProvinceServlet") public class FindProvinceServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
ProvinceService service = new ProvinceServiceImpl(); String json = service.findAllJson(); response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
|
优化后
注意
1. 使用redis缓存一些不经常发生变化的数据。
2. 数据库的数据一旦发生改变,则需要更新缓存。
3. 数据库的表执行 增删改的相关操作,需要将redis缓存数据情况,再次存入
4. 在service对应的增删改方法中,将redis数据删除。
源码
点击获取源码