diff --git a/pom.xml b/pom.xml index 03b3ed5..58418f1 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,7 @@ udf-starter-auth udf-starter-datasource-dynamic udf-starter-dal-mybatis + udf-starter-cache-redis @@ -155,6 +156,12 @@ udf-starter-dal-mybatis ${udf-starter.version} + + + org.itkk.udf + udf-starter-cache-redis + ${udf-starter.version} + diff --git a/udf-starter-cache-redis/.checkstyle b/udf-starter-cache-redis/.checkstyle new file mode 100644 index 0000000..7589bde --- /dev/null +++ b/udf-starter-cache-redis/.checkstyle @@ -0,0 +1,7 @@ + + + + + + + diff --git a/udf-starter-cache-redis/.gitignore b/udf-starter-cache-redis/.gitignore new file mode 100644 index 0000000..c541155 --- /dev/null +++ b/udf-starter-cache-redis/.gitignore @@ -0,0 +1,4 @@ +/target/ +/bin/ +/.classpath +/.project diff --git a/udf-starter-cache-redis/pom.xml b/udf-starter-cache-redis/pom.xml new file mode 100644 index 0000000..786859f --- /dev/null +++ b/udf-starter-cache-redis/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + udf-starter-cache-redis + udf-starter-cache-redis + jar + http://itkk.org:81 + + + org.itkk.udf + udf-starter + 1.0 + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-data-redis + + + org.itkk.udf + udf-starter-core + + + diff --git a/udf-starter-cache-redis/src/main/java/org/itkk/udf/cache/redis/CacheRedisConfig.java b/udf-starter-cache-redis/src/main/java/org/itkk/udf/cache/redis/CacheRedisConfig.java new file mode 100644 index 0000000..16350f3 --- /dev/null +++ b/udf-starter-cache-redis/src/main/java/org/itkk/udf/cache/redis/CacheRedisConfig.java @@ -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 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 redisTemplate(RedisConnectionFactory factory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(factory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); + redisTemplate.afterPropertiesSet(); + return redisTemplate; + } + +} diff --git a/udf-starter-cache-redis/src/main/java/org/itkk/udf/cache/redis/CacheRedisProperties.java b/udf-starter-cache-redis/src/main/java/org/itkk/udf/cache/redis/CacheRedisProperties.java new file mode 100644 index 0000000..f7fafd8 --- /dev/null +++ b/udf-starter-cache-redis/src/main/java/org/itkk/udf/cache/redis/CacheRedisProperties.java @@ -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 names; +} diff --git a/udf-starter-cache-redis/src/main/java/org/itkk/udf/cache/redis/DefRedisCachePrefix.java b/udf-starter-cache-redis/src/main/java/org/itkk/udf/cache/redis/DefRedisCachePrefix.java new file mode 100644 index 0000000..bc22cc5 --- /dev/null +++ b/udf-starter-cache-redis/src/main/java/org/itkk/udf/cache/redis/DefRedisCachePrefix.java @@ -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("_")); + } +} diff --git a/udf-starter-dal-mybatis/src/main/java/org/itkk/udf/dal/mybatis/plugin/pagequery/Page.java b/udf-starter-dal-mybatis/src/main/java/org/itkk/udf/dal/mybatis/plugin/pagequery/Page.java index 126a277..fd6f1f5 100644 --- a/udf-starter-dal-mybatis/src/main/java/org/itkk/udf/dal/mybatis/plugin/pagequery/Page.java +++ b/udf-starter-dal-mybatis/src/main/java/org/itkk/udf/dal/mybatis/plugin/pagequery/Page.java @@ -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 { +public class Page implements Serializable { + + /** + * 描述 : id + */ + private static final long serialVersionUID = 1L; /** * 当前页,默认1