【8.3.3】【file】【cache】初始化文件信息的缓存

pull/62/head
stylefeng 2025-01-14 18:17:09 +08:00
parent ef50a1d0fb
commit 20c6e51a38
9 changed files with 219 additions and 1 deletions

View File

@ -82,4 +82,16 @@ public interface FileConstants {
*/
String DEFAULT_AVATAR_FILE_OBJ_NAME = "defaultAvatar.png";
//-------------------------------缓存相关-------------------------------
/**
*
*/
String FILE_INFO_CACHE_NAME_PREFIX = "FILE_INFO:";
/**
*
*/
Long FILE_CACHE_TIMEOUT_SECONDS = 3600L * 24 * 2;
}

View File

@ -61,6 +61,20 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--内存或者redis的缓存-->
<dependency>
<groupId>com.javaguns.roses</groupId>
<artifactId>cache-sdk-memory</artifactId>
<version>${roses.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.javaguns.roses</groupId>
<artifactId>cache-sdk-redis</artifactId>
<version>${roses.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,27 @@
package cn.stylefeng.roses.kernel.file.modular.cache;
import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator;
import cn.stylefeng.roses.kernel.file.api.constants.FileConstants;
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
/**
*
* <p>
* keyidvalueSysFileInfoResponse
*
* @author fengshuonan
* @since 2025/1/14 16:58
*/
public class FileInfoMemoryCache extends AbstractMemoryCacheOperator<SysFileInfoResponse> {
public FileInfoMemoryCache(TimedCache<String, SysFileInfoResponse> timedCache) {
super(timedCache);
}
@Override
public String getCommonKeyPrefix() {
return FileConstants.FILE_INFO_CACHE_NAME_PREFIX;
}
}

View File

@ -0,0 +1,27 @@
package cn.stylefeng.roses.kernel.file.modular.cache;
import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator;
import cn.stylefeng.roses.kernel.file.api.constants.FileConstants;
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
import org.springframework.data.redis.core.RedisTemplate;
/**
* Redis
* <p>
* keyidvalueSysFileInfoResponse
*
* @author fengshuonan
* @since 2025/1/14 16:58
*/
public class FileInfoRedisCache extends AbstractRedisCacheOperator<SysFileInfoResponse> {
public FileInfoRedisCache(RedisTemplate<String, SysFileInfoResponse> redisTemplate) {
super(redisTemplate);
}
@Override
public String getCommonKeyPrefix() {
return FileConstants.FILE_INFO_CACHE_NAME_PREFIX;
}
}

View File

@ -33,6 +33,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
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.entity.BaseEntity;
@ -100,6 +101,9 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
@Resource
private SysFileStorageService sysFileStorageService;
@Resource(name = "fileInfoCache")
private CacheOperatorApi<SysFileInfoResponse> fileInfoCache;
@Override
public SysFileInfoResponse getFileInfoResult(Long fileId) {

View File

@ -31,6 +31,20 @@
<version>${roses.version}</version>
</dependency>
<!--内存或者redis的缓存-->
<dependency>
<groupId>com.javaguns.roses</groupId>
<artifactId>cache-sdk-memory</artifactId>
<version>${roses.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.javaguns.roses</groupId>
<artifactId>cache-sdk-redis</artifactId>
<version>${roses.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,59 @@
/*
* 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.file.starter.cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.file.api.constants.FileConstants;
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
import cn.stylefeng.roses.kernel.file.modular.cache.FileInfoMemoryCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
*
*
* @author fengshuonan
* @since 2025/1/14 17:03
*/
@Configuration
@ConditionalOnMissingClass("org.springframework.data.redis.connection.RedisConnectionFactory")
public class FileMemoryCacheAutoConfiguration {
/**
*
*
* @author fengshuonan
* @since 2025/1/14 17:04
*/
@Bean
public CacheOperatorApi<SysFileInfoResponse> fileInfoCache() {
TimedCache<String, SysFileInfoResponse> cache = CacheUtil.newTimedCache(1000 * FileConstants.FILE_CACHE_TIMEOUT_SECONDS);
return new FileInfoMemoryCache(cache);
}
}

View File

@ -0,0 +1,59 @@
/*
* 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.file.starter.cache;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.cache.redis.util.CreateRedisTemplateUtil;
import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
import cn.stylefeng.roses.kernel.file.modular.cache.FileInfoRedisCache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
/**
*
*
* @author fengshuonan
* @since 2025/1/14 17:23
*/
@Configuration
@ConditionalOnClass(name = "org.springframework.data.redis.connection.RedisConnectionFactory")
public class FileRedisCacheAutoConfiguration {
/**
* Redis
*
* @author fengshuonan
* @since 2025/1/14 17:25
*/
@Bean
public CacheOperatorApi<SysFileInfoResponse> fileInfoCache(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, SysFileInfoResponse> redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory);
return new FileInfoRedisCache(redisTemplate);
}
}

View File

@ -1,2 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.stylefeng.roses.kernel.file.starter.ProjectFileAutoConfiguration
cn.stylefeng.roses.kernel.file.starter.ProjectFileAutoConfiguration,\
cn.stylefeng.roses.kernel.file.starter.cache.FileRedisCacheAutoConfiguration,\
cn.stylefeng.roses.kernel.file.starter.cache.FileMemoryCacheAutoConfiguration