Springboot集成Redis,使用RedisTemplate操作

1、添加依赖

pom.xml:

        <!-- 整合redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2、Redis配置

application.properties:

spring.redis.host=localhost
spring.redis.port=6379
#会有分区,默认是0
#spring.redis.database=
#spring.redis.password=
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.jedis.pool.max-idle=500
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=5000ms

3、Controller业务

这里使用Springboot提供的RedisTemplate对Redis进行相关操作:

package com.lzz.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class indexController {
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/setString")
    public String setString(){
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set("test","2");
        return "DONE";
    }
    @RequestMapping("/getString")
    public String getString(){
        ValueOperations<String,String> ops= redisTemplate.opsForValue();
        return ops.get("test");
    }
}

RedisTemplate介绍

spring 封装了 RedisTemplate

RedisTemplate中定义了对5种数据结构操作

  • redisTemplate.opsForValue();//操作字符串
    redisTemplate.opsForList();//操作list
    redisTemplate.opsForHash();//操作hash
    redisTemplate.opsForSet();//操作set
    redisTemplate.opsForZSet();//操作sortSet,有序set

StringRedisTemplate与RedisTemplate

spring 同时提供StringRedisTemplate,在键值对都是String类型时推荐使用StringRedisTemplate进行操作;虽然StringRedisTemplate继承RedisTemplate,但是它们管理是数据是不互通的。此外:

  • SDR默认采用的序列化策略有两种,一种是String的序列化策略,一种是JDK的序列化策略。
  • StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。
  • RedisTemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。

两种策略进行序列化会有一定的不同:
JDK的序列化策略是将其key和value保存成字节数组;String则是存储为字符串;通过命令或管理工具查看时,看起来会是乱码。redis序列化配置方法如下:(这里只配置了key的序列化,因为value的类型很多,如果是对象类型是无法采用string序列化的)

/**
 * @author lzz
 * @description:
 * @date 2018/8/3 16:45
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate();
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        return  redisTemplate;
    }
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值