项目中集成redis

配置redis信息
spring:
redis:
host:
port: 6379
password:
database: 15
jedis:
pool:
#最大连接数
max-active: 100
#最大阻塞等待时间(负数表示没限制)
max-wait: 2000
#最大空闲
max-idle: 500
#最小空闲
min-idle: 8
#连接超时时间
timeout: 5000
配置redis连接池
package com.cloud.models.doctor.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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 lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**

  • RedisConfig

  • 功能描述:

  • @author liuqiang

  • @date 2019年12月20日
    */
    @Slf4j
    @Configuration
    @EnableCaching
    @EnableConfigurationProperties(RedisProperties.class)
    public class RedisConfig 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.jedis.pool.max-idle}")
    private int maxIdle;

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

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

    /**

    • 配置 redis 连接池
    • @return
      */
      @Bean
      public JedisPool redisPoolFactory(){
      JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
      jedisPoolConfig.setMaxIdle(maxIdle);
      jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
      return new JedisPool(jedisPoolConfig, host, port, timeout, password);
      }
      }

编写redis应用
package com.cloud.models.doctor.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.cloud.models.common.dto.ResCommonDto;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Slf4j
@Service
public class DoctorRedisServiceImpl implements DoctorRedisService {

@Autowired
JedisPool pool;

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

@Override
public ResCommonDto findByKey(String key) {
	Jedis jedis = null;
	ResCommonDto resCommonDto = new ResCommonDto();
	try{
		jedis = pool.getResource();
        jedis.select(database);
		jedis.get(key);
        resCommonDto.setMsg(jedis.get(key));
	}finally {
		if(null != jedis){
            jedis.close(); // 释放资源还给连接池
        }
	}
    return resCommonDto;
}    

@Override
public void addKey(String key,String value,String time) {
	log.error("addKey 接口调用  :   key  "+ key +"  value  "+ value);
	Jedis jedis = null;
    try{
        jedis = pool.getResource();
        jedis.select(database);
        if("".equals(time) || time == null) {
        	jedis.set(key,value);
        }else {
        	jedis.setex(key, Integer.valueOf(time), value);
        }
    }finally{
        if(null != jedis){
            jedis.close(); // 释放资源还给连接池
        }
    }
}

@Override
public void removeByKey(String key) {
	Jedis jedis = null;
    try{
        jedis = pool.getResource();
        jedis.select(database);
        jedis.del(key);
    }finally{
        if(null != jedis){
            jedis.close(); // 释放资源还给连接池
        }
    }

}    

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值