@Configuration
public class RedisConfig {
//这是一个固定模板,拿去就可以直接使用
@Bean
@SuppressWarnings(“all”)
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String, Object> 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();
//key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
//hash的key也采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
//value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
//hash的value序列化采用jackson
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
pojo
public class User implements Serializable {
private String name;
private int age;
}
测试
@Autowired
@Qualifier(“redisTemplate”)
private RedisTemplate redisTemplate;
@Test
public void test() throws JsonProcessingException {
//真实的开发一般都使用json来传递对象
User user = new User(“狂神说”, 3);
// String jsonUser = new ObjectMapper().writeValueAsString(user);
redisTemplate.opsForValue().set(“user”,user);
System.out.println(redisTemplate.opsForValue().get(“user”));
}
//没有自定义redisTemplate的话,保存的user是一对看不懂的字符串,自定义的话,保存的就是user
工具类
package com.tian.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
//自己的redisTemplate
@Autowired
private RedisTemplate<String,Object> redisTemplate;
public boolean expire(String key,long time){
try{
if (time>0){
redisTemplate.expire(key,time, TimeUnit.SECONDS);
}
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public long getExpire(String key){
return redisTemplate.getExpire(key,TimeUnit.SECONDS);
}
public boolean hasKey(String key){
try{
return redisTemplate.hasKey(key);
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public void del(String… key){
if(key!=null&&key.length>0){
if(key.length==1){
redisTemplate.delete(key[0]);
}else {
redisTemplate.delete((Collection) CollectionUtils.arrayToList(key));
}
}
}
public Object get(String key){
return key==null?null:redisTemplate.opsForValue().get(key);
}
public boolean set(String key,Object value){
try{
redisTemplate.opsForValue().set(key,value);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public boolean set(String key,Object value,long time){
try{
if(time>0){
redisTemplate.opsForValue().set(key,value,time,TimeUnit.SECONDS);
}else {
set(key,value);
}
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public long incr(String key,long delta){
if(delta<0){
throw new RuntimeException(“递增因子必须大于0”);
}
return redisTemplate.opsForValue().increment(key, delta);
}
public long decr(String key,long delta){
if(delta<0){
throw new RuntimeException(“递减因子必须大于0”);
}
return redisTemplate.opsForValue().increment(key, -delta);
}
public Object hget(String key,String item){
return redisTemplate.opsForHash().get(key,item);
}
public Map<Object,Object> hmget(String key){
return redisTemplate.opsForHash().entries(key);
}
public boolean hmset(String key,Map<String,Object>map){
try{
redisTemplate.opsForHash().putAll(key,map);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public boolean hmget(String key,Map<String,Object>map,long time){
try{
redisTemplate.opsForHash().putAll(key,map);
if(time>0){
expire(key,time);
}
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public boolean hset(String key,String item,Object value){
try{
redisTemplate.opsForHash().put(key,item,value);
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public boolean hset(String key,String item,Object value,long time){
try{
redisTemplate.opsForHash().put(key,item,value);
if(time>0){
expire(key, time);
}
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public void hdel(String key,Object… item){
redisTemplate.opsForHash().delete(key, item);
}
public boolean hHasKey(String key,String item){
return redisTemplate.opsForHash().hasKey(key,item);
}
public double hincr(String key,String item,double by){
return redisTemplate.opsForHash().increment(key,item,by);
}
public double hdecr(String key,String item,double by){
return redisTemplate.opsForHash().increment(key,item,-by);
}
public Set sGet(String key){
try{
return redisTemplate.opsForSet().members(key);
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public boolean sHasKey(String key,Object value){
try{
return redisTemplate.opsForSet().isMember(key,value);
}catch (Exception e){
e.printStackTrace();
return false;
}
}
public long sSet(String key,Object… value){
try{
return redisTemplate.opsForSet().add(key, value);
}catch (Exception e){
e.printStackTrace();
return 0;
}
}
public long sSetAndTime(String key,long time,Object… value){
try{
long count= redisTemplate.opsForSet().add(key, value);
if(time>0){
expire(key,time);
}
return count;
}catch (Exception e){
e.printStackTrace();
return 0;
}
}
public long sGetSetSize(String key){
try{
return redisTemplate.opsForSet().size(key);
}catch (Exception e){
e.printStackTrace();
return 0;
}
}
public long setRemove(String key,Object… values){
try{
long count=redisTemplate.opsForSet().remove(key, values);
return count;
}catch (Exception e){
e.printStackTrace();
return 0;
}
}
public List Ilist(String key,long start,long end){
try{
return redisTemplate.opsForList().range(key,start,end);
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public Object lGetIndex(String key,long index){
try{
return redisTemplate.opsForList().index(key, index);
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public boolean lSet(String key,Object value){
try{
redisTemplate.opsForList().rightPush(key, value);
return true;
}catch (Exception e){
最后
由于篇幅原因,就不多做展示了
dex(String key,long index){
try{
return redisTemplate.opsForList().index(key, index);
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public boolean lSet(String key,Object value){
try{
redisTemplate.opsForList().rightPush(key, value);
return true;
}catch (Exception e){
最后
[外链图片转存中…(img-quWavRkD-1719149909438)]
[外链图片转存中…(img-NKdrauMn-1719149909439)]
[外链图片转存中…(img-CLVRsD7x-1719149909439)]
[外链图片转存中…(img-Vt4hEZSq-1719149909440)]
[外链图片转存中…(img-F6XZzYgi-1719149909440)]
[外链图片转存中…(img-tn8hXzr1-1719149909441)]
由于篇幅原因,就不多做展示了