公司要对部分产品使用redis缓存,来提高速度。根据公司现有框架,公司使用ibatis框架,并使用ibator自动化工具生产领域对象。
在使用ibator生产的所有对象,都会有Key Class类。
Key class will contain properties for each field in the primary key of a table. The property names will be generated automatically by Ibator.
Recorld Class类是实际的领域对象,并继承Key Class对象。 所以,我使用Key Class对象作为redis缓存的命名空间,来区分不同的领域对象。在获取Record Class时,使用Key Class作为查询对象。在使用过程中,减少了开发人员的难度,并简化了缓存的实现。
public class UserKey {
private int ID;
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
}
public class User extends UserKey {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [name=" + name + "]";
}
}
public class JedisCacheTemplate {
private Jedis jedis;
private ObjectMapper mapper = new ObjectMapper();
public JedisCacheTemplate() {
this(new Jedis("localhost"));
}
public JedisCacheTemplate(Jedis jedis) {
this.jedis = jedis;
}
//直接通过数据库表映射对象来进行缓存数据
public void setObject(User user) {
Class<? extends User> clazz = user.getClass();
String namespaces = clazz.getSimpleName();
Class<?> superclazz = clazz.getSuperclass();
Method[] methods = superclazz.getDeclaredMethods();
Map<Object, Object> prefixobj = new HashMap<Object, Object>();
try {
for (Method md : methods) {
String mdname = md.getName();
Type retureType = md.getGenericReturnType();
if (!retureType.equals(Void.TYPE)) {
Object value = md.invoke(user, null);
prefixobj.put(mdname, value);
}
}
//根据父类主键来作为redis键的命名空间
String prefix = mapper.writeValueAsString(prefixobj);
String value = mapper.writeValueAsString(user);
jedis.set(namespaces + prefix, value);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
//根据ibator自动生成的主键对象来获取制定的对象
public Object getObject(Object key, Class clazz) {
String namespaces = clazz.getSimpleName();
Map<Object, Object> prefixobj = new HashMap<Object, Object>();
Method[] methods = key.getClass().getDeclaredMethods();
try {
for (Method md : methods) {
String mdname = md.getName();
Type retureType = md.getGenericReturnType();
if (!retureType.equals(Void.TYPE)) {
Object value = md.invoke(key, null);
prefixobj.put(mdname, value);
}
}
//来生成redis键的命名空间
String prefix = mapper.writeValueAsString(prefixobj);
String objectString = jedis.get(namespaces + prefix);
Object obj = mapper.readValue(objectString, clazz);
return obj;
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
JedisCacheTemplate jedisCache = new JedisCacheTemplate();
User user = new User();
user.setID(1);
user.setName("username");
//设置缓存
jedisCache.setObject(user);
//从缓存中获取制定对象
UserKey key = new UserKey();
key.setID(1);
User u = (User) jedisCache.getObject(key, User.class);
System.out.println(u);
}
}