Spring AOP 整合Redis 缓存
2015-11-20 16:13
501 查看
配置aop 扫描 :
序列化和反序列化 对象的抽象类:
package com.ikejie.util;
import java.io.Closeable;
import org.apache.log4j.Logger;
public abstract class SerializeTranscoder {
protected static Logger logger = Logger.getLogger(SerializeTranscoder.class);
public abstract byte[] serialize(Object value); public abstract Object deserialize(byte[] in); public void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception e) { logger.info("Unable to close " + closeable, e); } } }
}
序列化和反序列化对象的实现类:
package com.ikejie.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ObjectsTranscoder extends SerializeTranscoder {
@Override public byte[] serialize(Object value) { if (value == null) { throw new NullPointerException("Can't serialize null"); } byte[] result = null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); M m = (M) value; os.writeObject(m); os.close(); bos.close(); result = bos.toByteArray(); } catch (IOException e) { throw new IllegalArgumentException("Non-serializable object", e); } finally { close(os); close(bos); } return result; } @Override public M deserialize(byte[] in) { M result = null; ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if (in != null) { bis = new ByteArrayInputStream(in); is = new ObjectInputStream(bis); result = (M) is.readObject(); is.close(); bis.close(); } } catch (IOException e) { // LoggerUtils.error(logger, String.format("Caught IOException decoding %d bytes of data", // in == null ? 0 : in.length) + e); } catch (ClassNotFoundException e) { // LoggerUtils.error(logger, String.format("Caught CNFE decoding %d bytes of data", // in == null ? 0 : in.length) + e); } finally { close(is); close(bis); } return result; }
}
序列化和反序列化集合的实现类
package com.ikejie.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class ListTranscoder extends SerializeTranscoder {
@Override public byte[] serialize(Object value) { if (value == null) throw new NullPointerException("Can't serialize null"); List<M> values = (List<M>) value; byte[] results = null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); for (M m : values) { os.writeObject(m); } // os.writeObject(null); os.close(); bos.close(); results = bos.toByteArray(); } catch (IOException e) { throw new IllegalArgumentException("Non-serializable object", e); } finally { close(os); close(bos); } return results; } @Override public Object deserialize(byte[] in) { List<M> list = new ArrayList<>(); ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if (in != null) { bis = new ByteArrayInputStream(in); is = new ObjectInputStream(bis); while (true) { M m = (M)is.readObject(); if (m == null) { break; } list.add(m); } is.close(); bis.close(); } } catch (IOException e) { //LoggerUtils.error(logger, String.format("Caught IOException decoding %d bytes of data", // in == null ? 0 : in.length) + e); } catch (ClassNotFoundException e) { // LoggerUtils.error(logger, String.format("Caught CNFE decoding %d bytes of data", // in == null ? 0 : in.length) + e); } finally { close(is); close(bis); } return list; }
}
AOP代理实现缓存代码:
package com.ikejie.util;
import java.util.List;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import com.ikejie.po.User;
@Component
@Aspect
public class UserAopHelper {
@Pointcut("execution(* com.ikejie.service.UserService.selectUserAll())") public void userPoint(){} @Pointcut("execution(* com.ikejie.service.UserService.saveUser(User))") public void userAddPoint(){} @Around("userPoint()") public List<User> findUserList(ProceedingJoinPoint joinpoint){ List<User> userList=null; Jedis jedis = new Jedis("localhost"); System.out.println("Connection to server sucessfully"); byte[] in = jedis.get("userlist".getBytes()); ListTranscoder<User> listTranscoder = new ListTranscoder<>(); userList = (List<User>)listTranscoder.deserialize(in); if(userList==null||userList.size()==0){ try { userList=(List<User>) joinpoint.proceed(); System.out.println("get db from mysql"); byte[] result1 = listTranscoder.serialize(userList); jedis.set("userlist".getBytes(), result1); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } } return userList; } @After("userAddPoint()") public void saveUser(User user){ Jedis jedis = new Jedis("localhost"); System.out.println("Connection to server sucessfully"); }
}
做为两个请求的对比 :
Redis :返回数据的时间是 11ms 甚至更少 最低5ms
而mysql返回数据的时间是 35ms
同时里面用到jedis 操作redis 的简单应用
这里的jedis 操作比较简单,后续会提供这方便的工具类
相关文章推荐
- redis安装问题小结
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Redis偶发连接失败案例实战记录
- Redis中实现查找某个值的范围
- Redis和Memcached的区别详解
- 分割超大Redis数据库例子
- Redis总结笔记(一):安装和常用命令
- Redis sort 排序命令详解
- 用Redis实现微博关注关系
- redis中修改配置文件中的端口号 密码方法
- 在Ruby on Rails上使用Redis Store的方法
- Enterprise Library for .NET Framework 2.0缓存使用实例
- Redis和Memcache的区别总结
- PowerShell中编程清空IE缓存方法
- PowerShell中使用.NET将程序集加入全局程序集缓存
- yui3的AOP(面向切面编程)和OOP(面向对象编程)
- C#中缓存的基本用法总结
- 在Node.js应用中使用Redis的方法简介