add Redis
parent
0c296d45b9
commit
8bd640b8b0
@ -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<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -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<Serializable, Object> 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<Serializable, Object> 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<Serializable> 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<Serializable, Object> 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<String, Object, Object> 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<String, Object, Object> hash = redisTemplate.opsForHash();
|
||||
return hash.get(key,hashKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表添加
|
||||
*
|
||||
* @param k the k
|
||||
* @param v the v
|
||||
*/
|
||||
public void lPush(String k,Object v){
|
||||
ListOperations<String, Object> list = redisTemplate.opsForList();
|
||||
list.rightPush(k,v);
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表获取
|
||||
*
|
||||
* @param k the k
|
||||
* @param l the l
|
||||
* @param l1 the l 1
|
||||
* @return list
|
||||
*/
|
||||
public List<Object> lRange(String k, long l, long l1){
|
||||
ListOperations<String, Object> 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<String, Object> set = redisTemplate.opsForSet();
|
||||
set.add(key,value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 集合获取
|
||||
*
|
||||
* @param key the key
|
||||
* @return set
|
||||
*/
|
||||
public Set<Object> setMembers(String key){
|
||||
SetOperations<String, Object> 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<String, Object> 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<Object> rangeByScore(String key, double scoure, double scoure1){
|
||||
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
|
||||
return zset.rangeByScore(key, scoure, scoure1);
|
||||
}
|
||||
|
||||
}
|
@ -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()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue