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;
}
}