mirror of
https://github.com/yangzongzhuan/RuoYi-Cloud.git
synced 2025-09-02 19:04:42 +00:00
参数管理支持缓存操作
This commit is contained in:
@@ -77,6 +77,12 @@
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-swagger</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- RuoYi Common Redis-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@@ -48,7 +48,7 @@ public class SysConfigController extends BaseController
|
||||
List<SysConfig> list = configService.selectConfigList(config);
|
||||
return getDataTable(list);
|
||||
}
|
||||
|
||||
|
||||
@Log(title = "参数管理", businessType = BusinessType.EXPORT)
|
||||
@PreAuthorize("@ss.hasPermi('system:config:export')")
|
||||
@PostMapping("/export")
|
||||
@@ -120,4 +120,16 @@ public class SysConfigController extends BaseController
|
||||
{
|
||||
return toAjax(configService.deleteConfigByIds(configIds));
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空缓存
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('system:config:remove')")
|
||||
@Log(title = "参数管理", businessType = BusinessType.CLEAN)
|
||||
@DeleteMapping("/clearCache")
|
||||
public AjaxResult clearCache()
|
||||
{
|
||||
configService.clearCache();
|
||||
return AjaxResult.success();
|
||||
}
|
||||
}
|
||||
|
@@ -51,14 +51,6 @@ public interface ISysConfigService
|
||||
*/
|
||||
public int updateConfig(SysConfig config);
|
||||
|
||||
/**
|
||||
* 删除参数配置信息
|
||||
*
|
||||
* @param configId 参数ID
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteConfigById(Long configId);
|
||||
|
||||
/**
|
||||
* 批量删除参数信息
|
||||
*
|
||||
@@ -67,6 +59,11 @@ public interface ISysConfigService
|
||||
*/
|
||||
public int deleteConfigByIds(Long[] configIds);
|
||||
|
||||
/**
|
||||
* 清空缓存数据
|
||||
*/
|
||||
public void clearCache();
|
||||
|
||||
/**
|
||||
* 校验参数键名是否唯一
|
||||
*
|
||||
|
@@ -1,12 +1,15 @@
|
||||
package com.ruoyi.system.service.impl;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.ruoyi.common.core.constant.Constants;
|
||||
import com.ruoyi.common.core.constant.UserConstants;
|
||||
import com.ruoyi.common.core.text.Convert;
|
||||
import com.ruoyi.common.core.utils.StringUtils;
|
||||
import com.ruoyi.common.redis.service.RedisService;
|
||||
import com.ruoyi.system.domain.SysConfig;
|
||||
import com.ruoyi.system.mapper.SysConfigMapper;
|
||||
import com.ruoyi.system.service.ISysConfigService;
|
||||
@@ -22,6 +25,22 @@ public class SysConfigServiceImpl implements ISysConfigService
|
||||
@Autowired
|
||||
private SysConfigMapper configMapper;
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
/**
|
||||
* 项目启动时,初始化参数到缓存
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init()
|
||||
{
|
||||
List<SysConfig> configsList = configMapper.selectConfigList(new SysConfig());
|
||||
for (SysConfig config : configsList)
|
||||
{
|
||||
redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询参数配置信息
|
||||
*
|
||||
@@ -45,10 +64,20 @@ public class SysConfigServiceImpl implements ISysConfigService
|
||||
@Override
|
||||
public String selectConfigByKey(String configKey)
|
||||
{
|
||||
String configValue = Convert.toStr(redisService.getCacheObject(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))
|
||||
{
|
||||
redisService.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
|
||||
return retConfig.getConfigValue();
|
||||
}
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,7 +101,12 @@ public class SysConfigServiceImpl implements ISysConfigService
|
||||
@Override
|
||||
public int insertConfig(SysConfig config)
|
||||
{
|
||||
return configMapper.insertConfig(config);
|
||||
int row = configMapper.insertConfig(config);
|
||||
if (row > 0)
|
||||
{
|
||||
redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,19 +118,12 @@ public class SysConfigServiceImpl implements ISysConfigService
|
||||
@Override
|
||||
public int updateConfig(SysConfig config)
|
||||
{
|
||||
return configMapper.updateConfig(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除参数配置信息
|
||||
*
|
||||
* @param configId 参数ID
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
public int deleteConfigById(Long configId)
|
||||
{
|
||||
return configMapper.deleteConfigById(configId);
|
||||
int row = configMapper.updateConfig(config);
|
||||
if (row > 0)
|
||||
{
|
||||
redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,7 +135,22 @@ public class SysConfigServiceImpl implements ISysConfigService
|
||||
@Override
|
||||
public int deleteConfigByIds(Long[] configIds)
|
||||
{
|
||||
return configMapper.deleteConfigByIds(configIds);
|
||||
int count = configMapper.deleteConfigByIds(configIds);
|
||||
if (count > 0)
|
||||
{
|
||||
Collection<String> keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*");
|
||||
redisService.deleteObject(keys);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空缓存数据
|
||||
*/
|
||||
public void clearCache()
|
||||
{
|
||||
Collection<String> keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*");
|
||||
redisService.deleteObject(keys);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,4 +170,15 @@ public class SysConfigServiceImpl implements ISysConfigService
|
||||
}
|
||||
return UserConstants.UNIQUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置cache key
|
||||
*
|
||||
* @param configKey 参数键
|
||||
* @return 缓存键key
|
||||
*/
|
||||
private String getCacheKey(String configKey)
|
||||
{
|
||||
return Constants.SYS_CONFIG_KEY + configKey;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user