【二十二】redis之jedisPool封装(注解实现)

换成使用spring boot后,没有了spring的xml配置文件,改用@注解

以上一篇没有太大的区别,只是用配置文件还是用注解,用redis集群还是不用集群。

RedisPool.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @description: RedisPool简单使用
 **/
@Configuration
public class RedisPool {
    Logger logger = LoggerFactory.getLogger(RedisPool.class);

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

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

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

    @Bean
    public JedisPool redisPoolFactory() {
        logger.info("JedisPool注入成功!!");
        logger.info("redis地址:" + host + ":" + port);
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        //jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);

        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, auth);

        return jedisPool;
    }


}

RedisService.java


public interface RedisService {
    String setex(String key,String token,int seconds);
    String get(String key);
    long del(String key);
}

RedisServiceImpl.java


import com.base.util.RedisPool;
import com.base.util.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @description:
 **/
@Service
public class RedisServiceImpl implements RedisService {

    Logger logger = LoggerFactory.getLogger(RedisPool.class);

    @Autowired
    JedisPool jedisPool;

    @Override
    public String setex(String key, String token, int seconds) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.setex(key,seconds,token);

        } catch (Exception e) {
            logger.error("JedisUtil setex:",e);
        } finally {
            if (jedis != null) {
                try {
                    jedis.close();
                } catch (Exception e) {
                    logger.error("close redis conn fail : ",e);
                }
            }

        }
        return null;
    }

    @Override
    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.get(key);

        } catch (Exception e) {

            logger.error("JedisUtil get:",e);
        } finally {
            if (jedis != null) {
                try {
                    jedis.close();
                } catch (Exception e) {
                    logger.error("close redis conn fail : ",e);
                }
            }
        }
        return  null;
    }

    @Override
    public long del(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.del(key);
        } catch (Exception e) {
            logger.error("JedisUtil del:",e);
        } finally {
            if (jedis != null) {
                try {
                    jedis.close();
                } catch (Exception e) {
                    logger.error("close redis conn fail : ",e);
                }
            }
        }
        return 0;
    }
}

application.yml

server:
  port: 8080
  context-path: /sid

redis:
  host: 192.168.2.240
  auth: 123123
  port: 6379
  maxIdle: 200
  maxTotal: 300
  timeout: 3000

使用

@Autowired
RedisService redisService ;

redisService.setex("redis:key","testvalue",5000);

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值