diff --git a/pom.xml b/pom.xml index 68c58c6..faef7aa 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,15 @@ org.springframework.boot spring-boot-starter-jdbc + + + org.springframework.boot + spring-boot-starter-data-redis + + + org.apache.commons + commons-pool2 + com.baomidou mybatis-plus-boot-starter diff --git a/src/main/java/com/peteralbus/config/RedisConfig.java b/src/main/java/com/peteralbus/config/RedisConfig.java new file mode 100644 index 0000000..c3497f4 --- /dev/null +++ b/src/main/java/com/peteralbus/config/RedisConfig.java @@ -0,0 +1,46 @@ +package com.peteralbus.config; + + +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +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.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * The type Redis config. + * @author PeterAlbus + */ +@Configuration +public class RedisConfig { + + /** + * Redis template redis template. + * + * @param factory the factory + * @return the redis template + */ + @SuppressWarnings("all") + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate template = new RedisTemplate(); + template.setConnectionFactory(factory); + Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); + ObjectMapper om = new ObjectMapper(); + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); + jackson2JsonRedisSerializer.setObjectMapper(om); + StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); + template.setKeySerializer(stringRedisSerializer); + template.setHashKeySerializer(stringRedisSerializer); + template.setValueSerializer(jackson2JsonRedisSerializer); + template.setHashValueSerializer(jackson2JsonRedisSerializer); + template.afterPropertiesSet(); + return template; + } + +} diff --git a/src/main/java/com/peteralbus/util/RedisUtils.java b/src/main/java/com/peteralbus/util/RedisUtils.java new file mode 100644 index 0000000..0b3f22d --- /dev/null +++ b/src/main/java/com/peteralbus/util/RedisUtils.java @@ -0,0 +1,219 @@ +package com.peteralbus.util; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.*; +import org.springframework.stereotype.Component; + +import java.io.Serializable; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +/** + * The type Redis utils. + */ +@Component +public class RedisUtils { + + private RedisTemplate redisTemplate; + + @Autowired + public void setRedisTemplate(RedisTemplate redisTemplate) + { + this.redisTemplate = redisTemplate; + } + + /** + * 写入缓存 + * + * @param key the key + * @param value the value + * @return boolean + */ + public boolean set(final String key, Object value) { + boolean result = false; + try { + ValueOperations operations = redisTemplate.opsForValue(); + operations.set(key, value); + result = true; + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + + /** + * 写入缓存设置时效时间 + * + * @param key the key + * @param value the value + * @param expireTime the expire time + * @param timeUnit the time unit + * @return boolean + */ + public boolean set(final String key, Object value, Long expireTime , TimeUnit timeUnit) { + boolean result = false; + try { + ValueOperations operations = redisTemplate.opsForValue(); + operations.set(key, value); + redisTemplate.expire(key, expireTime, timeUnit); + result = true; + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + + /** + * 批量删除对应的value + * + * @param keys the keys + */ + public void remove(final String... keys) { + for (String key : keys) { + remove(key); + } + } + + /** + * 批量删除key + * + * @param pattern the pattern + */ + public void removePattern(final String pattern) { + Set keys = redisTemplate.keys(pattern); + if (keys.size() > 0){ + redisTemplate.delete(keys); + } + } + + /** + * 删除对应的value + * + * @param key the key + */ + public void remove(final String key) { + if (exists(key)) { + redisTemplate.delete(key); + } + } + + /** + * 判断缓存中是否有对应的value + * + * @param key the key + * @return boolean + */ + public boolean exists(final String key) { + return redisTemplate.hasKey(key); + } + + /** + * 读取缓存 + * + * @param key the key + * @return object + */ + public Object get(final String key) { + Object result = null; + ValueOperations operations = redisTemplate.opsForValue(); + result = operations.get(key); + return result; + } + + /** + * 哈希 添加 + * + * @param key the key + * @param hashKey the hash key + * @param value the value + */ + public void hmSet(String key, Object hashKey, Object value){ + HashOperations hash = redisTemplate.opsForHash(); + hash.put(key,hashKey,value); + } + + /** + * 哈希获取数据 + * + * @param key the key + * @param hashKey the hash key + * @return object + */ + public Object hmGet(String key, Object hashKey){ + HashOperations hash = redisTemplate.opsForHash(); + return hash.get(key,hashKey); + } + + /** + * 列表添加 + * + * @param k the k + * @param v the v + */ + public void lPush(String k,Object v){ + ListOperations list = redisTemplate.opsForList(); + list.rightPush(k,v); + } + + /** + * 列表获取 + * + * @param k the k + * @param l the l + * @param l1 the l 1 + * @return list + */ + public List lRange(String k, long l, long l1){ + ListOperations list = redisTemplate.opsForList(); + return list.range(k,l,l1); + } + + /** + * 集合添加 + * + * @param key the key + * @param value the value + */ + public void add(String key,Object value){ + SetOperations set = redisTemplate.opsForSet(); + set.add(key,value); + } + + /** + * 集合获取 + * + * @param key the key + * @return set + */ + public Set setMembers(String key){ + SetOperations set = redisTemplate.opsForSet(); + return set.members(key); + } + + /** + * 有序集合添加 + * + * @param key the key + * @param value the value + * @param scoure the scoure + */ + public void zAdd(String key,Object value,double scoure){ + ZSetOperations zset = redisTemplate.opsForZSet(); + zset.add(key,value,scoure); + } + + /** + * 有序集合获取 + * + * @param key the key + * @param scoure the scoure + * @param scoure1 the scoure 1 + * @return set + */ + public Set rangeByScore(String key, double scoure, double scoure1){ + ZSetOperations zset = redisTemplate.opsForZSet(); + return zset.rangeByScore(key, scoure, scoure1); + } + +} diff --git a/src/main/resources/applicationExample.yml b/src/main/resources/applicationExample.yml index a7c5695..d118c9d 100644 --- a/src/main/resources/applicationExample.yml +++ b/src/main/resources/applicationExample.yml @@ -1,6 +1,6 @@ server: port: 8089 - + spring: servlet: multipart: @@ -15,6 +15,10 @@ spring: password: password url: jdbc:mysql://localhost:3306/blogs?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai driver-class-name: com.mysql.cj.jdbc.Driver + redis: + host: redis-host + port: 6378 + password: yourPassword mybatis-plus: mapper-locations: classpath:mapper/*Mapper.xml diff --git a/src/test/java/com/peteralbus/BlogBackApplicationTests.java b/src/test/java/com/peteralbus/BlogBackApplicationTests.java index 80e5db0..dad86a5 100644 --- a/src/test/java/com/peteralbus/BlogBackApplicationTests.java +++ b/src/test/java/com/peteralbus/BlogBackApplicationTests.java @@ -1,21 +1,14 @@ package com.peteralbus; -import com.peteralbus.domain.Blog; -import com.peteralbus.mapper.BlogMapper; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import java.util.List; - @SpringBootTest class BlogBackApplicationTests { - @Test void contextLoads() { - } }