微服务版后端初始化
This commit is contained in:
47
xjrsoft-common/xjrsoft-common-redis/pom.xml
Normal file
47
xjrsoft-common/xjrsoft-common-redis/pom.xml
Normal file
@ -0,0 +1,47 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>xjrsoft-common</artifactId>
|
||||
<groupId>com.xjrsoft</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>xjrsoft-common-redis</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>${java.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${java.version}</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!--引入Lombok依赖-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Boot Redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--引入hutool依赖-->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@ -0,0 +1,70 @@
|
||||
//package com.xjrsoft.common.redis;
|
||||
//
|
||||
//import lombok.AllArgsConstructor;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//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.*;
|
||||
//import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
//
|
||||
///**
|
||||
// * @Author: tzx
|
||||
// * @Date: 2023/10/7 15:11
|
||||
// */
|
||||
//@Configuration
|
||||
//@AllArgsConstructor
|
||||
//public class RedisAutoConfiguration {
|
||||
//
|
||||
// @Autowired
|
||||
// private final RedisConnectionFactory factory;
|
||||
//
|
||||
// @Bean
|
||||
// public RedisTemplate<String, Object> redisTemplate() {
|
||||
// RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||
// redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
// redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
||||
// redisTemplate.setHashValueSerializer(new StringRedisSerializer());
|
||||
// redisTemplate.setValueSerializer(new StringRedisSerializer());
|
||||
// redisTemplate.setConnectionFactory(factory);
|
||||
// return redisTemplate;
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
|
||||
// return redisTemplate.opsForHash();
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
|
||||
// return redisTemplate.opsForValue();
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
|
||||
// return redisTemplate.opsForList();
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
|
||||
// return redisTemplate.opsForSet();
|
||||
// }
|
||||
//
|
||||
// @Bean
|
||||
// public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
|
||||
// return redisTemplate.opsForZSet();
|
||||
// }
|
||||
//
|
||||
//// /**
|
||||
//// * 配置监听器
|
||||
//// * @param connectionFactory
|
||||
//// * @return
|
||||
//// */
|
||||
//// @Bean
|
||||
//// public RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory) {
|
||||
////
|
||||
//// RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
//// container.setConnectionFactory(connectionFactory);
|
||||
//// return container;
|
||||
//// }
|
||||
//}
|
||||
@ -0,0 +1,70 @@
|
||||
package com.xjrsoft.common.redis.config;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
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.*;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
* Redis配置
|
||||
*
|
||||
* @author Zexy
|
||||
*/
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
//@AutoConfigureBefore(RedisAutoConfiguration.class)
|
||||
public class RedisConfig {
|
||||
private RedisConnectionFactory factory;
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate() {
|
||||
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
|
||||
redisTemplate.setValueSerializer(new StringRedisSerializer());
|
||||
redisTemplate.setConnectionFactory(factory);
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
|
||||
return redisTemplate.opsForHash();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
|
||||
return redisTemplate.opsForValue();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
|
||||
return redisTemplate.opsForList();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
|
||||
return redisTemplate.opsForSet();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
|
||||
return redisTemplate.opsForZSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置监听器
|
||||
* @param connectionFactory
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory) {
|
||||
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
return container;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,159 @@
|
||||
package com.xjrsoft.common.redis.service;
|
||||
|
||||
import cn.hutool.core.lang.TypeReference;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.google.gson.Gson;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author tzx
|
||||
* @description reidis操作工具类
|
||||
* @date 2022-03-02
|
||||
**/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
@SuppressWarnings(value = { "unchecked", "rawtypes" })
|
||||
public class RedisUtil {
|
||||
|
||||
private final RedisTemplate<String, Object> redisTemplate;
|
||||
private final ValueOperations<String, String> valueOperations;
|
||||
private final HashOperations<String, String, Object> hashOperations;
|
||||
private final ListOperations<String, Object> listOperations;
|
||||
private final SetOperations<String, Object> setOperations;
|
||||
private final ZSetOperations<String, Object> zSetOperations;
|
||||
/**
|
||||
* 默认过期时长,单位:秒
|
||||
*/
|
||||
public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
|
||||
/**
|
||||
* 不设置过期时长
|
||||
*/
|
||||
public final static long NOT_EXPIRE = -1;
|
||||
private final static Gson GSON = new Gson();
|
||||
|
||||
public void set(String key, Object value, long expire) {
|
||||
valueOperations.set(key, toJson(value));
|
||||
if (expire != NOT_EXPIRE) {
|
||||
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
public void set(String key, Object value) {
|
||||
set(key, value, NOT_EXPIRE);
|
||||
}
|
||||
|
||||
|
||||
public <T> T get(String key, Class<T> clazz, long expire) {
|
||||
String value = valueOperations.get(key);
|
||||
if (expire != NOT_EXPIRE) {
|
||||
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
||||
}
|
||||
return value == null ? null : fromJson(value, clazz);
|
||||
}
|
||||
|
||||
public <T> T get(String key, TypeReference<T> typeReference, long expire) {
|
||||
String value = valueOperations.get(key);
|
||||
if (expire != NOT_EXPIRE) {
|
||||
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
||||
}
|
||||
return value == null ? null : fromJson(value, typeReference);
|
||||
}
|
||||
|
||||
public <T> T get(String key, Class<T> clazz) {
|
||||
return get(key, clazz, NOT_EXPIRE);
|
||||
}
|
||||
|
||||
public <T> T get(String key, TypeReference<T> typeReference) {
|
||||
return get(key, typeReference, NOT_EXPIRE);
|
||||
}
|
||||
|
||||
public String get(String key, long expire) {
|
||||
String value = valueOperations.get(key);
|
||||
if (expire != NOT_EXPIRE) {
|
||||
redisTemplate.expire(key, expire, TimeUnit.SECONDS);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public String get(String key) {
|
||||
return get(key, NOT_EXPIRE);
|
||||
}
|
||||
|
||||
public Boolean containsKey(String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
public Long getExpire(String key) {
|
||||
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public Boolean isExpired(String key) {
|
||||
return redisTemplate.getExpire(key) > 0;
|
||||
}
|
||||
|
||||
public Long incr(String key, long delta) throws Exception {
|
||||
if (delta < 0) {
|
||||
throw new Exception("递增因子必须大于0");
|
||||
}
|
||||
return valueOperations.increment(key, delta);
|
||||
}
|
||||
|
||||
public Long decr(String key, long delta) throws Exception {
|
||||
if (delta < 0) {
|
||||
throw new Exception("递减因子必须大于0");
|
||||
}
|
||||
return valueOperations.increment(key, -delta);
|
||||
}
|
||||
|
||||
public void delete(String key) {
|
||||
redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除
|
||||
*
|
||||
* @param key
|
||||
*/
|
||||
public void deleteBatch(String key) {
|
||||
if (key != null) {
|
||||
|
||||
Set<String> keys = redisTemplate.keys(Pattern.matches("\\*$", key) ? key : key + "*");
|
||||
if (keys != null && keys.size() > 0) {
|
||||
redisTemplate.delete(keys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Object转成JSON数据
|
||||
*/
|
||||
private String toJson(Object object) {
|
||||
if (object instanceof Integer || object instanceof Long || object instanceof Float ||
|
||||
object instanceof Double || object instanceof Boolean || object instanceof String) {
|
||||
return String.valueOf(object);
|
||||
}
|
||||
return object == null ? "null" : JSONUtil.toJsonStr(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON数据,转成Object
|
||||
*/
|
||||
private <T> T fromJson(String json, Class<T> clazz) {
|
||||
return JSONUtil.toBean(json, clazz, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON数据,转成Object
|
||||
*/
|
||||
private <T> T fromJson(String json, TypeReference<T> typeReference) {
|
||||
return JSONUtil.toBean(json, typeReference, true);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
com.xjrsoft.common.redis.config.RedisConfig
|
||||
com.xjrsoft.common.redis.service.RedisUtil
|
||||
|
||||
Reference in New Issue
Block a user