参数管理支持缓存操作

dep_wangsr
RuoYi 5 years ago committed by Limy
parent 675a5f78bb
commit dbefa1311e

@ -132,6 +132,19 @@ public class SysConfigController extends BaseController
return toAjax(configService.deleteConfigByIds(ids));
}
/**
*
*/
@RequiresPermissions("system:config:remove")
@Log(title = "参数管理", businessType = BusinessType.CLEAN)
@GetMapping("/clearCache")
@ResponseBody
public AjaxResult clearCache()
{
configService.clearCache();
return success();
}
/**
*
*/

@ -10,6 +10,7 @@ import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysMenu;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysMenuService;
/**
@ -23,6 +24,9 @@ public class SysIndexController extends BaseController
@Autowired
private ISysMenuService menuService;
@Autowired
private ISysConfigService configService;
// 系统首页
@GetMapping("/index")
public String index(ModelMap mmap)
@ -33,6 +37,8 @@ public class SysIndexController extends BaseController
List<SysMenu> menus = menuService.selectMenusByUser(user);
mmap.put("menus", menus);
mmap.put("user", user);
mmap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme"));
mmap.put("skinName", configService.selectConfigByKey("sys.index.skinName"));
mmap.put("copyrightYear", Global.getCopyrightYear());
mmap.put("demoEnabled", Global.isDemoEnabled());
return "index";

@ -42,6 +42,14 @@
statistics="true">
</cache>
<!-- 系统参数缓存 -->
<cache name="sys-config"
maxEntriesLocalHeap="1000"
eternal="true"
overflowToDisk="true"
statistics="true">
</cache>
<!-- 系统会话缓存 -->
<cache name="shiro-activeSessionCache"
maxElementsInMemory="10000"

@ -1105,7 +1105,9 @@ var table = {
} else if (result.code == web_status.SUCCESS && table.options.type == table_type.bootstrapTreeTable) {
$.modal.msgSuccess(result.msg);
$.treeTable.refresh();
} else if (result.code == web_status.WARNING) {
} else if (result.code == web_status.SUCCESS && table.option.type == undefined) {
$.modal.msgSuccess(result.msg)
} else if (result.code == web_status.WARNING) {
$.modal.alertWarning(result.msg)
} else {
$.modal.alertError(result.msg);

@ -74,7 +74,7 @@
<link th:href="@{/ajax/libs/datapicker/bootstrap-datetimepicker.min.css}" rel="stylesheet"/>
</div>
<div th:fragment="datetimepicker-js">
<script th:src="@{/ajax/libs//datapicker/bootstrap-datetimepicker.min.js}"></script>
<script th:src="@{/ajax/libs/datapicker/bootstrap-datetimepicker.min.js}"></script>
</div>
<!-- ui布局插件 -->

@ -267,10 +267,8 @@ if($.common.isNotEmpty(skin)){
$("body").addClass(skin.split('|')[0]);
$("body").addClass(skin.split('|')[1]);
} else {
var sideTheme = [[${@config.getKey('sys.index.sideTheme')}]];
var skinName = [[${@config.getKey('sys.index.skinName')}]];
$("body").addClass(sideTheme);
$("body").addClass(skinName);
$("body").addClass([[${sideTheme}]]);
$("body").addClass([[${skinName}]]);
}
/* 用户管理-重置密码 */

@ -50,6 +50,9 @@
<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="system:config:export">
<i class="fa fa-download"></i> 导出
</a>
<a class="btn btn-danger" onclick="clearCache()" shiro:hasPermission="system:config:remove">
<i class="fa fa-refresh"></i> 清理缓存
</a>
</div>
<div class="col-sm-12 select-table table-striped">
<table id="bootstrap-table"></table>
@ -131,6 +134,10 @@
};
$.table.init(options);
});
function clearCache() {
$.operate.get(prefix + "/clearCache");
}
</script>
</body>
</html>

@ -57,6 +57,16 @@ public class Constants
*/
public static final String IS_ASC = "isAsc";
/**
* cache name
*/
public static final String SYS_CONFIG_CACHE = "sys-config";
/**
* cache key
*/
public static final String SYS_CONFIG_KEY = "sys_config:";
/**
*
*/

@ -0,0 +1,187 @@
package com.ruoyi.common.utils;
import java.util.Iterator;
import java.util.Set;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ruoyi.common.utils.spring.SpringUtils;
/**
* Cache
*
* @author ruoyi
*/
public class CacheUtils
{
private static Logger logger = LoggerFactory.getLogger(CacheUtils.class);
private static CacheManager cacheManager = SpringUtils.getBean(CacheManager.class);
private static final String SYS_CACHE = "sys-cache";
/**
* SYS_CACHE
*
* @param key
* @return
*/
public static Object get(String key)
{
return get(SYS_CACHE, key);
}
/**
* SYS_CACHE
*
* @param key
* @param defaultValue
* @return
*/
public static Object get(String key, Object defaultValue)
{
Object value = get(key);
return value != null ? value : defaultValue;
}
/**
* SYS_CACHE
*
* @param key
* @return
*/
public static void put(String key, Object value)
{
put(SYS_CACHE, key, value);
}
/**
* SYS_CACHE
*
* @param key
* @return
*/
public static void remove(String key)
{
remove(SYS_CACHE, key);
}
/**
*
*
* @param cacheName
* @param key
* @return
*/
public static Object get(String cacheName, String key)
{
return getCache(cacheName).get(getKey(key));
}
/**
*
*
* @param cacheName
* @param key
* @param defaultValue
* @return
*/
public static Object get(String cacheName, String key, Object defaultValue)
{
Object value = get(cacheName, getKey(key));
return value != null ? value : defaultValue;
}
/**
*
*
* @param cacheName
* @param key
* @param value
*/
public static void put(String cacheName, String key, Object value)
{
getCache(cacheName).put(getKey(key), value);
}
/**
*
*
* @param cacheName
* @param key
*/
public static void remove(String cacheName, String key)
{
getCache(cacheName).remove(getKey(key));
}
/**
*
*
* @param cacheName
*/
public static void removeAll(String cacheName)
{
Cache<String, Object> cache = getCache(cacheName);
Set<String> keys = cache.keys();
for (Iterator<String> it = keys.iterator(); it.hasNext();)
{
cache.remove(it.next());
}
logger.info("清理缓存: {} => {}", cacheName, keys);
}
/**
* key
*
* @param keys
*/
public static void removeByKeys(Set<String> keys)
{
removeByKeys(SYS_CACHE, keys);
}
/**
* key
*
* @param cacheName
* @param keys
*/
public static void removeByKeys(String cacheName, Set<String> keys)
{
for (Iterator<String> it = keys.iterator(); it.hasNext();)
{
remove(it.next());
}
logger.info("清理缓存: {} => {}", cacheName, keys);
}
/**
*
*
* @param key
* @return
*/
private static String getKey(String key)
{
return key;
}
/**
* Cache
*
* @param cacheName
* @return
*/
private static Cache<String, Object> getCache(String cacheName)
{
Cache<String, Object> cache = cacheManager.getCache(cacheName);
if (cache == null)
{
throw new RuntimeException("当前系统中没有定义“" + cacheName + "”这个缓存。");
}
return cache;
}
}

@ -58,6 +58,11 @@ public interface ISysConfigService
*/
public int deleteConfigByIds(String ids);
/**
*
*/
public void clearCache();
/**
*
*

@ -1,10 +1,13 @@
package com.ruoyi.system.service.impl;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
@ -21,6 +24,19 @@ public class SysConfigServiceImpl implements ISysConfigService
@Autowired
private SysConfigMapper configMapper;
/**
*
*/
@PostConstruct
public void init()
{
List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
for (SysConfig config : configsList)
{
CacheUtils.put(getCacheName(), getCacheKey(config.getConfigKey()), config.getConfigValue());
}
}
/**
*
*
@ -44,10 +60,20 @@ public class SysConfigServiceImpl implements ISysConfigService
@Override
public String selectConfigByKey(String configKey)
{
String configValue = Convert.toStr(CacheUtils.get(getCacheName(), getCacheKey(configKey)));
if (StringUtils.isNotEmpty(configValue))
{
return configValue;
}
SysConfig config = new SysConfig();
config.setConfigKey(configKey);
SysConfig retConfig = configMapper.selectConfig(config);
return StringUtils.isNotNull(retConfig) ? retConfig.getConfigValue() : "";
if (StringUtils.isNotNull(retConfig))
{
CacheUtils.put(getCacheName(), getCacheKey(configKey), retConfig.getConfigValue());
return retConfig.getConfigValue();
}
return StringUtils.EMPTY;
}
/**
@ -71,7 +97,12 @@ public class SysConfigServiceImpl implements ISysConfigService
@Override
public int insertConfig(SysConfig config)
{
return configMapper.insertConfig(config);
int row = configMapper.insertConfig(config);
if (row > 0)
{
CacheUtils.put(getCacheName(), getCacheKey(config.getConfigKey()), config.getConfigValue());
}
return row;
}
/**
@ -83,7 +114,12 @@ public class SysConfigServiceImpl implements ISysConfigService
@Override
public int updateConfig(SysConfig config)
{
return configMapper.updateConfig(config);
int row = configMapper.updateConfig(config);
if (row > 0)
{
CacheUtils.put(getCacheName(), getCacheKey(config.getConfigKey()), config.getConfigValue());
}
return row;
}
/**
@ -95,7 +131,21 @@ public class SysConfigServiceImpl implements ISysConfigService
@Override
public int deleteConfigByIds(String ids)
{
return configMapper.deleteConfigByIds(Convert.toStrArray(ids));
int count = configMapper.deleteConfigByIds(Convert.toStrArray(ids));
if (count > 0)
{
CacheUtils.removeAll(getCacheName());
}
return count;
}
/**
*
*/
public void clearCache()
{
CacheUtils.removeAll(getCacheName());
}
/**
@ -115,4 +165,25 @@ public class SysConfigServiceImpl implements ISysConfigService
}
return UserConstants.CONFIG_KEY_UNIQUE;
}
/**
* cache name
*
* @return
*/
private String getCacheName()
{
return Constants.SYS_CONFIG_CACHE;
}
/**
* cache key
*
* @param configKey
* @return key
*/
private String getCacheKey(String configKey)
{
return Constants.SYS_CONFIG_KEY + configKey;
}
}

Loading…
Cancel
Save