se-common/pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
se-common/src/main/java/com/terra/common/configure/FastJson2JsonRedisSerializer.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
se-common/src/main/java/com/terra/common/configure/RedisConfig.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
se-common/src/main/java/com/terra/common/configure/SpringDocAutoConfiguration.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
se-common/src/main/java/com/terra/common/configure/properties/SpringDocProperties.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
se-common/src/main/java/com/terra/common/service/RedisService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
se-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
se-common/pom.xml
@@ -107,6 +107,23 @@ <artifactId>javax.servlet-api</artifactId> </dependency> <!-- SpringBoot Boot Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- SpringBoot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringDoc webmvc --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> </dependency> </dependencies> </project> se-common/src/main/java/com/terra/common/configure/FastJson2JsonRedisSerializer.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,53 @@ package com.terra.common.configure; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONReader; import com.alibaba.fastjson2.JSONWriter; import com.alibaba.fastjson2.filter.Filter; import com.terra.common.constant.Constants; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationException; import java.nio.charset.Charset; /** * Redis使ç¨FastJsonåºåå * * @author ruoyi */ public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(Constants.JSON_WHITELIST_STR); private Class<T> clazz; public FastJson2JsonRedisSerializer(Class<T> clazz) { super(); this.clazz = clazz; } @Override public byte[] serialize(T t) throws SerializationException { if (t == null) { return new byte[0]; } return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET); } @Override public T deserialize(byte[] bytes) throws SerializationException { if (bytes == null || bytes.length <= 0) { return null; } String str = new String(bytes, DEFAULT_CHARSET); return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER); } } se-common/src/main/java/com/terra/common/configure/RedisConfig.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,43 @@ package com.terra.common.configure; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; 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; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * redisé ç½® * * @author ruoyi */ @Configuration @EnableCaching @AutoConfigureBefore(RedisAutoConfiguration.class) public class RedisConfig extends CachingConfigurerSupport { @Bean @SuppressWarnings(value = { "unchecked", "rawtypes" }) public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class); // 使ç¨StringRedisSerializeræ¥åºåååååºååredisçkeyå¼ template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(serializer); // Hashçkeyä¹éç¨StringRedisSerializerçåºååæ¹å¼ template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(serializer); template.afterPropertiesSet(); return template; } } se-common/src/main/java/com/terra/common/configure/SpringDocAutoConfiguration.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,64 @@ package com.terra.common.configure; import com.terra.common.configure.properties.SpringDocProperties; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityScheme; import io.swagger.v3.oas.models.servers.Server; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import java.util.ArrayList; import java.util.List; /** * Swagger ææ¡£é ç½® * * @author ruoyi */ @EnableConfigurationProperties(SpringDocProperties.class) @ConditionalOnProperty(name = "springdoc.api-docs.enabled", havingValue = "true", matchIfMissing = true) public class SpringDocAutoConfiguration { @Bean @ConditionalOnMissingBean(OpenAPI.class) public OpenAPI openApi(SpringDocProperties properties) { return new OpenAPI().components(new Components() // 设置认è¯ç请æ±å¤´ .addSecuritySchemes("apikey", securityScheme())) .addSecurityItem(new SecurityRequirement().addList("apikey")) .info(convertInfo(properties.getInfo())) .servers(servers(properties.getGatewayUrl())); } public SecurityScheme securityScheme() { return new SecurityScheme().type(SecurityScheme.Type.APIKEY) .name("Authorization") .in(SecurityScheme.In.HEADER) .scheme("Bearer"); } private Info convertInfo(SpringDocProperties.InfoProperties infoProperties) { Info info = new Info(); info.setTitle(infoProperties.getTitle()); info.setDescription(infoProperties.getDescription()); info.setContact(infoProperties.getContact()); info.setLicense(infoProperties.getLicense()); info.setVersion(infoProperties.getVersion()); return info; } public List<Server> servers(String gatewayUrl) { List<Server> serverList = new ArrayList<>(); serverList.add(new Server().url(gatewayUrl)); return serverList; } } se-common/src/main/java/com/terra/common/configure/properties/SpringDocProperties.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,135 @@ package com.terra.common.configure.properties; import io.swagger.v3.oas.models.info.Contact; import io.swagger.v3.oas.models.info.License; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; /** * Swagger é ç½®å±æ§ * * @author ruoyi */ @ConfigurationProperties(prefix = "springdoc") public class SpringDocProperties { /** * ç½å ³ */ private String gatewayUrl; /** * ææ¡£åºæ¬ä¿¡æ¯ */ @NestedConfigurationProperty private InfoProperties info = new InfoProperties(); /** * <p> * ææ¡£çåºç¡å±æ§ä¿¡æ¯ * </p> * * @see io.swagger.v3.oas.models.info.Info * * ä¸ºäº springboot èªå¨ç产é ç½®æç¤ºä¿¡æ¯ï¼æä»¥è¿éå¤å¶ä¸ä¸ªç±»åºæ¥ */ public static class InfoProperties { /** * æ é¢ */ private String title = null; /** * æè¿° */ private String description = null; /** * èç³»äººä¿¡æ¯ */ @NestedConfigurationProperty private Contact contact = null; /** * 许å¯è¯ */ @NestedConfigurationProperty private License license = null; /** * çæ¬ */ private String version = null; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Contact getContact() { return contact; } public void setContact(Contact contact) { this.contact = contact; } public License getLicense() { return license; } public void setLicense(License license) { this.license = license; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } } public String getGatewayUrl() { return gatewayUrl; } public void setGatewayUrl(String gatewayUrl) { this.gatewayUrl = gatewayUrl; } public InfoProperties getInfo() { return info; } public void setInfo(InfoProperties info) { this.info = info; } } se-common/src/main/java/com/terra/common/service/RedisService.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,265 @@ package com.terra.common.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.BoundSetOperations; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Component; import java.util.*; import java.util.concurrent.TimeUnit; /** * spring redis å·¥å ·ç±» * * @author ruoyi **/ @SuppressWarnings(value = { "unchecked", "rawtypes" }) @Component public class RedisService { @Autowired public RedisTemplate redisTemplate; /** * ç¼ååºæ¬ç对象ï¼IntegerãStringãå®ä½ç±»ç * * @param key ç¼åçé®å¼ * @param value ç¼åçå¼ */ public <T> void setCacheObject(final String key, final T value) { redisTemplate.opsForValue().set(key, value); } /** * ç¼ååºæ¬ç对象ï¼IntegerãStringãå®ä½ç±»ç * * @param key ç¼åçé®å¼ * @param value ç¼åçå¼ * @param timeout æ¶é´ * @param timeUnit æ¶é´é¢ç²åº¦ */ public <T> void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) { redisTemplate.opsForValue().set(key, value, timeout, timeUnit); } /** * è®¾ç½®æææ¶é´ * * @param key Redisé® * @param timeout è¶ æ¶æ¶é´ * @return true=设置æåï¼false=设置失败 */ public boolean expire(final String key, final long timeout) { return expire(key, timeout, TimeUnit.SECONDS); } /** * è®¾ç½®æææ¶é´ * * @param key Redisé® * @param timeout è¶ æ¶æ¶é´ * @param unit æ¶é´åä½ * @return true=设置æåï¼false=设置失败 */ public boolean expire(final String key, final long timeout, final TimeUnit unit) { return redisTemplate.expire(key, timeout, unit); } /** * è·åæææ¶é´ * * @param key Redisé® * @return æææ¶é´ */ public long getExpire(final String key) { return redisTemplate.getExpire(key); } /** * 夿 keyæ¯å¦åå¨ * * @param key é® * @return true åå¨ falseä¸åå¨ */ public Boolean hasKey(String key) { return redisTemplate.hasKey(key); } /** * è·å¾ç¼åçåºæ¬å¯¹è±¡ã * * @param key ç¼åé®å¼ * @return ç¼åé®å¼å¯¹åºçæ°æ® */ public <T> T getCacheObject(final String key) { ValueOperations<String, T> operation = redisTemplate.opsForValue(); return operation.get(key); } /** * å é¤å个对象 * * @param key */ public boolean deleteObject(final String key) { return redisTemplate.delete(key); } /** * å é¤éå对象 * * @param collection å¤ä¸ªå¯¹è±¡ * @return */ public boolean deleteObject(final Collection collection) { return redisTemplate.delete(collection) > 0; } /** * ç¼åListæ°æ® * * @param key ç¼åçé®å¼ * @param dataList å¾ ç¼åçListæ°æ® * @return ç¼åç对象 */ public <T> long setCacheList(final String key, final List<T> dataList) { Long count = redisTemplate.opsForList().rightPushAll(key, dataList); return count == null ? 0 : count; } /** * è·å¾ç¼åçlist对象 * * @param key ç¼åçé®å¼ * @return ç¼åé®å¼å¯¹åºçæ°æ® */ public <T> List<T> getCacheList(final String key) { return redisTemplate.opsForList().range(key, 0, -1); } /** * ç¼åSet * * @param key ç¼åé®å¼ * @param dataSet ç¼åçæ°æ® * @return ç¼åæ°æ®ç对象 */ public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet) { BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key); Iterator<T> it = dataSet.iterator(); while (it.hasNext()) { setOperation.add(it.next()); } return setOperation; } /** * è·å¾ç¼åçset * * @param key * @return */ public <T> Set<T> getCacheSet(final String key) { return redisTemplate.opsForSet().members(key); } /** * ç¼åMap * * @param key * @param dataMap */ public <T> void setCacheMap(final String key, final Map<String, T> dataMap) { if (dataMap != null) { redisTemplate.opsForHash().putAll(key, dataMap); } } /** * è·å¾ç¼åçMap * * @param key * @return */ public <T> Map<String, T> getCacheMap(final String key) { return redisTemplate.opsForHash().entries(key); } /** * å¾Hashä¸åå ¥æ°æ® * * @param key Redisé® * @param hKey Hashé® * @param value å¼ */ public <T> void setCacheMapValue(final String key, final String hKey, final T value) { redisTemplate.opsForHash().put(key, hKey, value); } /** * è·åHashä¸çæ°æ® * * @param key Redisé® * @param hKey Hashé® * @return Hashä¸ç对象 */ public <T> T getCacheMapValue(final String key, final String hKey) { HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash(); return opsForHash.get(key, hKey); } /** * è·åå¤ä¸ªHashä¸çæ°æ® * * @param key Redisé® * @param hKeys Hashé®éå * @return Hash对象éå */ public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) { return redisTemplate.opsForHash().multiGet(key, hKeys); } /** * å é¤Hashä¸çææ¡æ°æ® * * @param key Redisé® * @param hKey Hashé® * @return æ¯å¦æå */ public boolean deleteCacheMapValue(final String key, final String hKey) { return redisTemplate.opsForHash().delete(key, hKey) > 0; } /** * è·å¾ç¼åçåºæ¬å¯¹è±¡å表 * * @param pattern å符串åç¼ * @return 对象å表 */ public Collection<String> keys(final String pattern) { return redisTemplate.keys(pattern); } } se-common/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -1 +1,4 @@ com.terra.common.utils.SpringUtils com.terra.common.service.RedisService com.terra.common.configure.RedisConfig com.ruoyi.common.configure.SpringDocAutoConfiguration