redis 缓存对象列表

Posted struts-pring

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis 缓存对象列表相关的知识,希望对你有一定的参考价值。

在spring boot环境下有个StringRedisTemplate对象,默认已经为我们配置好了,只需要自动注入过来就能用,但是使用它只能在Redis中存放字符串。具体操作如下:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class Test {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void test() throws Exception {
        stringRedisTemplate.opsForValue().set("aaa", "111");
        Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));

    }
}

因为在StringRedisTemplate的构造器中,设置的序列化器是字符串,所以它只能存取字符串。构造器:

public StringRedisTemplate() {
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        this.setKeySerializer(stringSerializer);
        this.setValueSerializer(stringSerializer);
        this.setHashKeySerializer(stringSerializer);
        this.setHashValueSerializer(stringSerializer);
        }

现在,如果我们想使用RedisTemplate存取对象,那我们只需要设置相应的序列化器就行了。操作如下:

package com.newegg.core.service.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;

@Configuration
public class RedisConfig {

    /**
    * redisTemplate 序列化使用的jdkSerializeable, 存储二进制字节码, 所以自定义序列化类
    * @param redisConnectionFactory
    * @return
    */
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        // 使用Jackson2JsonRedisSerialize 替换默认序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        // 设置value的序列化规则和 key的序列化规则
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

接着我们来测试一下:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class Test {
    @Autowired
    private RedisTemplate<Object, Object> template;

    @Test
    public void savereids() {
        User u = new User(1, "王伟", 21);
        template.opsForValue().set(u.getId() + "", u);
        User result = (User) template.opsForValue().get(u.getId() + "");
        System.out.println(result.toString());
    }

    @Test
    public void saveHashReids(){
    for(int i=1;i<10;i++){
        User u=new User(i,"王伟",21);
        template.opsForHash().put("myCache",u.getId(),u);
    }
        ArrayList<User> list=template.opsForHash().values("myCache")
    }
}

 

以上是关于redis 缓存对象列表的主要内容,如果未能解决你的问题,请参考以下文章

redis的五种数据结构及其缓存

Redis缓存设计

为 memcached 和 Rails 组合片段和对象缓存的最佳方式

第二百九十七节,python操作redis缓存-List类型,可以理解为列表

使用 Redis 缓存来实现用户最近浏览的商品列表

缓存数据库-redis数据类型和操作(list)