zoukankan      html  css  js  c++  java
  • spring boot redis缓存JedisPool使用

    spring boot redis缓存JedisPool使用

    添加依赖pom.xml中添加如下依赖

    <!--  Spring Boot Redis -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-redis</artifactId>
    </dependency>
    

    redis配置文件

    # REDIS (RedisProperties)
    # Redis数据库索引(默认为0)
    spring.redis.database=0
    # Redis服务器地址
    spring.redis.host=192.168.1.80
    # Redis服务器连接端口
    spring.redis.port=6379
    # Redis服务器连接密码(默认为空)
    spring.redis.password=redis
    # 连接池最大连接数(使用负值表示没有限制)
    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
    

    JedisPool的使用

    package com.example.redis;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    /**
     * 类名:RedisCacheConfiguration<br>
     * 描述:<br>
     * 创建人:<br>
     * 创建时间:2016/9/6 17:33<br>
     *
     * @version v1.0
     */
    
    @Configuration
    @EnableCaching
    public class RedisCacheConfiguration extends CachingConfigurerSupport {
        Logger logger = LoggerFactory.getLogger(RedisCacheConfiguration.class);
    
        @Value("${spring.redis.host}")
        private String host;
    
        @Value("${spring.redis.port}")
        private int port;
    
        @Value("${spring.redis.timeout}")
        private int timeout;
    
        @Value("${spring.redis.pool.max-idle}")
        private int maxIdle;
    
        @Value("${spring.redis.pool.max-wait}")
        private long maxWaitMillis;
    
        @Value("${spring.redis.password}")
        private String password;
    
        @Bean
        public JedisPool redisPoolFactory() {
            logger.info("JedisPool注入成功!!");
            logger.info("redis地址:" + host + ":" + port);
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxIdle(maxIdle);
            jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
    
            JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
    
            return jedisPool;
        }
    
    }
    
    

    测试

    注入

    @Autowired
    JedisPool jedisPool;
    

    测试

        String uuid = UUID.randomUUID().toString();
    		logger.info("jedisPool uuid : " + uuid);
    		try (Jedis jedis = jedisPool.getResource()) {
    			jedis.setex(uuid, 1000, user.getUsername());
    		}
    
  • 相关阅读:
    git爬坑不完全指北(二):failed to push some refs to ‘XXX’的解决方案
    javascript精雕细琢(三):作用域与作用域链
    javascript精雕细琢(二):++、--那点事
    git爬坑不完全指北(一):Permission to xxx.git denied to user的解决方案
    深入浅出CSS(三):隐藏BOSS大盘点之默认属性小总结
    读书笔记
    MPP5运维手册
    HTML自闭合(self-closing)标签
    Mysql JDBC的通信协议(报文的格式和基本类型)
    详解 & 0xff 的作用
  • 原文地址:https://www.cnblogs.com/rwxwsblog/p/5846752.html
Copyright © 2011-2022 走看看