配置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 操作比较简单,后续会提供这方便的工具类