类 SystemParamCache
java.lang.Object
net.sohelp.boot.admin.core.cache.SystemParamCache
- 所有已实现的接口:
ISohelpCache
系统常量缓存(TTL 2小时)
支持 MEMORY / REDIS / NONE 三种缓存模式,由 SohelpCacheManager 按配置创建。
- NONE 模式:每次 get 直接查询
pb_system_param表,不使用缓存。 - MEMORY/REDIS 模式:缓存未命中时触发全量 reload,之后按 key 读取。
系统常量参数通常包括全局配置项,例如系统名称、分页大小、功能开关等, 供所有用户共享使用。
- 从以下版本开始:
- 2023/3/28
- 作者:
- AaronFung
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明获取缓存的中文描述信息。获取缓存名称。getBoolean(String name) 获取 Boolean 类型的系统参数值。获取 Date 类型的系统参数值。getDateTime(String name) 获取系统参数值并格式化为日期时间字符串(yyyy-MM-dd hh:mm:ss)。获取 Double 类型的系统参数值。获取 Float 类型的系统参数值。获取 Integer 类型的系统参数值。获取 List 类型的系统参数值。获取 Long 类型的系统参数值。获取 Map 类型的系统参数值。获取 Object 类型的系统参数值。获取 String 类型的系统参数值。voidinit()初始化方法,在 Bean 创建完成后由容器自动调用。void首次初始化参数(若已存在则跳过)。voidreload()全量重新加载:清空缓存并从数据库读取所有系统参数。void保存系统参数:如果不存在则初始化,如果已存在则更新数据值。void仅更新已存在的参数值。从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait从接口继承的方法 net.sohelp.boot.admin.core.cache.ISohelpCache
clear, get, getProvider, put, remove
-
构造器详细资料
-
SystemParamCache
public SystemParamCache()
-
-
方法详细资料
-
init
@PostConstruct public void init()初始化方法,在 Bean 创建完成后由容器自动调用。 通过缓存管理器创建名为 "system_param"、TTL 为 2 小时的缓存提供者。 -
cacheName
获取缓存名称。- 指定者:
cacheName在接口中ISohelpCache- 返回:
- 缓存名称字符串 "system_param"
-
cacheDescription
获取缓存的中文描述信息。- 指定者:
cacheDescription在接口中ISohelpCache- 返回:
- 缓存描述字符串 "系统常量缓存"
-
reload
@Comment("\u521d\u59cb\u5316\u7f13\u5b58") public void reload()全量重新加载:清空缓存并从数据库读取所有系统参数。NONE 模式下不需要 reload,因为每次 get 会直接查库,所以直接返回。 MEMORY/REDIS 模式下先清空缓存,再将数据库中所有参数逐条写入缓存。
- 指定者:
reload在接口中ISohelpCache
-
initial
@Comment("\u4ec5\u4ec5\u4f5c\u7b2c\u4e00\u6b21\u521d\u59cb\u5316\u6570\u636e\uff0c\u5982\u679c\u6570\u636e\u5df2\u7ecf\u5b58\u5728\u5219\u4e0d\u4f1a\u8fdb\u884c\u521d\u5316") public void initial(@Comment("\u53c2\u6570\u540d\u79f0") String name, @Comment("\u53c2\u6570\u503c") Object value, @Comment("\u53c2\u6570\u63cf\u8ff0") String description) throws SQLException 首次初始化参数(若已存在则跳过)。在缓存模式下,先检查缓存中是否存在该参数名(未命中则触发 reload), 如果仍不存在则执行数据库保存并写入缓存。
在 NONE 模式下,直接查询数据库判断参数是否存在,不存在则保存。
- 参数:
name- 参数名称,作为缓存和数据库的唯一标识value- 参数值description- 参数描述,用于在管理界面展示该参数的用途说明- 抛出:
SQLException- 当数据库操作异常时抛出
-
save
@Comment("\u5982\u679c\u4e0d\u5b58\u5728\u5219\u521d\u59cb\u5316\u53c2\u6570\uff0c\u5982\u679c\u5b58\u5728\u5219\u66f4\u65b0\u6570\u636e\u503c") public void save(@Comment("\u53c2\u6570\u540d\u79f0") String name, @Comment("\u53c2\u6570\u503c") Object value, @Comment("\u53c2\u6570\u8bf4\u660e") String description) throws SQLException 保存系统参数:如果不存在则初始化,如果已存在则更新数据值。在缓存模式下,先通过缓存判断参数是否存在(未命中则触发 reload), 然后根据存在与否执行更新或初始化操作。
在 NONE 模式下,直接查询数据库判断后执行相应操作。
- 参数:
name- 参数名称value- 参数值,不能为null或空description- 参数描述- 抛出:
SQLException- 当数据库操作异常时抛出
-
update
@Comment("\u4ec5\u4ec5\u66f4\u65b0\u5df2\u5b58\u5728\u7684\u53c2\u6570\u503c") public void update(@Comment("\u53c2\u6570\u540d\u79f0") String name, @Comment("\u53c2\u6570\u503c") Object value) throws SQLException 仅更新已存在的参数值。先更新数据库中的参数值,如果缓存已启用则同步更新缓存。
- 参数:
name- 参数名称value- 新的参数值- 抛出:
SQLException- 当数据库操作异常时抛出
-
getObject
@Comment("\u83b7\u53d6Object\u7c7b\u578b\u7684\u53c2\u6570\u503c") public Object getObject(@Comment("\u53c2\u6570\u540d\u79f0") String name) 获取 Object 类型的系统参数值。NONE 模式下直接查询数据库。缓存模式下先从缓存中获取, 如果未命中则触发全量 reload 后再次获取。
- 参数:
name- 参数名称- 返回:
- 参数值对象;如果不存在则返回
null
-
getString
@Comment("\u83b7\u53d6String\u7c7b\u578b\u7684\u53c2\u6570\u503c") public String getString(@Comment("\u53c2\u6570\u540d\u79f0") String name) 获取 String 类型的系统参数值。- 参数:
name- 参数名称- 返回:
- 参数的字符串值;如果不存在则返回空字符串 ""
-
getInt
@Comment("\u83b7\u53d6Int\u7c7b\u578b\u7684\u53c2\u6570\u503c") public Integer getInt(@Comment("\u53c2\u6570\u540d\u79f0") String name) 获取 Integer 类型的系统参数值。- 参数:
name- 参数名称- 返回:
- 参数的整数值;如果不存在或转换失败则返回默认值 0
-
getDouble
@Comment("\u83b7\u53d6Double\u7c7b\u578b\u7684\u53c2\u6570\u503c") public Double getDouble(@Comment("\u53c2\u6570\u540d\u79f0") String name) 获取 Double 类型的系统参数值。- 参数:
name- 参数名称- 返回:
- 参数的双精度浮点值;如果不存在或转换失败则返回默认值 0.0
-
getLong
@Comment("\u83b7\u53d6Long\u7c7b\u578b\u7684\u53c2\u6570\u503c") public Long getLong(@Comment("\u53c2\u6570\u540d\u79f0") String name) 获取 Long 类型的系统参数值。- 参数:
name- 参数名称- 返回:
- 参数的长整型值;如果不存在或转换失败则返回默认值 0L
-
getFloat
@Comment("\u83b7\u53d6Float\u7c7b\u578b\u7684\u53c2\u6570\u503c") public Float getFloat(@Comment("\u53c2\u6570\u540d\u79f0") String name) 获取 Float 类型的系统参数值。- 参数:
name- 参数名称- 返回:
- 参数的单精度浮点值;如果不存在或转换失败则返回默认值 0.0f
-
getDate
@Comment("\u83b7\u53d6Date\u7c7b\u578b\u7684\u53c2\u6570\u503c") public Date getDate(@Comment("\u53c2\u6570\u540d\u79f0") String name) 获取 Date 类型的系统参数值。- 参数:
name- 参数名称- 返回:
- 参数的日期值;如果不存在或转换失败则返回
null
-
getDateTime
@Comment("\u83b7\u53d6\u65e5\u671f\u65f6\u95f4") public String getDateTime(@Comment("\u53c2\u6570\u540d\u79f0") String name) 获取系统参数值并格式化为日期时间字符串(yyyy-MM-dd hh:mm:ss)。- 参数:
name- 参数名称- 返回:
- 格式为 "yyyy-MM-dd hh:mm:ss" 的日期时间字符串;如果值为
null则返回null
-
getMap
@Comment("\u83b7\u53d6Map\u7c7b\u578b\u7684\u53c2\u6570\u503c") public Map<String,Object> getMap(@Comment("\u53c2\u6570\u540d\u79f0") String name) 获取 Map 类型的系统参数值。如果首次获取为
null,会尝试重新加载一次缓存后再获取。 支持参数值直接为 Map 对象或 JSON 字符串两种存储形式。- 参数:
name- 参数名称- 返回:
- 解析后的
Map<String, Object>对象;如果不存在或为空则返回空的 HashMap
-
getList
@Comment("\u83b7\u53d6List\u7c7b\u578b\u7684\u53c2\u6570\u503c") public List getList(@Comment("\u53c2\u6570\u540d\u79f0") String name) 获取 List 类型的系统参数值。支持参数值直接为 List 对象或 JSON 字符串两种存储形式。
- 参数:
name- 参数名称- 返回:
- 解析后的 List 对象;如果不存在或为空则返回空的 ArrayList
-
getBoolean
@Comment("\u83b7\u53d6boolean\u7c7b\u578b\u7684\u53c2\u6570\u503c") public Boolean getBoolean(@Comment("\u53c2\u6570\u540d\u79f0") String name) 获取 Boolean 类型的系统参数值。- 参数:
name- 参数名称- 返回:
- 参数的布尔值;如果不存在或转换失败则返回
null
-