最近做了个东西,使用的数据是根据元数据进行读取和加工,加工后又基本不变,使用又比较频繁。所以要用到缓存。ehcache是当对象超过配置的最大内存时才部分写入磁盘(非完整),或者配置diskPersistent=true才可以写入磁盘。当初一直以为ehcache在磁盘中读取的缓存是非完整的(重启完就没了)。当初没发现有diskPersistent这个配置,所以就自己先实现了一个简单的。
1、对序列化文件进行读写的类:
2、关键的缓存类:
/**
* 缓存顶层接口
*/
public interface Cache {
CacheKey getCacheKey();
}
[/code]
5、缓存唯一标识接口
public interface Key {
String getKey();
}
6、缓存唯一标识类
/**
* 存放key值 , 重写hashcode 比较方法
*/
public class CacheKey implements Key,Serializable{
private static final long serialVersionUID = 1L;
//目前只存放属性
private String type;
/**
* 比较文件中的缓存是否是同一个
*/
public boolean equals(Object o) {
if (getKey() == null) {
throw new CacheException("cachekey 不能为空.");
}
if (this == o) {
return true;
}
if (!(o instanceof Key))
return false;
Key otherCache = (Key) o;
return getKey().equals(otherCache.getKey());
}
/**
* hashmap是根据hashcode为key获取数据
* 重写这个方法以取得需要的key对应的数据
*/
public int hashCode() {
if (getKey() == null) {
throw new CacheException("cachekey 不能为空.");
}
return getKey().hashCode();
}
/**
* 仓库编号,包编号,类编号
* @return 唯一key
*/
public String getKey() {
return type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
7、需要缓存的对象
public class Content implements Cache,Serializable {
private static final long serialVersionUID = 6963213185510643340L;
//缓存对象的key, 各种类型的缓存
private CacheKey key;
private Object obj;
public String getKey() {
return key.getKey();
}
public void setObject(Object obj) {
this.obj = obj;
}
}
8、最后写一个注册类方便调用
/**
* 注册缓存,将组装完的数据放入内存
* 单服务器用, 多服务器(分布式)时将不可用,文件都不知道在哪个服务器上没有同步。
*/
public class Register {
//整个会话都保存唯一的一个缓存变量
private static MpCache mPcache = MpCache.getInstance();
/**
* 注册解析的内存对象到缓存(全局)
* @throws Exception
*/
public static void regist(Cache c) throws Exception {
CacheKey k = c.getCacheKey();
mPcache.put(k, c);
}
/**
* 根据key值获取缓存对象
* @param key
* @return
* @throws Exception
*/
public static Cache getCache(CacheKey key) throws Exception {
return mPcache.get(key);
}
/**
* 清空缓存对象
*/
public static void clear() {
mPcache.clear();
}
}
话外:分布式缓存也可以用ehcache,或者用mencache。
1、对序列化文件进行读写的类:
/**
* 对缓存序列号
* 读写内存对象到文件
*/
public abstract class FileReader {
/**
* 根据key读取缓存中的数据
* @param key 多种组合
* @return 缓存对象对象
* @throws Exception
*/
public Object read(String path) throws Exception {
Object o = null;
ObjectInputStream in = null;
try {
//判断文件是否存在
File file = new File(path);
if (!file.exists()) {
return o;
}
//读取缓存文件
in = new ObjectInputStream(new FileInputStream(file));
o = in.readObject();
} catch (FileNotFoundException e) {
throw new Exception("read inputstream exception", e);
} catch (IOException e) {
throw new Exception("red inputstream io exception", e);
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
throw new Exception("close inputstream exception", e);
}
}
}
return o;
}
/**
* @param key 将key当初文件名
* @param o 缓存的对象
* @throws Exception IO抛出异常
*/
public void write(String path, Object o) throws Exception {
ObjectOutputStream out = null;
FileOutputStream fout = null;
File file;
try {
file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
fout = new FileOutputStream(file);
out = new ObjectOutputStream(fout);
//清空输出流
out.reset();
//写入对象
out.writeObject(o);
} catch (IOException e) {
throw new Exception("write outputstream exception", e);
} finally {
if (null != out) {
try {
out.close();
} catch (IOException e) {
throw new Exception("close outputstream exception", e);
}
}
if (null != fout) {
try {
fout.close();
} catch (IOException e) {
throw new Exception("close outputstream exception", e);
}
}
}
}
}
2、关键的缓存类:
/**
* 数据内存缓存,同时写入缓存文件,用于重启服务时内存数据消失时读取
* 此处不用ehcache,因为ehcache需要读取配置,不能及时存入缓存文件,只是部分存放。
*/
public class MpCache extends FileReader {
private static MpCache mpCache = new MpCache();
private static Map<CacheKey, Cache> contents;
private static String path;
private static State state;
static {
path = System.getProperty("java.io.tmpdir");
contents = new HashMap<CacheKey, Cache>();
state = State.getInstance();
}
//这是个单例
public static MpCache getInstance() {
return mpCache;
}
/**
* 存入缓存数据,同时更新缓存文件
* @param k
* @param v
* @throws Exception
*/
public synchronized void put(CacheKey k, Cache v) throws Exception {
contents.put(k, v);
write(k, v);
}
/**
* 读取缓存数据
* @param key
* @return
* @throws Exception
*/
public Cache get(CacheKey k) throws Exception {
Cache c = contents.get(k);
if (null == c && !state.hasSync(k)) {
c = read(k);
contents.put(k, c);
}
return c;
}
/**
* 根据key读取缓存中的数据
* @param key 多种组合
* @return 缓存对象对象
* @throws Exception
*/
private Cache read(CacheKey k) throws Exception {
String key = k.getKey();
String p = path.concat(key).concat(".dat");
Cache c = (Cache) super.read(p);
return c;
}
/**
* @param key 将key当初文件名
* @throws Exception IO抛出异常
*/
private void write(CacheKey k, Object o) throws Exception {
String key = k.getKey();
String p = path.concat(key).concat(".dat");
super.write(p, o);
}
public void clear() {
contents.clear();
state.clear();
}
}
3、状态类,对应内存对象是否已经从磁盘中读取过。
[code="java"]
/**
* 文件状态和内存对象是否同步
*/
public class State extends FileReader {
private static State state = new State();
private static Map<String, Boolean> map = new HashMap<String, Boolean>();
/**
*
* @param key
* @return
*/
public boolean hasSync(CacheKey key) {
String k = key.getKey();
Boolean v = map.get(k);
if (null == v) {
//设置已经被同步
map.put(k, true);
//返回未同步
return false;
}
return v;
}
public static State getInstance() {
return state;
}
public void clear() {
map.clear();
}
}
4、存放的对象需要实现的接口
/**
* 缓存顶层接口
*/
public interface Cache {
CacheKey getCacheKey();
}
[/code]
5、缓存唯一标识接口
public interface Key {
String getKey();
}
6、缓存唯一标识类
/**
* 存放key值 , 重写hashcode 比较方法
*/
public class CacheKey implements Key,Serializable{
private static final long serialVersionUID = 1L;
//目前只存放属性
private String type;
/**
* 比较文件中的缓存是否是同一个
*/
public boolean equals(Object o) {
if (getKey() == null) {
throw new CacheException("cachekey 不能为空.");
}
if (this == o) {
return true;
}
if (!(o instanceof Key))
return false;
Key otherCache = (Key) o;
return getKey().equals(otherCache.getKey());
}
/**
* hashmap是根据hashcode为key获取数据
* 重写这个方法以取得需要的key对应的数据
*/
public int hashCode() {
if (getKey() == null) {
throw new CacheException("cachekey 不能为空.");
}
return getKey().hashCode();
}
/**
* 仓库编号,包编号,类编号
* @return 唯一key
*/
public String getKey() {
return type;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
7、需要缓存的对象
public class Content implements Cache,Serializable {
private static final long serialVersionUID = 6963213185510643340L;
//缓存对象的key, 各种类型的缓存
private CacheKey key;
private Object obj;
public String getKey() {
return key.getKey();
}
public void setObject(Object obj) {
this.obj = obj;
}
}
8、最后写一个注册类方便调用
/**
* 注册缓存,将组装完的数据放入内存
* 单服务器用, 多服务器(分布式)时将不可用,文件都不知道在哪个服务器上没有同步。
*/
public class Register {
//整个会话都保存唯一的一个缓存变量
private static MpCache mPcache = MpCache.getInstance();
/**
* 注册解析的内存对象到缓存(全局)
* @throws Exception
*/
public static void regist(Cache c) throws Exception {
CacheKey k = c.getCacheKey();
mPcache.put(k, c);
}
/**
* 根据key值获取缓存对象
* @param key
* @return
* @throws Exception
*/
public static Cache getCache(CacheKey key) throws Exception {
return mPcache.get(key);
}
/**
* 清空缓存对象
*/
public static void clear() {
mPcache.clear();
}
}
话外:分布式缓存也可以用ehcache,或者用mencache。
本文介绍了一种使用元数据进行高效缓存的实现方式,通过自定义缓存类、状态管理和对象序列化,实现了内存数据与磁盘文件的同步,确保了服务重启时内存数据的持久化。此方案适用于数据读取和加工频繁、数据量较大且性能要求高的场景。
6933

被折叠的 条评论
为什么被折叠?



