【7.0.4】更新缓存的拓展

pull/16/MERGE
fengshuonan 2021-05-17 17:05:35 +08:00
parent a969fb6cb1
commit dedc085efb
8 changed files with 184 additions and 37 deletions

View File

@ -45,7 +45,7 @@ public interface ScannerConstants {
/**
*
*/
String RESOURCE_CACHE_KEY = "GUNS_RESOURCE_CACHES";
String RESOURCE_CACHE_KEY = "GUNS_RESOURCE_CACHES:";
/**
*

View File

@ -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>

View File

@ -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>> {
/**
*
* TimedCachekeyappCodevaluekeyurlvalueResourceDefinition
*
* @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;
}
}

View File

@ -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.
*
* GunsAPACHE 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>> {
/**
* RedisTemplatekeyappCodevaluekeyurlvalueResourceDefinition
*
* @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;
}
}

View File

@ -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;
}
/**
* mapkeyurlvalueResourceDefinition
*
* @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;
}
}

View File

@ -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

View File

@ -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.
*
* GunsAPACHE 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);
}
}

View File

@ -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