jedis单例配置

package com.transsnet.palmpay.util;

import io.swagger.annotations.Scope;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.BitSet;
import java.util.List;

@Component
@Data
public class RedisUtils {

    @Autowired
    private JedisPool jedisPool;
    


    /**
     * 获取一个Jedis实例
     */
    @PostConstruct
    public Jedis getInstance() {
        Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
                jedis.select(0); // 选择存储库,单机版默认为db(0)
            } catch (Exception e) {
//                log.error("[Redis] jedisPool.getResource() error:{}",e);
            }
        return jedis;
    }

    /**
     * 回收Jedis实例
     */
    public void closeJedis(Jedis jedis) {
        if (jedis != null && jedisPool != null) {
//            jedisPool.close();
            jedis.close();
        }
    }

    /**
     * java对象序列化后存入redis
     */
    public byte[] objectSerialize(Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            return baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * byte反序列化为java对象
     */
//    public Object deObjectSerialize(byte[] bytes) {
//        ObjectIutputStream ois = null;
//        ByteArrayIutputStream bais = null;
//        try {
//            bais = new ByteArrayIutputStream(bytes);
//            ois = new ObjectIutputStream(bais);
//            return ois.readObject();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (ois != null) {
//                    ois.close();
//                }
//                if (bais != null) {
//                    bais.close();
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        return null;
//    }



    // 将redis的bitmap转换为java 的bitset
    public BitSet convertRedisBitmapToJava(byte[] redisBitmapData) {
        int len = redisBitmapData.length;
        BitSet bitSet = new BitSet();
        // 每个 byte 8位, 所以整个bitmap 的长度为 len * 8
        for (int i = 0; i < len * 8; i++) {
            byte currentSegment = redisBitmapData[i / 8];
            if(currentSegment == 0) {
                continue;
            }
            if((currentSegment & (1 << (7 - (i % 8) ) ) ) != 0 ) {
                bitSet.set(i);
            }
        }
        return bitSet;
    }

    // 生成redis的bitmap数据
    public byte[] genRedisBitmap(List<Integer> items) {
        BitSet bitSet = new BitSet();
        // 2 55 133
        for (int k : items) {
            bitSet.set(k);
        }
        byte[] targetBitmap = bitSet.toByteArray();
        convertJavaToRedisBitmap(targetBitmap);
        return targetBitmap;
    }

    // 将java中的字节数组转换为redis的bitmap数据形式
    public void convertJavaToRedisBitmap(byte[] bytes) {
        int len = bytes.length;
        for (int i = 0; i < len; i++) {
            byte b1 = bytes[i];
            if(b1 == 0) {
                continue;
            }
            byte transByte = 0;
            for (byte j = 0; j < 8; j++) {
                transByte |= (b1 & (1 << j)) >> j << (7 -j);
            }
            bytes[i] = transByte;
        }
    }

}

@Configuration
@EnableCaching
@Slf4j
public class RedisConfig extends CachingConfigurerSupport {

@Bean
    public JedisPoolConfiggetJedisCluster() {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//        jedisPoolConfig.setMaxTotal(maxTotal);
//        jedisPoolConfig.setMaxIdle(maxIdle);
//        jedisPoolConfig.setBlockWhenExhausted(true);
//        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
//        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, null);
//        log.info("注册ReidsPool成功:redis地址 {} ,端口号 {} ", host, port);
//        return jedisPool;
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值