二.使用Redis
本文最后更新于 1538 天前,其中的信息可能已经有所发展或是发生改变。

1. pom

<!--redis启动器坐标-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.2.3.RELEASE</version>
</dependency>

2. application.yml

spring:
  redis:
    # Redis服务器地址
    host: ${redis.ip:127.0.0.1}
    # Redis服务器连接端口
    port: ${redis.port:6379}
    # Redis服务器连接密码
    password: ${redis.password:}
    # 连接超时时间(毫秒)
    timeout: 5000
    jedis:
      pool:
        # 连接池最大连接数(使用负值表示没有限制)
        max-active: 8
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1
        # 连接池中的最大空闲连接
        max-idle: 10
        # 连接池中的最小空闲连接
        min-idle: 5

3. 编写Spring Data Redis的配置类

/**
 * 配置Redis
 * @author yuyy
 * @date 2020/2/2 16:00
 */
@Configuration
public class RedisConfig {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Bean
    public RedisTemplate<Object,Object> getRedisTemplate(){
        System.out.println(redisTemplate);
        //配置key的序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //配置value的序列化
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}

4. 使用Redis

@Autowired
private RedisConfig redisConfig;

RedisTemplate<Object, Object> redisTemplate = redisConfig.getRedisTemplate();
//配置value的Json格式存储
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(TestParam.class));
redisTemplate.opsForValue().set("yuyy",TestParam.builder().a(55).b(52).c(98).build());
redisTemplate.opsForValue().set("dd","fdsfdsfsdfds");
TestParam yuyy = (TestParam)redisTemplate.opsForValue().get("yuyy");
System.out.println(yuyy);

5. 查看Redis数据库

image

作者:Yuyy
博客:https://yuyy.info
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇