spring boot整合redis

引入jar包

<!--使用jedis -->
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

 

直接代码奉上:

package com.qudaowuyou.vote.util.cache;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Set;

/**
 * @Author zbf
 * @DATA 2019/11/4 17:58
 * @ClassIntroduction
 */
@Component
@Slf4j
public class RedisCache {

    @Value("${redis_host}")
    private String host="47.110.158.207";
    @Value("${redis_port}")
    private int port=6379;
    @Value("${redis_password}")
    private String passWord="";
    private JedisPool pool;

    @PostConstruct
    public void init() {
        JedisPoolConfig config = new JedisPoolConfig();
        // 最大空闲连接数, 应用自己评估,不要超过ApsaraDB for Redis每个实例最大的连接数
        config.setMaxIdle(100);
        // 最大连接数, 应用自己评估,不要超过ApsaraDB for Redis每个实例最大的连接数
        config.setMaxTotal(300);
        config.setMaxWaitMillis(1000);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        pool =  new JedisPool(config, host, Integer.valueOf(port), 3000, passWord);
        try {
            pool.getResource();
            log.info("redis pool init success" + pool);
        } catch (Exception e) {
            log.info("redis get resource error", e);
        }
    }

    public void destory() {
        pool.close();
    }

    protected Jedis getResource() {
        return pool.getResource();
    }

    protected void returnResource(Jedis jedis) {
        jedis.close();
    }

    public <T> void innerPut(String key, T value) {
        innerPut(key, value, 180);
    }

    public <T> void innerPut(String key, T value, int seconds) {

        if (value == null) {
            return;
        }
        byte[] data = stringToByte(JSON.toJSONString(value));
        Jedis resource = getResource();
        try {
            byte[] keyBytes = key.getBytes();
            resource.set(keyBytes, data);
            resource.expire(keyBytes, seconds);
        } finally {
            resource.close();
        }
    }


    public String simpleGet(String key) {

        String value = null;
        Jedis resource = getResource();
        try {
            byte[] data = resource.get(stringToByte(key));
            if (data == null) {
                return null;
            }
            value = byteToString(data);
        } finally {
            resource.close();
        }
        return value;
    }

    private String byteToString(byte[] data) {
        try {
            return new String(data, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    public void simplePutLast(String key, String value) {

        if (value == null) {
            return;
        }
        byte[] data = stringToByte(value);
        Jedis resource = getResource();
        try {
            byte[] keyBytes = stringToByte(key);
            resource.set(keyBytes, data);
        } finally {
            resource.close();
        }
    }

    public <T> void simplePutLastBean(String key, T value) {
        if (value == null) {
            return;
        }
        byte[] data = stringToByte(JSON.toJSONString(value));
        Jedis resource = getResource();
        try {
            byte[] keyBytes = key.getBytes();
            resource.set(keyBytes, data);
        } finally {
            resource.close();
        }
    }




    public void simplePut(String key, String value, int seconds) {

        if (value == null) {
            return;
        }

        byte[] data = stringToByte(value);
        Jedis resource = getResource();
        try {
            byte[] keyBytes = stringToByte(key);
            resource.set(keyBytes, data);
            resource.expire(keyBytes, seconds);
        } finally {
            resource.close();
        }
    }

    private byte[] stringToByte(String value) {
        try {
            return value.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    public <T> T innerGet(String key, Class<T> clazz) {

        Jedis resource = getResource();
        try {
            byte[] data = resource.get(key.getBytes());
            if (data == null) {
                return null;
            }
            T value = JSON.parseObject(byteToString(data),clazz);
            return value;
        } finally {
            resource.close();
        }
    }

    public void innerRemove(String key) {

        Jedis resource = getResource();
        resource.del(key.getBytes());
        resource.close();
    }

    public void batchDel(String pre_str) {
        Jedis resource = getResource();
        Set<String> set = resource.keys(pre_str + "*");
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            String keyStr = it.next();
            resource.del(keyStr);
        }
    }

    public void setHost(String host) {
        this.host = host;
    }

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



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值