redis配置

博客展示了Redis相关的属性类和配置类代码。属性类定义了Redis的默认参数,配置类则使用Spring Boot的注解进行条件配置,引入了Jedis和RedisTemplate等,为Redis的使用提供了配置基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.属性类
package com.djhu.followup.config;
/**

  • Created by zw on 2017/12/13.
    */

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = “spring.boot.djhu.redis”)
public class RedisProperties {
public static final int DEFAULT_MAXIDLE = 30;
public static final int DEFAULT_MAXACTIVE = 30;
public static final boolean DEFAULT_TESTONBORROW = true;

private String ip;
private Integer port;
private String auth;

private Integer maxIdle = DEFAULT_MAXIDLE;
private Integer maxActive  = DEFAULT_MAXACTIVE;
private Boolean testOnBorrow = DEFAULT_TESTONBORROW;


public String getIp() {
    return ip;
}

public void setIp(String ip) {
    this.ip = ip;
}

public Integer getPort() {
    return port;
}

public void setPort(Integer port) {
    this.port = port;
}

public String getAuth() {
    return auth;
}

public void setAuth(String auth) {
    this.auth = auth;
}

public Integer getMaxIdle() {
    return maxIdle;
}

public void setMaxIdle(Integer maxIdle) {
    this.maxIdle = maxIdle;
}

public Integer getMaxActive() {
    return maxActive;
}

public void setMaxActive(Integer maxActive) {
    this.maxActive = maxActive;
}

public Boolean getTestOnBorrow() {
    return testOnBorrow;
}

public void setTestOnBorrow(Boolean testOnBorrow) {
    this.testOnBorrow = testOnBorrow;
}

}
二.配置类
package com.djhu.followup.config;
/**

  • Created by zw on 2017/12/13.
    */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;

/**

  • code is far away from bug with the animal protecting
  • ┏┓   ┏┓
  • ┏┛┻━━━┛┻┓
  • ┃       ┃
  • ┃   ━   ┃
  • ┃ ┳┛ ┗┳ ┃
  • ┃       ┃
  • ┃   ┻   ┃
  • ┃       ┃
  • ┗━┓   ┏━┛
  • ┃   ┃神兽保佑
  • ┃   ┃代码无BUG!
  • ┃   ┗━━━┓
  • ┃       ┣┓
  • ┃       ┏┛
  • ┗┓┓┏━┳┓┏┛
  • ┃┫┫ ┃┫┫
  • ┗┻┛ ┗┻┛

*/

@Configuration
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnClass(value = {Jedis.class, RedisTemplate.class})
@ConditionalOnProperty(prefix = “spring.boot.djhu.redis”, value=“enabled”, matchIfMissing =false )
public class RedisAutoConfiguration {

@Autowired
private RedisProperties redisProperties;

@Bean
public JedisPoolConfig jedisPoolConfig(){
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(redisProperties.getMaxActive());
    jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
    jedisPoolConfig.setTestOnBorrow(redisProperties.getTestOnBorrow());
    return jedisPoolConfig;
}

@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
    jedisConnectionFactory.setHostName(redisProperties.getIp());
    jedisConnectionFactory.setPassword(redisProperties.getAuth());
    jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
    jedisConnectionFactory.afterPropertiesSet();
    return jedisConnectionFactory;
}

@Bean
public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory){
    RedisTemplate redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(jedisConnectionFactory);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new StringRedisSerializer());
    redisTemplate.afterPropertiesSet();
    return redisTemplate;
}




//@ConditionalOnClass()

}

### 如何配置 Redis 服务器设置参数 #### 使用 YAML 文件配置 Redis 并通过 Python 进行访问 当涉及到使用 YAML 文件来配置 Redis,在Python环境中可以通过`yaml`模块读取配置文件,并利用`redis-py`库建立与Redis服务器的连接。这允许开发者依据具体的应用场景灵活调整配置项,比如主机地址、端口号或是认证密码等[^1]。 ```python import yaml import redis with open('path/to/config.yaml', 'r') as file: config = yaml.safe_load(file) client = redis.Redis( host=config['redis']['host'], port=config['redis']['port'], password=config['redis'].get('password') ) ``` #### 修改 `redis.conf` 来直接配置 Redis 参数 对于更底层或者永久性的更改,则可以直接编辑Redis默认使用的配置文件——通常是位于安装目录下的`redis.conf`文件。此文件包含了众多可调选项,例如绑定IP(`bind`)、监听端口(`port`)以及持久化策略(`save`)等等[^4]。 要修改这些设置,可以按照如下命令给予适当权限后打开配置文件进行编辑: ```bash chmod 777 /etc/redis/redis.conf vim /etc/redis/redis.conf ``` 完成编辑保存退出之后记得重启Redis服务使新设定生效[^5]。 #### 考虑内存大小的选择 值得注意的是,在决定分配给Redis多少内存时应考虑其用途。如果是作为纯粹的数据缓存层,那么即使是较小规模的应用也可能只需要64MB至128MB之间的工作空间就已经足够满足需求了;当然这也取决于具体的业务逻辑复杂度及数据量级等因素[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值