新建分支

This commit is contained in:
19991905653
2024-04-09 09:29:59 +08:00
parent 95405fa625
commit 342d0d82e1
47 changed files with 2392 additions and 109 deletions

View File

@@ -15,5 +15,5 @@ public class RedisCst {
public static final String TABLE_CART = "TABLE:CART:";
public static final String PRODUCT = "PRODUCT:";
public static final String INTEGRAL_COIN_KEY = "";
public static final String INTEGRAL_COIN_KEY = "INTEGRAL:COIN:KEY";
}

View File

@@ -1,5 +1,7 @@
package com.chaozhanggui.system.cashierservice.redis;
import com.chaozhanggui.system.cashierservice.socket.AppWebSocketServer;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -7,6 +9,7 @@ import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.io.*;
import java.util.*;
/**
@@ -17,7 +20,7 @@ import java.util.*;
* @Description redis工具类
*/
@Component
public class RedisUtil {
public class RedisUtil{
// 成功标识
private static final int REDIS_SUCCESS = 1;
// 失败标识
@@ -480,4 +483,128 @@ public class RedisUtil {
}
return REDIS_FAILED+"";
}
public byte[] serialize(Object obj) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
return bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object deserialize(byte[] bytes) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
return ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
// public String serialize(Object list) {
// try {
// ObjectMapper mapper = new ObjectMapper();
// return mapper.writeValueAsString(list);
// } catch (Exception e) {
// e.printStackTrace();
// return null;
// }
// }
// public <T> List<AppWebSocketServer> deserializeJson(String json, Class<T> clazz) {
// try {
// ObjectMapper mapper = new ObjectMapper();
// return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, clazz));
// } catch (Exception e) {
// e.printStackTrace();
// return null;
// }
// }
public byte[] getHashAll(String key) {
Jedis jedis = null;
try {
// 从jedis池中获取一个jedis实例
jedis = pool.getResource();
if (database!=0) {
jedis.select(database);
}
byte[] serializedList = jedis.get(key.getBytes());
return serializedList;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放对象池即获取jedis实例使用后要将对象还回去
if (jedis != null) {
jedis.close();
}
}
return null;
}
public int saveHashAll(byte[] key,byte[] value) {
Jedis jedis = null;
try {
// 从jedis池中获取一个jedis实例
jedis = pool.getResource();
if (database!=0) {
jedis.select(database);
}
jedis.rpush(key,value);
return REDIS_SUCCESS;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放对象池即获取jedis实例使用后要将对象还回去
if (jedis != null) {
jedis.close();
}
}
return REDIS_FAILED;
}
public Set<String> getSet(String key ){
Jedis jedis = null ;
Set<String> set = null ;
try {
jedis = pool.getResource();
set = jedis.smembers(key);
}catch (Exception e) {
e.printStackTrace();
} finally {
// 释放对象池即获取jedis实例使用后要将对象还回去
if (jedis != null) {
jedis.close();
}
}
return set ;
}
/**
* 往set中添加数据
* @param key
* @param values
* @return
*/
public Long addSet(String key , String... values ){
Jedis jedis = null ;
try {
jedis = pool.getResource();
return jedis.sadd(key,values);
}catch (Exception e) {
e.printStackTrace();
} finally {
// 释放对象池即获取jedis实例使用后要将对象还回去
if (jedis != null) {
jedis.close();
}
}
return 0L ;
}
}