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

      

      上篇整合了DB层,现在开始整合缓存层,使用redis。

      springboot驱动注解,使用spring注入JedisPool便可封装自己的redis工具类。

    package hello.configuration;
    
    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 org.springframework.context.annotation.PropertySource;
    
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    @Configuration
    @PropertySource(value = "classpath:/redis.properties")
    @EnableCaching
    public class RedisCacheConfiguration extends CachingConfigurerSupport {
        @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;
    
        @Bean
        public JedisPool redisPoolFactory() {
            System.out.println("JedisPool注入成功!!");
            System.out.println("redis地址:" + host + ":" + port);
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxIdle(maxIdle);
            jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
            JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port);
            return jedisPool;
        }
    }

      redis.properties配置文件,为了清晰区分资源文件。

    spring.redis.database=0
    spring.redis.host=localhost
    spring.redis.port=6379
    spring.redis.pool.max-active=8
    spring.redis.pool.max-idle=8
    spring.redis.pool.max-wait=-1
    spring.redis.pool.min-idle=0
    spring.redis.timeout=0
  • 相关阅读:
    查看另外一台服务器的版本号
    制作数据集(二)
    制作数据集(一)
    中文分词工具包 PKUSeg
    Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend)
    修改主机名
    例题
    Oracle基本使用
    Linux里面的MySQL忘记密码RROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    SpringBoot2.x以上配置schema.sql脚本
  • 原文地址:https://www.cnblogs.com/cl2Blogs/p/5679657.html
Copyright © 2011-2022 走看看