Jedis的使用
文章目录
-
- Jedis的使用
-
- 1、引入依赖
- 2、配置文件
- 3、配置类
- 4、工具类
1、引入依赖
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
dependency>
2、配置文件
spring:
redis:
database: 0
host: 127.0.0.1
port: 6379
password:
timeout: 5000
lettuce:
pool:
max-active: 20
max-idle: 20
min-idle: 0
max-wait: -1
3、配置类
package com.hxlinks.system.config;
import com.hxlinks.common.exception.CustomException;
import com.hxlinks.common.util.JedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@Slf4j
public class JedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.lettuce.pool.max-active}")
private int maxActive;
@Value("${spring.redis.lettuce.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.lettuce.pool.min-idle}")
private int minIdle;
@Bean
public JedisPool jedisPool() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setMaxTotal(maxActive);
jedisPoolConfig.setJmxEnabled(true);
JedisPool jedisPool;
if (StringUtils.isEmpty(password)) {
jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout);
} else {
jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
}
JedisUtils.setJedisPool(jedisPool);
try {
Jedis jedis = JedisUtils.getJedis();
if (jedis != null) {
log.info("---[Jedis]---连接成功---;host = " + host + " ---port = " + port);
} else {
throw new CustomException("---[Jedis]---连接失败---;host = " + host + " ---port = " + port);
}
JedisUtils.close(jedis);
} catch (Exception e) {
e.printStackTrace();
throw new CustomException("---[Jedis]---连接失败---;host = " + host + " ---port = " + port);
}
return jedisPool;
}
}
4、工具类
package com.hxlinks.common.util;
import com.alibaba.fastjson.JSON;
import com.hxlinks.common.dao.StationMapper;
import com.hxlinks.common.entity.RealTimeData;
import com.hxlinks.common.entity.Station;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Component
public class JedisUtils {
private static JedisPool jedisPool;
public static void setJedisPool(JedisPool jedisPool) {
JedisUtils.jedisPool = jedisPool;
}
public static Jedis getJedis() {
return jedisPool.getResource();
}
public static void close(Jedis jedis) {
try {
if (jedis != null) {
jedis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Resource
private StationMapper stationMapper;
private static StationMapper stationDao;
@PostConstruct
private void construct() {
JedisUtils.stationDao = this.stationMapper;
}
public static boolean set(String key, String value) {
Jedis jedis = getJedis();
if (jedis == null) {
return false;
}
try {
String result = jedis.set(key, value);
if ("OK".equals(result)) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
} finally {
close(jedis);
}
return false;
}
public static String get(String key) {
Jedis jedis = getJedis();
if (jedis == null) {
return null;
}
String result = null;
try {
result = jedis.get(key);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
} finally {
close(jedis);
}
return result;
}
public static boolean setSnapshotValues(List<RealTimeData> realTimeDataList) {
if (CollectionUtils.isEmpty(realTimeDataList)) {
return false;
}
Jedis jedis = getJedis();
if (jedis == null) {
return false;
}
String[] keysValues = new String[realTimeDataList.size() * 2];
for (int i = 0; i < realTimeDataList.size(); i++) {
keysValues[i * 2] = realTimeDataList.get(i).getName();
keysValues[(i * 2) + 1] = JSON.toJSONString(realTimeDataList.get(i));
}
try {
String result = jedis.mset(keysValues);
if ("OK".equals(result)) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
} finally {
close(jedis);
}
return false;
}
public static Map<String, RealTimeData> getSnapshotValues(Long companyId, List<String> keyList) {
if (CollectionUtils.isEmpty(keyList)) {
return null;
}
Jedis jedis = getJedis();
if (jedis == null) {
return null;
}
try {
Map<String, RealTimeData> rtnMap = new HashMap<>();
List<String> result = jedis.mget(keyList.toArray(new String[0]));
for (int i = 0; i < keyList.size(); i++) {
if (CollectionUtils.isEmpty(result)) {
rtnMap.put(keyList.get(i), null);
} else {
String jsonValue = result.get(i);
RealTimeData realTimeData = StringUtils.isNotEmpty(jsonValue) ? JSON.parseObject(jsonValue, RealTimeData.class) : null;
rtnMap.put(keyList.get(i), realTimeData);
}
}
return dealRealTimeDataPoint(companyId, keyList, rtnMap);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
return null;
} finally {
close(jedis);
}
}
public static Map<String, RealTimeData> dealRealTimeDataPoint(Long companyId, List<String> keyList, Map<String, RealTimeData> realTimeDataMap) {
List<Station> stationList = stationDao.getStationListByNameList(companyId, keyList);
mapFor:
for (Map.Entry<String, RealTimeData> dataEntry : realTimeDataMap.entrySet()) {
RealTimeData realTimeData = dataEntry.getValue();
if (realTimeData == null) {
continue;
}
for (Station station : stationList) {
if (dataEntry.getKey().equals(station.getKey())) {
realTimeData.setValue(TypeChangeUtils.parsePoint(Double.parseDouble(realTimeData.getValue()), StringUtils.isEmpty(station.getDecimalDigits()) ? 0 : Integer.parseInt(station.getDecimalDigits())));
continue mapFor;
}
}
realTimeData.setValue(TypeChangeUtils.parsePoint(Double.parseDouble(realTimeData.getValue()), 0));
}
return realTimeDataMap;
}
public static void publish(String channel, String message) {
Jedis jedis = getJedis();
if (jedis == null) {
return;
}
jedis.publish(channel, message);
}
public static boolean hmset(String key, Map<String, String> hash) {
Jedis jedis = getJedis();
if (jedis == null) {
return false;
}
try {
String result = jedis.hmset(key, hash);
if ("OK".equals(result)) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
} finally {
close(jedis);
}
return false;
}
public static List<String> hvals(String key) {
Jedis jedis = getJedis();
if (jedis == null) {
return null;
}
try {
return jedis.hvals(key);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
return null;
} finally {
close(jedis);
}
}
public static boolean hdel(String key, String field) {
Jedis jedis = getJedis();
if (jedis == null) {
return false;
}
try {
return jedis.hdel(key, field) > 0;
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
} finally {
close(jedis);
}
return false;
}
public static List<String> hmget(String key, String... fields) {
Jedis jedis = getJedis();
if (jedis == null) {
return null;
}
try {
return jedis.hmget(key, fields);
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
} finally {
close(jedis);
}
}
}