springboot使用redistemplate操作redis

一、Redis数据类型

1、String字符串

  String是Redis最基本的类型,一个key对应一个value。String类型是二进制安全的,即Redis的string可以包含任何数据,比如jpg图片或者序列化的对象 。一个键最大能存储512MB。

2、列表

  列表是简单的字符串列表(使用链表实现的),排序为插入的顺序。列表的最大长度为2^32-1。用列表可以实现生产者消费者模式,生产者调用lpush添加项到列表中,消费者调用rpop从列表中提取,如果没有元素,则轮询去获取,或者使用brpop等待生产者添加项到列表中。

3、集合

  集合是无序的字符串集合,集合中的值是唯一的,无序的。可测试元素是否存在,对多个集合执行交集、并集和差集等等。

4、ZSet 有序集合

  有序集合由唯一的,不重复的字符串元素组成。有序集合中的每个元素都关联了一个浮点值,称为分数。可以把有序看成hash和集合的混合体,分数即为hash的key。有序集合中的元素是按序存储的,不是请求时才排序的。

5、Hash

  哈希是字符串和字符串之间的映射,是表示对象的完美数据类型。

二、Springboot整合Redis

1、添加pom依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>    
2、数据库连接配置
-- Redis数据库索引(默认为0)
spring.redis.database=0  
-- Redis服务器地址
spring.redis.host=127.0.0.1
-- Redis服务器连接端口
spring.redis.port=6379  
-- Redis服务器连接密码(默认为空)
spring.redis.password=
-- 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8  
-- 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1  
-- 连接池中的最大空闲连接
spring.redis.pool.max-idle=8  
-- 连接池中的最小空闲连接
spring.redis.pool.min-idle=0  
-- 连接超时时间(毫秒)
spring.redis.timeout=0  
3、RedisConfig类
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.EnableCaching;
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
@EnableCaching
public class RedisConfig {

    /**
     * retemplate相关配置
     *
     * @param factory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {

        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 配置连接工厂
        template.setConnectionFactory(factory);

        // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
        Jackson2JsonRedisSerializer 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);

        // 值采用json序列化
        template.setValueSerializer(jacksonSeial);
        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());

        // 设置hash key 和value序列化模式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jacksonSeial);
        template.afterPropertiesSet();

        return template;
    }
    
     /**
     * 对redis字符串类型数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    /**
     * 对链表类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForList();
    }

    /**
     * 对无序集合类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

    /**
     * 对有序集合类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForZSet();
    }

    /**
     * 对hash类型的数据操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }
}
4、Redis操作工具类
@Slf4j
@Component
public class RedisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private ValueOperations<String, Object> valueOperations;

    @Autowired
    private ListOperations<String, Object> listOperations;

    @Autowired
    private SetOperations<String, Object> setOperations;

    @Autowired
    private ZSetOperations<String, Object> zSetOperations;

    @Autowired
    private HashOperations<String, String, Object> hashOperations;

    public void test() {
        redisTemplate.expire("testValue", 1, TimeUnit.MINUTES);
        redisTemplate.delete(Lists.newArrayList("testValue", "testList", "testSet", "testZset", "testHash"));
    }

    public void testValueOperation() {
        valueOperations.set("testValue", "11111");
        log.info(valueOperations.get("testValue").toString());
    }

    public void testListOperation() {
        listOperations.leftPush("testList", "22222");
        log.info(listOperations.leftPop("testList").toString());
//        log.info(listOperations.leftPop("testList").toString());

        listOperations.leftPush("testList", "33333");
        listOperations.leftPush("testList", "44444");
        log.info("size:{}", listOperations.size("testList"));
    }

    public void testSetOperation() {
        setOperations.add("testSet", "55555", "66666");
        log.info(setOperations.members("testSet").toString());
    }

    public void testZsetOperation() {
        zSetOperations.incrementScore("testZset", "v1", 1);
        zSetOperations.incrementScore("testZset", "v2", 2);
        zSetOperations.incrementScore("testZset", "v3", 3);
        zSetOperations.incrementScore("testZset", "v4", 4);
        zSetOperations.incrementScore("testZset", "v1", 10);
        log.info(zSetOperations.score("testZset", "v1").toString());
        log.info(zSetOperations.reverseRange("testZset", 1, 3).toString());
    }

    public void testHashOperation() {
        hashOperations.put("testHash", "k1", "v1");
        hashOperations.put("testHash", "k2", "v2");
        hashOperations.put("testHash", "k3", "v3");
        log.info(hashOperations.get("testHash", "k1").toString());
        log.info(hashOperations.entries("testHash").toString());
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值