web开发中对缓存的使用
很久没有发表随笔了,最近工作不是太忙,抽点时间 给大家谈谈缓存吧 ;
在我从事web开发的几年实践中 接触了缓存技术 也是比较多的,在最初的 项目当中 我们用到 hibernate 的 一二级缓存,在到后期的 nosql产品 redis,memcache,还有互联网中常用的 页面缓存 oscache
缓存使用场景介绍
一级缓存是对方法层面的缓存 只是类级别的 缓存用处不大 只是在配置当中进行开启
二级缓存在SqlFactory 层次的缓存 这样的缓存配置 网上还是很多的 http://www.open-open.com/lib/view/open1413527015465.html
下面就说说 大家在开发中常用nosql 缓存了
memcache 我在项目中的使用 列子如下 maven为列
<!-- memcached -->
<dependency>
<groupId>com.google.code.maven-play-plugin.spy</groupId>
<artifactId>memcached</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.danga</groupId>
<artifactId>memcached</artifactId>
<version>2.0.1</version>
</dependency>
spring AOP下的缓存配置
<!-- Memcached 配置 -->
<!-- Memcached 配置 -->
<bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">
<constructor-arg>
<value>sockIOPool</value>
</constructor-arg>
</bean>
<!-- Memcached连接池 -->
<bean id="sockIOPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<value>sockIOPool</value>
</constructor-arg>
<property name="servers">
<list>
<value>192.168.25.129:11211</value>
</list>
</property>
<property name="weights">
<list>
<value>1</value>
</list>
</property>
</bean>
<!-- 切面对象 -->
<bean id="cacheInterceptor" class="cn.itcast.common.web.aop.CacheInterceptor">
<property name="expiry" value="4200000"/>
</bean>
<!-- Spring Aop 配置 get* 配置环绕 -->
<aop:config>
<!-- 面 -->
<aop:aspect ref="cacheInterceptor">
<!-- 点 -->
<aop:around method="doAround" pointcut="execution(* cn.itcast.core.service.*.*.get*(..))"/>
<!-- 变更 -->
<aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.update*(..))"/>
<aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.add*(..))"/>
<aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.delete*(..))"/>
</aop:aspect>
</aop:config>
拦截器
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.springframework.beans.factory.annotation.Autowired;
import cn.itcast.common.encode.MemCachedUtil;
import com.danga.MemCached.MemCachedClient;
/**
* 缓存Memcached中数据的切面对象
* aroud
* after
* @author lx
*
*/
public class CacheInterceptor {
@Autowired
private MemCachedClient memCachedClient;
//时间 缓存时间
public static final int TIMEOUT = 360000;//秒
private int expiry = TIMEOUT;
//配置环绕方法
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
//去Memcached中看看有没有我们的数据 包名+ 类名 + 方法名 + 参数(多个)
String cacheKey = getCacheKey(pjp);
System.out.println(cacheKey);
//如果Memcached 连接不上呢
if(memCachedClient.stats().isEmpty()){
System.out.println("Memcached服务器可能不存在或是连接不上");
return pjp.proceed();
}
//返回值
if(null == memCachedClient.get(cacheKey)){
//回Service
Object proceed = pjp.proceed();
//先放到Memcached中一份
memCachedClient.set(cacheKey, proceed,expiry);
}
return memCachedClient.get(cacheKey);
}
//后置由于数据库数据变更 清理get*
public void doAfter(JoinPoint jp){
//包名+ 类名 + 方法名 + 参数(多个) 生成Key
//包名+ 类名
String packageName = jp.getTarget().getClass().getName();
//包名+ 类名 开始的 都清理
List<String> list = MemCachedUtil.getAllKeys(memCachedClient);
//遍历
for(String entry : list){
if(entry.startsWith(packageName)){
memCachedClient.delete(entry);
}
}
}
//包名+ 类名 + 方法名 + 参数(多个) 生成Key
public String getCacheKey(ProceedingJoinPoint pjp){
//StringBuiter
StringBuilder key = new StringBuilder();
//包名+ 类名 cn.itcast.core.serice.product.ProductServiceImpl.productList
String packageName = pjp.getTarget().getClass().getName();
key.append(packageName);
// 方法名
String methodName = pjp.getSignature().getName();
key.append(".").append(methodName);
//参数(多个)
Object[] args = pjp.getArgs();
ObjectMapper om = new ObjectMapper();
om.setSerializationInclusion(Inclusion.NON_NULL);
for(Object arg : args){
//流
StringWriter str = new StringWriter();
//对象转Json 写的过程 Json是字符串流
try {
om.writeValue(str, arg);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//参数
key.append(".").append(str);
}
return key.toString();
}
public void setExpiry(int expiry) {
this.expiry = expiry;
}
}
这就是我的 Memcached 缓存使用 这样的话 我们在开发中 只需要 去写我们代码 遵循我们开始的定义规范 不用去管我们缓存了
web开发中对缓存的使用的更多相关文章
- 在 Web 开发中,img 标签用来呈现图片,而且一般来说,浏览器是会对这些图片进行缓存的。
在 Web 开发中,img 标签用来呈现图片,而且一般来说,浏览器是会对这些图片进行缓存的. 比如访问百度,我们可以发现,图片.脚本这种都是从缓存(内存缓存/磁盘缓存)中加载的,而不是再去访问一次百度 ...
- 【初码干货】使用阿里云对Web开发中的资源文件进行CDN加速的深入研究和实践
提示:阅读本文需提前了解的相关知识 1.阿里云(https://www.aliyun.com) 2.阿里云CDN(https://www.aliyun.com/product/cdn) 3.阿里云OS ...
- Redis在WEB开发中的应用与实践
Redis在WEB开发中的应用与实践 一.Redis概述: Redis是一个功能强大.性能高效的开源数据结构服务器,Redis最典型的应用是NoSQL.但事实上Redis除了作为NoSQL数据库使用之 ...
- [译]如何在Web开发中使用Python
[译]如何在Web开发中使用Python 原文:HOWTO Use Python in the Web 摘要 这篇文档展示了Python如何融入到web中.它介绍了几种Python结合web服务器的方 ...
- Web开发中的18个关键性错误
前几年,我有机会能参与一些有趣的项目,并且独立完成开发.升级.重构以及新功能的开发等工作. 本文总结了一些PHP程序员在Web开发中经常 忽略的关键错误,尤其是在处理中大型的项目上问题更为突出.典型的 ...
- jave web 开发中 遇到修改不生效的几部方法 总结
在web开发中经常遇到修改文件之后没有正确被加载的情况 1.重启服务器 2.结束多余 javaw.exe 进程 3.删除web容器下的缓存文件 work 4. ctrl+f5 强制刷新缓存,如果是 ...
- Web 开发中很实用的10个效果【附源码下载】
在工作中,我们可能会用到各种交互效果.而这些效果在平常翻看文章的时候碰到很多,但是一时半会又想不起来在哪,所以养成知识整理的习惯是很有必要的.这篇文章给大家推荐10个在 Web 开发中很有用的效果,记 ...
- WEB开发中的字符集和编码
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- Web 开发中很实用的10个效果
在工作中,我们可能会用到各种交互效果.而这些效果在平常翻看文章的时候碰到很多,但是一时半会又想不起来在哪,所以养成知识整理的习惯是很有必要的.这篇文章给大家推荐10个在 Web 开发中很有用的效果,记 ...
随机推荐
- 读论文系列:Object Detection SPP-net
本文为您解读SPP-net: Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition Motivat ...
- [HAOI2009]毛毛虫
题目描述 对于一棵树,我们可以将某条链和与该链相连的边抽出来,看上去就象成一个毛毛虫,点数越多,毛毛虫就越大.例如下图左边的树(图 1 )抽出一部分就变成了右边的一个毛毛虫了(图 2 ). 输入输出格 ...
- IDEA的Maven依赖如何引入到External Libraries中
现象 在Apollo项目中,遇到了一个问题.当在Module的pom.xml中引入依赖: <dependency> <groupId>com.ctrip.framework.a ...
- Discuz的安装与使用
Discuz的安装与使用 一.Discuz的安装 由于本机已经安装好XAMPP集成工具,后续Discuz访问数据库以及服务器等都是基于XAMPP环境.在主机localhost根目录下新建bbs文件夹. ...
- Linux中的 awk查找日志中的相关记录
假设要在 api.log.201707201830 文件中,(此文件的多个字段数据以不可见字符^A(键盘上按下Ctrl+V+A)分隔),要输出第70个字段: awk -F '^A' '{print $ ...
- 关于Apache配置虚拟主机后在局域网中让其他电脑访问
#-----------adxssp------------# NameVirtualHost *:80 <VirtualHost *:80> ServerName www.b.com D ...
- PHP读取大文本文件并处理数据的思路
//处理文件 $file = fopen($filename, "r") or exit("Unable to open file!"); $total_lin ...
- php获取今日开始时间戳和结束时间戳
1.php获取今日开始时间戳和结束时间戳 $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));$endToday=mktime(0,0,0 ...
- php正则判断字符串是否含有中文
<?php $str = '若你安好便是晴天'; if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $str)>0) { echo '全是中文'; } ...
- windows下apache服务器开启压缩和网页缓存
找到配置文件:http.conf apache开启压缩 一.开启配置,去除下面代码前面的#号LoadModule deflate_module modules/mod_deflate.soLoadMo ...