mirror of https://gitee.com/stylefeng/roses
【7.0.4】更新缓存的拓展
parent
a969fb6cb1
commit
dedc085efb
|
@ -45,7 +45,7 @@ public interface ScannerConstants {
|
|||
/**
|
||||
* 资源前缀标识
|
||||
*/
|
||||
String RESOURCE_CACHE_KEY = "GUNS_RESOURCE_CACHES";
|
||||
String RESOURCE_CACHE_KEY = "GUNS_RESOURCE_CACHES:";
|
||||
|
||||
/**
|
||||
* 资源汇报的监听器的顺序
|
||||
|
|
|
@ -32,6 +32,21 @@
|
|||
<version>7.0.4</version>
|
||||
</dependency>
|
||||
|
||||
<!--缓存api-->
|
||||
<!--缓存资源扫描的资源-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>cache-sdk-memory</artifactId>
|
||||
<version>7.0.4</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>cache-sdk-redis</artifactId>
|
||||
<version>7.0.4</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!--参数校验模块-->
|
||||
<!--用在控制器,参数校验-->
|
||||
<dependency>
|
||||
|
|
|
@ -24,50 +24,35 @@
|
|||
*/
|
||||
package cn.stylefeng.roses.kernel.system.modular.resource.cache;
|
||||
|
||||
import cn.hutool.cache.impl.TimedCache;
|
||||
import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator;
|
||||
import cn.stylefeng.roses.kernel.scanner.api.constants.ScannerConstants;
|
||||
import cn.stylefeng.roses.kernel.scanner.api.pojo.resource.ResourceDefinition;
|
||||
import cn.stylefeng.roses.kernel.system.modular.resource.entity.SysResource;
|
||||
import cn.stylefeng.roses.kernel.system.modular.resource.factory.ResourceFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* 资源缓存
|
||||
* 基于内存的资源缓存
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2019-09-10-17:29
|
||||
* @date 2021/5/17 16:05
|
||||
*/
|
||||
@Component
|
||||
public class ResourceCache {
|
||||
|
||||
private final List<ResourceDefinition> resourceDefinitions = new ArrayList<>();
|
||||
public class MemoryResourceCache extends AbstractMemoryCacheOperator<Map<String, ResourceDefinition>> {
|
||||
|
||||
/**
|
||||
* 保存资源存储到缓存
|
||||
* TimedCache的key是appCode,value的key是资源url,value是ResourceDefinition
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/11/24 20:06
|
||||
* @date 2021/5/17 16:06
|
||||
*/
|
||||
public void saveResourcesToCache(List<SysResource> sysResources) {
|
||||
if (sysResources == null || sysResources.size() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (SysResource sysResource : sysResources) {
|
||||
ResourceDefinition resourceDefinition = ResourceFactory.createResourceDefinition(sysResource);
|
||||
resourceDefinitions.add(resourceDefinition);
|
||||
}
|
||||
public MemoryResourceCache(TimedCache<String, Map<String, ResourceDefinition>> timedCache) {
|
||||
super(timedCache);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存的所有资源
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/12/9 14:22
|
||||
*/
|
||||
public List<ResourceDefinition> getAllCaches() {
|
||||
return resourceDefinitions;
|
||||
@Override
|
||||
public String getCommonKeyPrefix() {
|
||||
return ScannerConstants.RESOURCE_CACHE_KEY;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.system.modular.resource.cache;
|
||||
|
||||
import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator;
|
||||
import cn.stylefeng.roses.kernel.scanner.api.constants.ScannerConstants;
|
||||
import cn.stylefeng.roses.kernel.scanner.api.pojo.resource.ResourceDefinition;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* 基于redis的资源缓存
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/5/17 16:05
|
||||
*/
|
||||
public class RedisResourceCache extends AbstractRedisCacheOperator<Map<String, ResourceDefinition>> {
|
||||
|
||||
/**
|
||||
* RedisTemplate的key是appCode,value的key是资源url,value是ResourceDefinition
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/5/17 16:06
|
||||
*/
|
||||
public RedisResourceCache(RedisTemplate<String, Map<String, ResourceDefinition>> redisTemplate) {
|
||||
super(redisTemplate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommonKeyPrefix() {
|
||||
return ScannerConstants.RESOURCE_CACHE_KEY;
|
||||
}
|
||||
|
||||
}
|
|
@ -36,6 +36,8 @@ import com.alibaba.fastjson.JSON;
|
|||
import com.alibaba.fastjson.parser.Feature;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
|
@ -191,4 +193,26 @@ public class ResourceFactory {
|
|||
return fieldMetadataSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将资源的集合整理成一个map,key是url,value是ResourceDefinition
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/5/17 16:21
|
||||
*/
|
||||
public static Map<String, ResourceDefinition> orderedResourceDefinition(List<ResourceDefinition> sysResourceList) {
|
||||
|
||||
HashMap<String, ResourceDefinition> result = new HashMap<>();
|
||||
|
||||
if (ObjectUtil.isEmpty(sysResourceList)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (ResourceDefinition sysResource : sysResourceList) {
|
||||
String url = sysResource.getUrl();
|
||||
result.put(url,sysResource);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ import cn.hutool.core.util.ObjectUtil;
|
|||
import cn.stylefeng.roses.kernel.auth.api.LoginUserApi;
|
||||
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
|
||||
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
|
||||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
|
||||
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
|
||||
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
|
||||
|
@ -44,7 +45,6 @@ import cn.stylefeng.roses.kernel.system.api.RoleServiceApi;
|
|||
import cn.stylefeng.roses.kernel.system.api.pojo.resource.LayuiApiResourceTreeNode;
|
||||
import cn.stylefeng.roses.kernel.system.api.pojo.resource.ResourceRequest;
|
||||
import cn.stylefeng.roses.kernel.system.api.pojo.role.dto.SysRoleResourceDTO;
|
||||
import cn.stylefeng.roses.kernel.system.modular.resource.cache.ResourceCache;
|
||||
import cn.stylefeng.roses.kernel.system.modular.resource.entity.SysResource;
|
||||
import cn.stylefeng.roses.kernel.system.modular.resource.factory.ResourceFactory;
|
||||
import cn.stylefeng.roses.kernel.system.modular.resource.mapper.SysResourceMapper;
|
||||
|
@ -76,12 +76,12 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
|
|||
@Resource
|
||||
private SysResourceMapper resourceMapper;
|
||||
|
||||
@Resource
|
||||
private ResourceCache resourceCache;
|
||||
|
||||
@Resource
|
||||
private RoleServiceApi roleServiceApi;
|
||||
|
||||
@Resource(name = "resourceCache")
|
||||
private CacheOperatorApi<Map<String, ResourceDefinition>> resourceCache;
|
||||
|
||||
@Override
|
||||
public PageResult<SysResource> findPage(ResourceRequest resourceRequest) {
|
||||
LambdaQueryWrapper<SysResource> wrapper = createWrapper(resourceRequest);
|
||||
|
@ -252,9 +252,11 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
|
|||
|
||||
//获取当前应用的所有资源
|
||||
ArrayList<SysResource> allResources = new ArrayList<>();
|
||||
ArrayList<ResourceDefinition> resourceDefinitionArrayList = new ArrayList<>();
|
||||
for (Map.Entry<String, Map<String, ResourceDefinition>> appModularResources : resourceDefinitions.entrySet()) {
|
||||
Map<String, ResourceDefinition> value = appModularResources.getValue();
|
||||
for (Map.Entry<String, ResourceDefinition> modularResources : value.entrySet()) {
|
||||
resourceDefinitionArrayList.add(modularResources.getValue());
|
||||
SysResource resource = ResourceFactory.createResource(modularResources.getValue());
|
||||
allResources.add(resource);
|
||||
}
|
||||
|
@ -264,7 +266,8 @@ public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysRe
|
|||
this.saveBatch(allResources, allResources.size());
|
||||
|
||||
//将资源存入缓存一份
|
||||
resourceCache.saveResourcesToCache(allResources);
|
||||
Map<String, ResourceDefinition> resourceDefinitionMap = ResourceFactory.orderedResourceDefinition(resourceDefinitionArrayList);
|
||||
resourceCache.put(projectCode, resourceDefinitionMap);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright [2020-2030] [https://www.stylefeng.cn]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
|
||||
*
|
||||
* 1.请不要删除和修改根目录下的LICENSE文件。
|
||||
* 2.请不要删除和修改Guns源码头部的版权声明。
|
||||
* 3.请保留源码和相关描述文件的项目出处,作者声明等。
|
||||
* 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns
|
||||
* 6.若您的项目无法满足以上几点,可申请商业授权
|
||||
*/
|
||||
package cn.stylefeng.roses.kernel.system.starter;
|
||||
|
||||
import cn.hutool.cache.CacheUtil;
|
||||
import cn.hutool.cache.impl.TimedCache;
|
||||
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
|
||||
import cn.stylefeng.roses.kernel.cache.api.constants.CacheConstants;
|
||||
import cn.stylefeng.roses.kernel.scanner.api.pojo.resource.ResourceDefinition;
|
||||
import cn.stylefeng.roses.kernel.system.modular.resource.cache.MemoryResourceCache;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 资源缓存自动配置
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/5/17 16:44
|
||||
*/
|
||||
@Configuration
|
||||
public class GunsResourceCacheAutoConfiguration {
|
||||
|
||||
/**
|
||||
* 资源缓存
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/5/17 16:44
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "resourceCache")
|
||||
public CacheOperatorApi<Map<String, ResourceDefinition>> resourceCache() {
|
||||
TimedCache<String, Map<String, ResourceDefinition>> timedCache = CacheUtil.newTimedCache(CacheConstants.NONE_EXPIRED_TIME);
|
||||
return new MemoryResourceCache(timedCache);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.stylefeng.roses.kernel.system.starter.GunsUserCacheAutoConfiguration
|
||||
cn.stylefeng.roses.kernel.system.starter.GunsUserCacheAutoConfiguration,\
|
||||
cn.stylefeng.roses.kernel.system.starter.GunsResourceCacheAutoConfiguration
|
||||
|
|
Loading…
Reference in New Issue