All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.cosycode.ext.io.cache.AbstractObjCacheHandler Maven / Gradle / Ivy

Go to download

扩展模块, 用于存放一些非常用的工具或模块的扩展类, 例如在poi基础上扩展的excel的导入模块, 模拟按键模块

The newest version!
package com.github.cosycode.ext.io.cache;

import com.github.cosycode.common.ext.hub.Throws;
import com.github.cosycode.common.util.io.FileSystemUtils;
import com.github.cosycode.common.util.io.IoUtils;
import com.github.cosycode.ext.se.util.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NonNull;

import java.io.File;
import java.nio.charset.StandardCharsets;

/**
 * Description : 
 * 

* created in 2022/12/8 *

* * @author CPF * @since 0.2.2 **/ @AllArgsConstructor public abstract class AbstractObjCacheHandler { @Getter private String tag; public abstract void put(T t); public abstract T get(); public boolean validate(T t) { return t.validate(); } public void clear() { put(null); } public static AbstractObjCacheHandler geneMemoryCacheHandler(String tag) { return new AbstractObjCacheHandler(tag) { T value; @Override public void put(T value) { this.value = value; } @Override public T get() { return value; } }; } public static AbstractObjCacheHandler geneFileCacheHandler(String tag, @NonNull String filePath, Class tClass) { // 此处 filePath 为闭包 return new AbstractObjCacheHandler(tag + " => " + filePath) { @Override public void put(T value) { File file = new File(filePath); FileSystemUtils.insureFileExist(file); Throws.runtimeEpt(() -> IoUtils.writeFile(file.getPath(), JsonUtils.toJson(value).getBytes(StandardCharsets.UTF_8))); } @Override public T get() { File file = new File(filePath); if (file.exists()) { String response = Throws.runtimeEpt(() -> IoUtils.readFile(file)); return JsonUtils.fromJson(response, tClass); } return null; } @Override public void clear() { File file = new File(filePath); file.deleteOnExit(); } }; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy