页面缓存
简单理解缓存原理
互联网架构
页面缓存
使用Oscache实现页面缓存。
测试页面缓存
创建web工程,导入jar
- commons-logging.jar
- oscache-2.4.1.jar
测试
创建一个index.jsp页面,使用时间来测试:
访问地址
http://localhost:8080/Oscache19/index.jsp
http://localhost:8080/Oscache19/
分析:上面2个地址都访问同一个页面,为什么缓存会变化?
缓存原理:
缓存数据结构:map,key存储浏览器访问url,上面2个url不一致,缓存肯定变化。
Value:缓存页面数据
存储范围
缓存默认存储在application域当中。
改变缓存Session
固定缓存key
现在时间:<%=new Date() %><br>
<oscache:cache key="hcx">
缓存时间:<%=new Date() %><br>
</oscache:cache>
每隔4秒同步一次
缓存持久化
创建oscache.properties
这个配置文件必须在classpath下面:
cache.memory=false
cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
cache.path=F:\\cache
持久化文件
Oscache整合ssm项目
约定:商品页面访问量特别大,给商品页面缓存。
Items路径下所有请求都缓存。
<filter>
<filter-name>oscache</filter-name>
<filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class>
<init-param>
<param-name>time</param-name>
<param-value>3600</param-value>
</init-param>
<init-param>
<param-name>scope</param-name>
<param-value>application</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>oscache</filter-name>
<url-pattern>/items/*</url-pattern>
</filter-mapping>
Springmvc的freemarker支持
分析:需要jar
Freemarker的jar,context-support.jar
配置freemarker视图支持
<mvc:annotation-driven/>
<!-- 配置freemarker模版文件前缀,模版文件编码 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/jsps/"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<!-- 配置freemarker视图解析后缀,页面显示视图编码 -->
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".ftl"></property>
<property name="contentType" value="text/html;charset=utf-8"></property>
</bean>
编写freemarker页面
ftl.ftl:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>${hello}</h1>
</body>
</html>
代码:
package cn.hcx.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/ftl")
public class FtlController {
@RequestMapping("hello")
public String hello(Model model){
model.addAttribute("hello", "页面静态化,凤姐!");
return "ftl";
}
}
修改itemsList页面
<#assign picPath="http://127.0.0.1:8003/ssmImage19" />
<#assign projectName="springmvc19_day02_02" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body>
<form action="${projectName }/items/deleteByIds.do" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
<td><input type="submit" value="批量删除"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>ID</td>
<td>商品名称</td>
<td>商品图片</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<#list itemsList as item>
<tr>
<td>
<input type="checkbox" name="id" value="${item.id }">
</td>
<td>${item.name }</td>
<td>
<img id='imgSize1ImgSrc' src='${picPath }${item.pic }' height="100" width="100" />
</td>
<td>${item.price }</td>
<td></td>
<td>${item.detail }</td>
<td><a href="${projectName }/items/edit.do?id=${item.id}">修改</a>
<a href="${projectName }/items/deleteByID.do?id=${item.id}">删除</a>
</td>
</tr>
</#list>
</table>
</form>
</body>
</html>