zoukankan      html  css  js  c++  java
  • springboot集成redis

    1.配置文件如下

    spring:
    # 热部署
    devtools:
    restart:
    enabled: true #设置开启热部署
    additional-paths: src/main/java #重启目录
    exclude: WEB-INF/**
    freemarker:
    cache: false #页面不加载缓存,修改即时生效
    # redis
    redis:
    host: 192.168.0.6
    port: 6479
    timeout: 5000 # 连接超时时间(毫秒)
    # password:
    pool:
    minIdle: 0 # 连接池中的最小空闲连接
    maxIdle: 8 # 连接池中的最大空闲连接
    maxWait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
    maxActive: 8 # 连接池最大连接数(使用负值表示没有限制)

     2.引入data-redisjar包

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

    3.写redis配置类

    @Configuration
    @ConditionalOnClass(RedisOperations.class)
    @EnableConfigurationProperties(RedisProperties.class)
    public class RedisConfig {

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(
    RedisConnectionFactory redisConnectionFactory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    //使用fastjson序列化
    FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
    // value值的序列化采用fastJsonRedisSerializer
    template.setValueSerializer(fastJsonRedisSerializer);
    template.setHashValueSerializer(fastJsonRedisSerializer);
    // key的序列化采用StringRedisSerializer
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setConnectionFactory(redisConnectionFactory);
    return template;
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(
    RedisConnectionFactory redisConnectionFactory) {
    StringRedisTemplate template = new StringRedisTemplate();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
    }

    }

    @GetMapping("/getdata")
    public void getdata(){
    //测试redis

    redisUtil.set("name","小猫");
    redisUtil.set("age", "11", 15);
    System.out.println(redisUtil.getExpire("name"));
    System.out.println( redisUtil.get("name"));
    System.out.println(redisUtil.getExpire("age"));
    System.out.println( redisUtil.get("age"));

    }

    4.配置好了写简单测试类

  • 相关阅读:
    php面向对象之构造函数和析构函数
    C#语言基础原理及优缺点
    零零散散学算法之具体解释几种最短路径
    Java解惑七:很多其它类之谜
    《Linux设备驱动开发具体解释(第3版)》进展同步更新
    setsockopt的作用
    全排列算法及实现
    【ASP.NET】怎样使用类创建公共函数,在不同ASP.NET页面间反复调用
    Git经常使用命令以及使用方法
    Ansi,UTF8,Unicode,ASCII编码的差别
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/11922500.html
Copyright © 2011-2022 走看看