springboot指定redis库编号配置实现

Posted zincredible

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot指定redis库编号配置实现相关的知识,希望对你有一定的参考价值。

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);
    }

}

 



以上是关于springboot指定redis库编号配置实现的主要内容,如果未能解决你的问题,请参考以下文章

基于springboot实现快递代取管理系统

刚刚用spring boot 并用缓存数据库redis ,哪里有比较好的教程呢,菜鸟

2022-03-12 SpringBoot 使用redis做缓存,设置失效时间以及序列化

php实现redis数据库指定库号迁移的方法

springboot-redis缓存

Java之SpringBoot集成redis实现消息队列