feat():redis配置以及redisService

master
Saisai Liu 2024-06-04 15:12:24 +08:00
parent 7e14155c64
commit 020466a782
5 changed files with 288 additions and 1 deletions

12
pom.xml
View File

@ -75,6 +75,18 @@
<version>2.0.42</version> <version>2.0.42</version>
</dependency> </dependency>
<!-- excel工具 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<!-- redis依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- http调用框架 --> <!-- http调用框架 -->
<dependency> <dependency>
<groupId>com.dtflys.forest</groupId> <groupId>com.dtflys.forest</groupId>

View File

@ -13,6 +13,7 @@ import com.muyu.domain.resp.VehicleInstanceResp;
import com.muyu.service.PositionRouteService; import com.muyu.service.PositionRouteService;
import com.muyu.service.VehicleInstanceService; import com.muyu.service.VehicleInstanceService;
import com.muyu.utils.MD5Util; import com.muyu.utils.MD5Util;
import com.muyu.utils.RedisService;
import com.muyu.utils.ReflectUtils; import com.muyu.utils.ReflectUtils;
import com.muyu.vehicle.VehicleInstance; import com.muyu.vehicle.VehicleInstance;
import com.muyu.vehicle.api.ClientAdmin; import com.muyu.vehicle.api.ClientAdmin;
@ -46,6 +47,9 @@ public class VehicleInstanceServiceImpl implements VehicleInstanceService {
@Autowired @Autowired
private ClientAdmin clientAdmin; private ClientAdmin clientAdmin;
@Autowired
private RedisService redisService;
/** /**
* *
@ -130,6 +134,7 @@ public class VehicleInstanceServiceImpl implements VehicleInstanceService {
.build(); .build();
vehicleInstance.setMqttProperties(mqttProperties); vehicleInstance.setMqttProperties(mqttProperties);
vehicleInstance.initClient(); vehicleInstance.initClient();
redisService.setValue(vin, mqttServerModel.getBroker());
log.info("vin[{}],上线成功", vin); log.info("vin[{}],上线成功", vin);
} }

View File

@ -0,0 +1,266 @@
package com.muyu.utils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* spring redis
*
* @author muyu
**/
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
@Lazy
public class RedisService {
@Autowired
public RedisTemplate redisTemplate;
/**
* IntegerString
*
* @param key
* @param value
*/
public <T> void setCacheObject(final String key, final T value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* IntegerString
*
* @param key
* @param value
* @param timeout
* @param timeUnit
*/
public <T> void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/**
*
*
* @param key Redis
* @param timeout
* @return true=false=
*/
public boolean expire(final String key, final long timeout) {
return expire(key, timeout, TimeUnit.SECONDS);
}
/**
*
*
* @param key Redis
* @param timeout
* @param unit
* @return true=false=
*/
public boolean expire(final String key, final long timeout, final TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
/**
*
*
* @param key Redis
* @return
*/
public long getExpire(final String key) {
return redisTemplate.getExpire(key);
}
/**
* key
*
* @param key
* @return true false
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
*
*
* @param key
* @return
*/
public <T> T getCacheObject(final String key) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
/**
*
*
* @param key
*/
public boolean deleteObject(final String key) {
return redisTemplate.delete(key);
}
/**
*
*
* @param collection
* @return
*/
public boolean deleteObject(final Collection collection) {
return redisTemplate.delete(collection) > 0;
}
/**
* List
*
* @param key
* @param dataList List
* @return
*/
public <T> long setCacheList(final String key, final List<T> dataList) {
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
/**
* list
*
* @param key
* @return
*/
public <T> List<T> getCacheList(final String key) {
return redisTemplate.opsForList().range(key, 0, -1);
}
/**
* Set
*
* @param key
* @param dataSet
* @return
*/
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet) {
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
Iterator<T> it = dataSet.iterator();
while (it.hasNext()) {
setOperation.add(it.next());
}
return setOperation;
}
/**
* set
*
* @param key
* @return
*/
public <T> Set<T> getCacheSet(final String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* Map
*
* @param key
* @param dataMap
*/
public <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
/**
* Map
*
* @param key
* @return
*/
public <T> Map<String, T> getCacheMap(final String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* Hash
*
* @param key Redis
* @param hKey Hash
* @param value
*/
public <T> void setCacheMapValue(final String key, final String hKey, final T value) {
redisTemplate.opsForHash().put(key, hKey, value);
}
/**
* Hash
*
* @param key Redis
* @param hKey Hash
* @return Hash
*/
public <T> T getCacheMapValue(final String key, final String hKey) {
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
/**
* Hash
*
* @param key Redis
* @param hKeys Hash
* @return Hash
*/
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) {
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
/**
* Hash
*
* @param key Redis
* @param hKey Hash
* @return
*/
public boolean deleteCacheMapValue(final String key, final String hKey) {
return redisTemplate.opsForHash().delete(key, hKey) > 0;
}
/**
*
*
* @param pattern
* @return
*/
public Collection<String> keys(final String pattern) {
return redisTemplate.keys(pattern);
}
/**
*
*
* @param key
* @param t
* @param <V>
*/
public <V extends List<?>> void setCacheList(String key, T t) {
redisTemplate.opsForList().leftPush(String.valueOf(key), t);
}
public <T> long setCacheList(final String key, final T dataList) {
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
public void setValue(String vin, String broker) {
redisTemplate.opsForValue().set(vin,broker);
}
}

View File

@ -1,6 +1,10 @@
server: server:
port: 81 port: 81
spring: spring:
redis:
host: 43.142.100.73
port: 6379
password:
mvc: mvc:
static-path-pattern: /static/** static-path-pattern: /static/**

File diff suppressed because one or more lines are too long