mirror of
https://gitee.com/wangkang/udf.git
synced 2026-10-29 19:53:40 +00:00
添加redis缓存组件
This commit is contained in:
@@ -45,6 +45,7 @@
|
||||
<module>udf-starter-auth</module>
|
||||
<module>udf-starter-datasource-dynamic</module>
|
||||
<module>udf-starter-dal-mybatis</module>
|
||||
<module>udf-starter-cache-redis</module>
|
||||
</modules>
|
||||
<!-- 依赖管理 -->
|
||||
<dependencyManagement>
|
||||
@@ -155,6 +156,12 @@
|
||||
<artifactId>udf-starter-dal-mybatis</artifactId>
|
||||
<version>${udf-starter.version}</version>
|
||||
</dependency>
|
||||
<!-- udf-starter-cache-redis -->
|
||||
<dependency>
|
||||
<groupId>org.itkk.udf</groupId>
|
||||
<artifactId>udf-starter-cache-redis</artifactId>
|
||||
<version>${udf-starter.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<!-- 构建 -->
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="true">
|
||||
<fileset name="all" enabled="true" check-config-name="itkk.org" local="false">
|
||||
<file-match-pattern match-pattern="." include-pattern="true"/>
|
||||
</fileset>
|
||||
</fileset-config>
|
||||
@@ -0,0 +1,4 @@
|
||||
/target/
|
||||
/bin/
|
||||
/.classpath
|
||||
/.project
|
||||
@@ -0,0 +1,34 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<!-- maven坐标 -->
|
||||
<artifactId>udf-starter-cache-redis</artifactId>
|
||||
<name>udf-starter-cache-redis</name>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://itkk.org:81</url>
|
||||
<!-- 父依赖 -->
|
||||
<parent>
|
||||
<groupId>org.itkk.udf</groupId>
|
||||
<artifactId>udf-starter</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- spring-boot-starter-test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- springboot redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.itkk.udf</groupId>
|
||||
<artifactId>udf-starter-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package org.itkk.udf.cache.redis;
|
||||
|
||||
import org.itkk.udf.core.ApplicationConfig;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.cache.RedisCacheManager;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* CacheRedisConfig
|
||||
*/
|
||||
@EnableCaching
|
||||
@Configuration
|
||||
public class CacheRedisConfig {
|
||||
|
||||
/**
|
||||
* DEFAULT_EXPIRATION
|
||||
*/
|
||||
public static final long DEFAULT_EXPIRATION = 60;
|
||||
|
||||
/**
|
||||
* cacheManager
|
||||
*
|
||||
* @param applicationConfig applicationConfig
|
||||
* @param cacheRedisProperties cacheRedisProperties
|
||||
* @param redisTemplate redisTemplate
|
||||
* @return CacheManager
|
||||
*/
|
||||
@Bean
|
||||
public CacheManager cacheManager(ApplicationConfig applicationConfig, CacheRedisProperties cacheRedisProperties, RedisTemplate<Object, Object> redisTemplate) {
|
||||
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
|
||||
cacheManager.setTransactionAware(true);
|
||||
cacheManager.setUsePrefix(cacheRedisProperties.getUsePrefix());
|
||||
cacheManager.setCachePrefix(new DefRedisCachePrefix(cacheRedisProperties.getPrefix()));
|
||||
cacheManager.setDefaultExpiration(cacheRedisProperties.getDefaultExpiration());
|
||||
cacheManager.setExpires(cacheRedisProperties.getNames());
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* redisTemplate
|
||||
*
|
||||
* @param factory factory
|
||||
* @return RedisTemplate
|
||||
*/
|
||||
@Bean
|
||||
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
|
||||
redisTemplate.setConnectionFactory(factory);
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
|
||||
redisTemplate.afterPropertiesSet();
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package org.itkk.udf.cache.redis;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* CacheRedisProperties
|
||||
*/
|
||||
@Component
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "org.itkk.cache.redis.properties")
|
||||
public class CacheRedisProperties {
|
||||
|
||||
/**
|
||||
* 是否使用前缀
|
||||
*/
|
||||
private Boolean usePrefix = true;
|
||||
|
||||
/**
|
||||
* 描述 : 缓存前缀
|
||||
*/
|
||||
private String prefix;
|
||||
|
||||
/**
|
||||
* 默认的过期时间
|
||||
*/
|
||||
private Long defaultExpiration = CacheRedisConfig.DEFAULT_EXPIRATION;
|
||||
|
||||
/**
|
||||
* 描述 : 缓存名称(规则: key:缓存名称,value:缓存过期时间)
|
||||
*/
|
||||
private Map<String, Long> names;
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package org.itkk.udf.cache.redis;
|
||||
|
||||
import org.springframework.data.redis.cache.RedisCachePrefix;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* DefRedisCachePrefix
|
||||
*/
|
||||
public class DefRedisCachePrefix implements RedisCachePrefix {
|
||||
/**
|
||||
* serializer
|
||||
*/
|
||||
private final RedisSerializer serializer = new StringRedisSerializer();
|
||||
|
||||
/**
|
||||
* prefix
|
||||
*/
|
||||
private final String prefix;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param prefix 前缀
|
||||
*/
|
||||
public DefRedisCachePrefix(String prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte[] prefix(String cacheName) {
|
||||
return serializer.serialize(prefix.concat("_"));
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -4,6 +4,7 @@ import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -13,7 +14,12 @@ import java.util.List;
|
||||
*/
|
||||
@ApiModel(description = "分页消息体")
|
||||
@Data
|
||||
public class Page<E> {
|
||||
public class Page<E> implements Serializable {
|
||||
|
||||
/**
|
||||
* 描述 : id
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 当前页,默认1
|
||||
|
||||
Reference in New Issue
Block a user