栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

4.springboot整合redis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

4.springboot整合redis

1.导入依赖包
 
        
            org.springframework.boot
            spring-boot-starter-data-redis
        

  
            org.springframework.boot
            spring-boot-starter-test
            test
        
2.application.yaml配置redis
 redis:
    host: 127.0.0.1
    port: 6379
    #有密码的要这个没有密码不用写
   #password:
    jedis:
      pool:
        # 连接池最大连接数(使用负值表示没有限制)
        max-active: 8
        #连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1
        # 连接池中的最大空闲连接
        max-idle: 500
        # 连接池中的最小空闲连接
        min-idle: 0
    lettuce:
      #在关闭客户端连接之前等待任务处理完成的最长时间,在这之后,无论任务是否执行完成,都会被执行器关闭。断开与客户端的链接,防止资源不释放
      #默认就是100ms
      shutdown-timeout: 100
3.设置序列化

1.为啥要使用序列化?
在JAVA中,一切皆对象,而将对象的状态信息转为存储或传输的形式需要序列化。


当我们不配置序列化时候使用@AutoWired注解IOC容器为我们选择了StringRedisTemplate类来注入

package com.zrp.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
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.GenericToStringSerializer;


@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 使用 GenericFastJsonRedisSerializer 替换默认序列化
        GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
        // 设置key和value的序列化规则
        redisTemplate.setKeySerializer(new GenericToStringSerializer<>(Object.class));
        redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);
        // 设置hashKey和hashValue的序列化规则
        redisTemplate.setHashKeySerializer(new GenericToStringSerializer<>(Object.class));
        redisTemplate.setHashValueSerializer(genericFastJsonRedisSerializer);
        // 设置支持事物
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

4.添加代码测试
import com.zrp.DomeApplication;
import com.zrp.po.User;
import javafx.application.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = DomeApplication.class)
public class RedisTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testRedis1(){
        redisTemplate.opsForValue().set("aa","123");
        redisTemplate.opsForValue().set("aa","123", 10,TimeUnit.SECONDS);
        System.out.println( redisTemplate.opsForValue().get("aa"));
        User u = new User();
        u.setAge(14);
        u.setUserId(123);
        redisTemplate.opsForValue().set("bb",u);
        System.out.println(redisTemplate.opsForValue().get("bb"));
    }

    @Test
    public void testRedis2(){
      
        //或者
        //redisTemplate.setKeySerializer(new StringRedisSerializer());
        //redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.opsForValue().set("myKey","myValue");
        System.out.println(redisTemplate.opsForValue().get("myKey"));

    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425306.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号