@Override public boolean setCache(CacheSelector cacheSelector, Object cacheValue) throws Exception { long timeout = cacheSelector.getTimeout(); String filePath = this.getRealFilePath(cacheSelector.getCacheKey()); boolean isSave = FileOperate.saveObjectToFile(filePath, new FileCacheObject(timeout, cacheValue)); if (isSave && timeout > 0) { FileCacheMonitor.getInstance().setFileCacheTime(filePath, timeout); } return isSave; }
@Override public int updateCache(CacheSelector cacheSelector) throws Exception { // 如果只有这一层则在该层中更新 if (null == this.getNextCache()) { return this.setCache(cacheSelector, cacheSelector.getSelectorValue()) ? 1 : 0; } else { return 0; } }
@Override public int deleteCache(CacheSelector cacheSelector) throws Exception { String cacheId = cacheSelector.getCacheKey(); String filePath = this.getRealFilePath(cacheId); File f = new File(filePath); // 文件缓存不存在则直接返回已删除 if (!f.exists()) { return 1; } else { return f.delete() ? 1 : 0; } }
@Override public Object getCache(CacheSelector cacheSelector) throws Exception { File file = new File(this.getRealFilePath(cacheSelector.getCacheKey())); if (!file.exists()) { return null; } Object fileObj = FileOperate.getFileObject(file); // 文件中保存的对象 Object fileStorageObject = null; // 文件中保存的对象 if (null != fileObj && fileObj instanceof FileCacheObject) { FileCacheObject fcObject = (FileCacheObject) fileObj; // 缓存未过期才能获取对象 if (!fcObject.isCacheExpire()) { fileStorageObject = fcObject.getStorageObject(); } } // 如果不存在或已过期则删除 if (null == fileStorageObject) { file.delete(); } return fileStorageObject; }