Springboot+Redis 多库操作

2021-12-05  本文已影响0人  HeloWxl

参考:https://blog.csdn.net/Abysscarry/article/details/87495631

版本:

示例代码

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
       </dependency>
#Redis配置
redis:
  database:
    a: 0
    b: 1
  host: localhost
  port: 6379
  password: 123456
  timeout: 5000
  pool:
    max-idle: 30
    min-idle: 0
    max-wait: -1
package com.lin.chat.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableAutoConfiguration
public class RedisConfig {
    @Value("${redis.database.a}")
    private int foreDatabase;

    @Value("${redis.database.b}")
    private int wechatDatabase;

    @Value("${redis.host}")
    private String host;

    @Value("${redis.password}")
    private String password;

    @Value("${redis.port}")
    private int port;

    @Value("${redis.timeout}")
    private int timeout;

    @Value("${redis.pool.max-idle}")
    private int maxIdle;

    @Value("${redis.pool.min-idle}")
    private int minIdle;

    @Value("${redis.pool.max-wait}")
    private long maxWait;

    @Bean
    public JedisPoolConfig getJedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMinIdle(minIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWait);
        return jedisPoolConfig;
    }


    @Bean(name = "foreRedisTemplate")
    public RedisTemplate getForeRedisTemplate(){

        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setPoolConfig(getJedisPoolConfig());
        connectionFactory.setDatabase(foreDatabase);    //此处配置database
        connectionFactory.setHostName(host);
        connectionFactory.setPassword(password);
        connectionFactory.setPort(port);
        connectionFactory.setTimeout(timeout);
        connectionFactory.afterPropertiesSet();         //记得添加这行!
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(connectionFactory);
        return stringRedisTemplate;
    }

    @Bean(name = "wechatRedisTemplate")
    public RedisTemplate getWechatRedisTemplate(){
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setPoolConfig(getJedisPoolConfig());
        connectionFactory.setDatabase(wechatDatabase);
        connectionFactory.setHostName(host);
        connectionFactory.setPassword(password);
        connectionFactory.setPort(port);
        connectionFactory.setTimeout(timeout);
        connectionFactory.afterPropertiesSet();
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(connectionFactory);
        return stringRedisTemplate;
    }
}
@RestController
public class TestController {

    @Autowired
    private RedisTemplate foreRedisTemplate;

    @Autowired
    private RedisTemplate wechatRedisTemplate;

    @RequestMapping(value = "/testRedis", method = RequestMethod.GET)
    public String test() {
        ValueOperations operations = foreRedisTemplate.opsForValue();
        operations.set("age", "11");
        ValueOperations operations2 = wechatRedisTemplate.opsForValue();
        operations2.set("address", "bj");
        return "OK";
    }
}

测试

image.png
上一篇下一篇

猜你喜欢

热点阅读