mirror of https://gitee.com/stylefeng/roses
【8.3.3】【file】【cache】初始化文件信息的缓存
parent
ef50a1d0fb
commit
20c6e51a38
|
@ -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;
|
||||
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
* key是文件id,value是SysFileInfoResponse
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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>
|
||||
* key是文件id,value是SysFileInfoResponse
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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) {
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
* 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.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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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.
|
||||
*
|
||||
* 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.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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue