springboot redis工具类
1.RedisConfig
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;
/**
* @Author: zhangdan
* @Date: 2020/5/25 09:39
* @Description:
*/
@Configuration
public class RedisConfig {
@Bean(name = "template")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// 创建RedisTemplate<String, Object>对象
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置连接工厂
template.setConnectionFactory(factory);
// 定义Jackson2JsonRedisSerializer序列化对象
Jackson2JsonRedisSerializer<Object> jacksonSeial = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会报异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jacksonSeial.setObjectMapper(om);
StringRedisSerializer stringSerial = new StringRedisSerializer();
// redis key 序列化方式使用stringSerial
template.setKeySerializer(stringSerial);
// redis value 序列化方式使用jackson
template.setValueSerializer(stringSerial);
// redis hash key 序列化方式使用stringSerial
template.setHashKeySerializer(stringSerial);
// redis hash value 序列化方式使用jackson
template.setHashValueSerializer(stringSerial);
template.afterPropertiesSet();
return template;
}
}
2.RedisClientService
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Resdis工具
*/
@Component
public class RedisClientService<T extends Serializable> {
@Autowired
@Qualifier("redisTemplate") //注入自己定义的redisTemplate
private RedisTemplate redisTemplate;
@Autowired(required = false)
public void setRedisTemplate(RedisTemplate redisTemplate) {
StringRedisSerializer stringSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
this.redisTemplate = redisTemplate;
}
/**
* 查询
* @param key
* @return
*/
public String get(final String key) {
String result = (String)redisTemplate.execute(new RedisCallback<T>() {
@Override
public T doInRedis(RedisConnection connection)
throws DataAccessException {
ValueOperations<String, Serializable> operations = redisTemplate.opsForValue();
T t = (T) operations.get(key);
return t;
}
});
return result;
}
/**
* 新增
* @param key
* @param t
* @return
*/
public Boolean add(final String key ,final T t) {
boolean result = (Boolean) redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
ValueOperations<String, Serializable> valueOper = redisTemplate.opsForValue();
valueOper.set(key, t);
return true;
}
}, false, true);
return result;
}
/**
* 删除
* @param key
*/
public void delete(String key) {
List<String> list = new ArrayList<String>();
list.add(key);
delete(list);
}
/**
* 批量删除
* @param keys
*/
public void delete(List<String> keys) {
redisTemplate.delete(keys);
}
/**
* 删除hash结构中指定的key(hdel)
* @param key1
* @param key2
*/
public void del(String key1,String key2) {
redisTemplate.opsForHash().delete(key1,key2);
}
/**
* 为key设置有效时间
* @param key
* @param val
* @param time
*/
public void setKeyWithTime(String key,String val,int time) {
boolean result = (Boolean) redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection)
throws DataAccessException {
ValueOperations<String, Serializable> valueOper = redisTemplate.opsForValue();
valueOper.set(key,val);
redisTemplate.expire(key,time,TimeUnit.MINUTES);
return true;
}
}, false, true);
}
//redis队列推送
public void publish(String channal ,Object obj) {
redisTemplate.convertAndSend(channal,obj );
}
}