diff --git a/conf/szwh.properties b/conf/szwh.properties index ebd3f2b..444f688 100644 --- a/conf/szwh.properties +++ b/conf/szwh.properties @@ -1,5 +1,7 @@ # jwt 密钥 jwt.key=wenhuayun_token_ +# redis配置信息 + # 用户认证 user.pem.prikey.path=/conf/cert/user_real/private_key.pem user.pem.pubkey.path=/conf/cert/user_real/public_key.pem diff --git a/pom.xml b/pom.xml index f79cf18..03d7c2c 100644 --- a/pom.xml +++ b/pom.xml @@ -53,7 +53,12 @@ ch.qos.logback logback-classic - + + + com.google.code.gson + gson + 2.8.6 + com.github.pagehelper diff --git a/src/main/java/cn/chjyj/szwh/configure/RedisConfig.java b/src/main/java/cn/chjyj/szwh/configure/RedisConfig.java new file mode 100644 index 0000000..cfbd3e4 --- /dev/null +++ b/src/main/java/cn/chjyj/szwh/configure/RedisConfig.java @@ -0,0 +1,54 @@ +package cn.chjyj.szwh.configure; + +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; + +/** + * Redis配置 + * + */ +@Configuration +public class RedisConfig { + @Autowired + private RedisConnectionFactory factory; + + @Bean + public RedisTemplate redisTemplate() { + RedisTemplate 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 hashOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForHash(); + } + + @Bean + public ValueOperations valueOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForValue(); + } + + @Bean + public ListOperations listOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForList(); + } + + @Bean + public SetOperations setOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForSet(); + } + + @Bean + public ZSetOperations zSetOperations(RedisTemplate redisTemplate) { + return redisTemplate.opsForZSet(); + } +} diff --git a/src/main/java/cn/chjyj/szwh/utils/RedisKeys.java b/src/main/java/cn/chjyj/szwh/utils/RedisKeys.java new file mode 100644 index 0000000..a49ff1d --- /dev/null +++ b/src/main/java/cn/chjyj/szwh/utils/RedisKeys.java @@ -0,0 +1,13 @@ +package cn.chjyj.szwh.utils; + +/** + * Redis所有Keys + * + * @author Mark sunlightcs@gmail.com + */ +public class RedisKeys { + + public static String getSysConfigKey(String key){ + return "sys:config:" + key; + } +} diff --git a/src/main/java/cn/chjyj/szwh/utils/RedisUtils.java b/src/main/java/cn/chjyj/szwh/utils/RedisUtils.java new file mode 100644 index 0000000..44f26c0 --- /dev/null +++ b/src/main/java/cn/chjyj/szwh/utils/RedisUtils.java @@ -0,0 +1,91 @@ +package cn.chjyj.szwh.utils; + +import com.google.gson.Gson; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.*; +import org.springframework.stereotype.Component; + +import java.util.concurrent.TimeUnit; + +/** + * Redis工具类 + * + * @author Mark sunlightcs@gmail.com + */ +@Component +public class RedisUtils { + @Autowired + private RedisTemplate redisTemplate; + @Autowired + private ValueOperations valueOperations; + @Autowired + private HashOperations hashOperations; + @Autowired + private ListOperations listOperations; + @Autowired + private SetOperations setOperations; + @Autowired + private ZSetOperations 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, DEFAULT_EXPIRE); + } + + public T get(String key, Class 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 get(String key, Class clazz) { + return get(key, clazz, 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 void delete(String key) { + redisTemplate.delete(key); + } + + /** + * 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 gson.toJson(object); + } + + /** + * JSON数据,转成Object + */ + private T fromJson(String json, Class clazz){ + return gson.fromJson(json, clazz); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a48f914..64d89ff 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -7,4 +7,14 @@ server.port=9090 # 服务请求上下文 #server.servlet.context-path=/chapi mybatis.mapper-locations=classpath*:/mapper/**/*.xml -spring. \ No newline at end of file + +#指定redis信息 (如 host, ip, password) +spring.redis.host=localhost +spring.redis.port=6379 +#没有密码可以不用配置这个 +#spring.redis.password=123456 +# 连接池配置 +spring.redis.jedis.pool.enabled=true +# 最大连接 +spring.redis.jedis.pool.max-active=8 +spring.redis.jedis.pool.max-wait=-1 \ No newline at end of file diff --git a/szwh.iml b/szwh.iml index 0475e52..671477c 100644 --- a/szwh.iml +++ b/szwh.iml @@ -107,6 +107,7 @@ +