@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 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; }