!122 初步编写项目缓存逻辑

Merge pull request !122 from 格物方能致知/develop
This commit is contained in:
格物方能致知 2023-09-29 16:06:31 +00:00 committed by Gitee
commit 003fd36e9a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 318 additions and 8 deletions

View File

@ -30,9 +30,9 @@ public class AppConfig {
* @return 默认应用配置对象
*/
public static AppConfig generateDefaultAppConfig() {
AppConfig myData = new AppConfig();
myData.setLanguage(CHINESE);
myData.setTextWrap(false);
AppConfig config = new AppConfig();
config.setLanguage(CHINESE);
config.setTextWrap(false);
List<ShortcutKey> shortcutKeys = new ArrayList<>();
shortcutKeys.add(createShortcutKey("newItem", CTRL_N));
@ -43,8 +43,8 @@ public class AppConfig {
shortcutKeys.add(createShortcutKey("openConfigItem", ALT_S));
shortcutKeys.add(createShortcutKey("pluginManager", ""));
shortcutKeys.add(createShortcutKey("countItem", ""));
myData.setShortcutKey(shortcutKeys);
return myData;
config.setShortcutKey(shortcutKeys);
return config;
}
/**

View File

@ -49,13 +49,13 @@ public class ApplicationManager {
}
/**
* 初始化程序
* 初始化程序(Initializes the application)
*
* @apiNote
* @since 2023/9/20 17:26
*/
public void initializeApp() {
// 设置应用程序主题
// 设置应用程序主题 SetTheApplicationTheme
Application.setUserAgentStylesheet(new PrimerLight().getUserAgentStylesheet());
// 初始化scene
initScene();

View File

@ -0,0 +1,86 @@
package org.jcnc.jnotepad.common.manager;
import org.jcnc.jnotepad.model.entity.Cache;
import java.util.Map;
/**
* 抽象缓存管理类
*
* @author gewuyou
*/
public abstract class AbstractCacheManager {
/**
* 缓存集合
*/
protected Map<String, Cache<?>> caches;
/**
* 获取全局命名空间
*
* @return 全局命名空间
*/
public abstract String getGlobalNamespace();
/**
* 获取缓存集合
*
* @return 缓存集合
*/
public abstract Map<String, Cache<?>> getCaches();
/**
* 设置缓存集合
*
* @param caches 缓存集合
*/
public abstract void setCaches(Map<String, Cache<?>> caches);
/**
* 添加缓存
*
* @param cache 缓存
*/
public void addCache(Cache<?> cache) {
String cacheKey = cache.getCacheKey();
// 如果集合中已存在该缓存则更新读写时间
if (caches.containsKey(cacheKey)) {
cache.setLastReadOrWriteTime(System.currentTimeMillis());
}
caches.put(cacheKey, cache);
}
/**
* 获取缓存类
*
* @param cacheKey 缓存key
* @return 缓存类
*/
public Cache<?> getCache(String cacheKey) {
if (caches.isEmpty()) {
return null;
}
if (caches.containsKey(cacheKey)) {
Cache<?> cache = caches.get(cacheKey);
cache.setLastReadOrWriteTime(System.currentTimeMillis());
return cache;
}
return null;
}
/**
* 获取缓存数据
*
* @param cacheKey 缓存key
* @return 缓存类
*/
public Object getCacheData(String cacheKey) {
Cache<?> cache = getCache(cacheKey);
if (cache == null) {
return null;
}
return cache.getCacheData();
}
}

View File

@ -0,0 +1,38 @@
package org.jcnc.jnotepad.common.manager;
import org.jcnc.jnotepad.model.entity.Cache;
import java.util.Map;
/**
* 应用程序缓存管理类
*
* @author gewuyou
*/
public class ApplicationCacheManager extends AbstractCacheManager {
private static final ApplicationCacheManager INSTANCE = new ApplicationCacheManager();
private ApplicationCacheManager() {
}
public static ApplicationCacheManager getInstance() {
return INSTANCE;
}
@Override
public Map<String, Cache<?>> getCaches() {
return caches;
}
@Override
public void setCaches(Map<String, Cache<?>> caches) {
this.caches = caches;
}
@Override
public String getGlobalNamespace() {
return "jcnc";
}
}

View File

@ -0,0 +1,74 @@
package org.jcnc.jnotepad.controller.cache;
import org.jcnc.jnotepad.exception.AppException;
import org.jcnc.jnotepad.model.entity.Cache;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Map;
import static org.jcnc.jnotepad.common.constants.AppConstants.DEFAULT_PROPERTY;
/**
* 缓存控制器
*
* @author gewuyou
*/
public class CacheController {
private static final CacheController INSTANCE = new CacheController();
private String cacheDir;
private CacheController() {
cacheDir = Paths.get(System.getProperty(DEFAULT_PROPERTY), ".jnotepad", "caches").toString();
loadCaches();
}
public static CacheController getInstance() {
return INSTANCE;
}
private void loadCaches() {
}
private void writeCaches(Map<String, Cache<?>> caches) {
File cacheFileDir = new File(cacheDir);
if (!cacheFileDir.exists()) {
cacheFileDir.mkdirs();
}
// 生成缓存
caches.forEach((key, value) -> {
// 判断当前命名空间对应目录是否创建
File namespaceDir = new File(cacheFileDir, value.getNamespace());
if (!namespaceDir.exists()) {
namespaceDir.mkdirs();
}
// 判断当前组文件是否创建
File groupFile = new File(namespaceDir, value.getGroup());
if (!groupFile.exists()) {
try {
boolean flag = groupFile.createNewFile();
if (!flag) {
throw new AppException("创建文件失败");
}
} catch (IOException e) {
throw new AppException(e);
}
}
});
//
}
/**
* 如果缓存
*/
private void createCachesIfNotExists() {
}
}

View File

@ -0,0 +1,109 @@
package org.jcnc.jnotepad.model.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* 缓存类
*
* @author gewuyou
*/
public class Cache<T> {
/**
* 命名空间
*/
@JsonIgnore
private String namespace;
/**
*
*/
@JsonIgnore
private String group;
/**
* 缓存名称
*/
private String name;
/**
* 缓存数据
*/
private T cacheData;
/**
* 过期时间<br/>如果过期时间为负数则永不过期
*/
private Long expirationTime;
/**
* 上次读或写时间
*/
private Long lastReadOrWriteTime;
public Cache() {
}
public Cache(String namespace, String group, String name, T cacheData, Long expirationTime) {
this.namespace = namespace;
this.group = group;
this.name = name;
this.cacheData = cacheData;
this.expirationTime = expirationTime;
this.lastReadOrWriteTime = System.currentTimeMillis();
}
/**
* 根据缓存key
*
* @return key
*/
public String getCacheKey() {
return this.getNamespace() + "." + this.getGroup() + "." + this.getName();
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public T getCacheData() {
return cacheData;
}
public void setCacheData(T cacheData) {
this.cacheData = cacheData;
}
public Long getExpirationTime() {
return expirationTime;
}
public void setExpirationTime(Long expirationTime) {
this.expirationTime = expirationTime;
}
public Long getLastReadOrWriteTime() {
return lastReadOrWriteTime;
}
public void setLastReadOrWriteTime(Long lastReadOrWriteTime) {
this.lastReadOrWriteTime = lastReadOrWriteTime;
}
}

View File

@ -218,7 +218,10 @@ public class PluginManagementPane extends BorderPane {
// 创建一个按钮
var toggleSwitch = new ToggleSwitch();
// 创建一个图标
ImageView icon = new ImageView(new Image(pluginDescriptor.getIcon() == null ? "plug.png" : pluginDescriptor.getIcon()));
ImageView icon = new ImageView(
new Image(
pluginDescriptor.getIcon() == null ||
pluginDescriptor.getIcon().isEmpty() ? "plug.png" : pluginDescriptor.getIcon()));
// 指定要缩放的固定像素大小
double iconSize = ICON_SIZE;