zoukankan      html  css  js  c++  java
  • springboot指定redis库编号配置实现

    springboot版本1.5.10

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath />
    </parent>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    yml配置

    spring:  
      redis:
        database: 0   #shiro
        host: 127.0.0.1
        port: 6379
        timeout: 6000
        password: null
      redis-cache:
        database: 1 #字符串缓存
        host: 127.0.0.1
        port: 6379
        timeout: 6000
        password: null
        maxTotal: 200
        maxIdle: 20
        minIdle: 10

    redis配置类

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.util.StringUtils;
    
    import redis.clients.jedis.JedisPoolConfig;
    
    @Configuration
    public class RedisCacheConfig {
    
        @Value("${spring.redis-cache.host}")
        private String host; // ip
        @Value("${spring.redis-cache.port}")
        private int port; // 端口
        @Value("${spring.redis-cache.password}")
        private String password; // 密码
        @Value("${spring.redis-cache.database}")
        private int database;//数据库索引
        @Value("${spring.redis-cache.timeout}")
        private int timeout; //链接超时
        @Value("${spring.redis-cache.maxIdle}")
        private int maxIdle;// 最大空闲连接
        @Value("${spring.redis-cache.minIdle}")
        private int minIdle;// 最小空闲连接
        @Value("${spring.redis-cache.maxTotal}")
        private int maxTotal;// 连接池最大连接数(使用负值表示没有限制)
    //注入字符串缓存实例,同理可注入Object缓存实例等等
    @Bean(name
    = "cacheTemplate") public StringRedisTemplate redisTemplate() { StringRedisTemplate temple = new StringRedisTemplate(); temple.setConnectionFactory(connectionFactory()); return temple; } public RedisConnectionFactory connectionFactory() { JedisConnectionFactory jedis = new JedisConnectionFactory(); jedis.setHostName(host); jedis.setPort(port); jedis.setTimeout(timeout); if (!StringUtils.isEmpty(password)) { jedis.setPassword(password); } if (database != 0) { jedis.setDatabase(database); } jedis.setPoolConfig(poolCofig()); // 初始化连接pool jedis.afterPropertiesSet(); return jedis; } public JedisPoolConfig poolCofig() { JedisPoolConfig poolCofig = new JedisPoolConfig(); poolCofig.setMaxIdle(maxIdle); poolCofig.setMinIdle(minIdle); poolCofig.setMaxTotal(maxTotal); return poolCofig; } }

    缓存服务接口和实现

    public interface IStringCacheService {
    
        boolean set(String key, String value);
    
        boolean set(String key, String value, long secondTime);
        
        boolean set(String key, String value, int hourTime);
    
        boolean delete(String... key);
    
        boolean hasKey(String key);
    
        boolean update(String key, String value);
        
        boolean update(String key, String value,long time);
    
        String get(String key);
    
        long getExpire(String key);
        
        boolean setExpire(String key,long time);
    }
    
    import java.util.Arrays;
    import java.util.concurrent.TimeUnit;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.stereotype.Service;
    
    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    @Service
    public class StringCacheServiceImpl implements IStringCacheService {
        @Autowired
        @Qualifier("cacheTemplate")
        private StringRedisTemplate cacheTemplate;
    
        @Override
        public boolean set(String key, String value, long secondTime) {
            try {
                cacheTemplate.opsForValue().set(key, value, secondTime, TimeUnit.SECONDS);
                return true;
            } catch (Exception e) {
                log.error(e.toString());
            }
            return false;
        }
    
        @Override
        public boolean set(String key, String value, int hourTime) {
            try {
                cacheTemplate.opsForValue().set(key, value, hourTime, TimeUnit.HOURS);
                return true;
            } catch (Exception e) {
                log.error(e.toString());
            }
            return false;
        }
    
        @Override
        public boolean hasKey(String key) {
            try {
                return cacheTemplate.hasKey(key);
            } catch (Exception e) {
                log.error(e.toString());
            }
            return false;
        }
    
        @Override
        public long getExpire(String key) {
            return cacheTemplate.getExpire(key, TimeUnit.SECONDS);
        }
    
        @Override
        public boolean setExpire(String key, long time) {
            try {
                if (time > 0) {
                    return cacheTemplate.expire(key, time, TimeUnit.SECONDS);
                }
            } catch (Exception e) {
                log.error(e.toString());
            }
            return false;
        }
    
        @Override
        public boolean set(String key, String value) {
            try {
                cacheTemplate.opsForValue().set(key, value);
                return true;
            } catch (Exception e) {
                log.error(e.toString());
            }
            return false;
        }
    
        @Override
        public boolean delete(String... key) {
            try {
                if (key != null && key.length > 0) {
                    if (key.length == 1) {
                        cacheTemplate.delete(key[0]);
                    } else {
                        cacheTemplate.delete(Arrays.asList(key));
                    }
                }
                return true;
            } catch (Exception e) {
                log.error(e.toString());
            }
            return false;
        }
    
        @Override
        public boolean update(String key, String value) {
            return set(key, value);
        }
    
        @Override
        public String get(String key) {
            return key == null ? null : cacheTemplate.opsForValue().get(key);
        }
    
        @Override
        public boolean update(String key, String value, long time) {
            return set(key, value, time);
        }
    
    }
  • 相关阅读:
    他扎根农村种植双孢菇,从门外汉成为了“土专家”
    农村小伙成立生态农业基地,带领村民打开致富新门路
    小伙让蜜柚成为真正的“黄金柚”,帮助600多农户发家致富
    js时间格式化函数,支持Unix时间戳
    js时间格式化函数,支持Unix时间戳
    js时间格式化函数,支持Unix时间戳
    js时间格式化函数,支持Unix时间戳
    Netty随记之ChannelInboundHandlerAdapter、SimpleChannelInboundHandler
    Netty随记之ChannelInboundHandlerAdapter、SimpleChannelInboundHandler
    Netty随记之ChannelInboundHandlerAdapter、SimpleChannelInboundHandler
  • 原文地址:https://www.cnblogs.com/zincredible/p/10237831.html
Copyright © 2011-2022 走看看