Browse Source

/*增加redis模块*/

master
htjcAdmin 5 months ago
parent
commit
d2a60136ca
4 changed files with 233 additions and 0 deletions
  1. +4
    -0
      src/main/java/com/inspect/simulator/service/impl/AlgorithmServiceImpl.java
  2. +49
    -0
      src/main/java/com/inspect/simulator/utils/redis/FastJson2JsonRedisSerializer.java
  3. +44
    -0
      src/main/java/com/inspect/simulator/utils/redis/RedisConfig.java
  4. +136
    -0
      src/main/java/com/inspect/simulator/utils/redis/RedisService.java

+ 4
- 0
src/main/java/com/inspect/simulator/service/impl/AlgorithmServiceImpl.java View File

@ -19,6 +19,7 @@ import com.inspect.simulator.mapper.PatrolPresetPosMapper;
import com.inspect.simulator.service.AlgorithmService;
import com.inspect.simulator.service.remote.AnalysisRemoteService;
import com.inspect.simulator.utils.HttpClientUtils;
import com.inspect.simulator.utils.redis.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@ -52,6 +53,9 @@ public class AlgorithmServiceImpl implements AlgorithmService {
@Resource
private HikVisionServiceImpl hikVisionService;
@Resource
private RedisService redisService;
@Override
public String filterPicAnalyse(String analyseRequestStr) {
log.info(Color.MAGENTA + "[FILTER] filterPicAnalyse: analyseRequestStr={}" + Color.END, analyseRequestStr);


+ 49
- 0
src/main/java/com/inspect/simulator/utils/redis/FastJson2JsonRedisSerializer.java View File

@ -0,0 +1,49 @@
package com.inspect.simulator.utils.redis;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.util.Assert;
import java.nio.charset.Charset;
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {
private ObjectMapper objectMapper = new ObjectMapper();
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private Class<T> clazz;
public FastJson2JsonRedisSerializer(Class<T> clazz) {
this.clazz = clazz;
}
public byte[] serialize(T t) throws SerializationException {
return t == null?new byte[0]:JSON.toJSONString(t, new SerializerFeature[]{SerializerFeature.WriteClassName}).getBytes(DEFAULT_CHARSET);
}
public T deserialize(byte[] bytes) throws SerializationException {
if(bytes != null && bytes.length > 0) {
String str = new String(bytes, DEFAULT_CHARSET);
return JSON.parseObject(str, this.clazz);
} else {
return null;
}
}
public void setObjectMapper(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "'objectMapper' must not be null");
this.objectMapper = objectMapper;
}
protected JavaType getJavaType(Class<?> clazz) {
return TypeFactory.defaultInstance().constructType(clazz);
}
static {
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
}
}

+ 44
- 0
src/main/java/com/inspect/simulator/utils/redis/RedisConfig.java View File

@ -0,0 +1,44 @@
package com.inspect.simulator.utils.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(connectionFactory);
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, DefaultTyping.NON_FINAL, As.PROPERTY);
serializer.setObjectMapper(mapper);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(serializer);
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}
}

+ 136
- 0
src/main/java/com/inspect/simulator/utils/redis/RedisService.java View File

@ -0,0 +1,136 @@
package com.inspect.simulator.utils.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
@Component
public class RedisService {
private final Logger logger = LoggerFactory.getLogger(RedisService.class);
public final RedisTemplate redisTemplate;
public RedisService(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public <T> void setCacheObject(String key, T value) {
redisTemplate.opsForValue().set(key, value);
}
public <T> void setCacheObjectOfTask(String key, String taskCode, T value) {
String redisKey = key + taskCode;
redisTemplate.opsForValue().set(redisKey, value);
logger.info("RedisService.setCacheObjectOfTask: {}", redisKey);
}
public <T> void setCacheObject(String key, T value, Long timeout, TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
public <T> void setCacheObjectOfTask(String key, String taskCode, T value, Long timeout, TimeUnit timeUnit) {
String redisKey = key + taskCode;
redisTemplate.opsForValue().set(redisKey, value, timeout, timeUnit);
logger.info("RedisService.setCacheObjectOfTaskWithTimeout: {}", redisKey);
}
public boolean expire(String key, long timeout) {
return expire(key, timeout, TimeUnit.SECONDS);
}
public boolean expire(String key, long timeout, TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
public long getExpire(String key) {
return redisTemplate.getExpire(key);
}
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
public <T> T getCacheObject(String key) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
public <T> T getCacheObjectOfTask(String key, String taskCode) {
String redisKey = key + taskCode;
ValueOperations<String, T> operation = redisTemplate.opsForValue();
logger.info("RedisService.getCacheObjectOfTask: {}", redisKey);
return operation.get(redisKey);
}
public boolean deleteObject(String key) {
return redisTemplate.delete(key).booleanValue();
}
public boolean deleteObjectOfTask(String key, String taskCode) {
String redisKey = key + taskCode;
logger.info("RedisService.deleteObjectOfTask: {}", redisKey);
return redisTemplate.delete(redisKey).booleanValue();
}
public long deleteObject(Collection collection) {
return redisTemplate.delete(collection).longValue();
}
public <T> long setCacheList(String key, List<T> dataList) {
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0L : count;
}
public <T> List<T> getCacheList(String key) {
return redisTemplate.opsForList().range(key, 0L, -1L);
}
public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet) {
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
Iterator it = dataSet.iterator();
while(it.hasNext()) {
setOperation.add((T[]) new Object[]{it.next()});
}
return setOperation;
}
public <T> Set<T> getCacheSet(String key) {
return redisTemplate.opsForSet().members(key);
}
public <T> void setCacheMap(String key, Map<String, T> dataMap) {
if(dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
public <T> Map<String, T> getCacheMap(String key) {
return redisTemplate.opsForHash().entries(key);
}
public <T> void setCacheMapValue(String key, String hKey, T value) {
redisTemplate.opsForHash().put(key, hKey, value);
}
public <T> T getCacheMapValue(String key, String hKey) {
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
public <T> List<T> getMultiCacheMapValue(String key, Collection<Object> hKeys) {
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
public Collection<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
}

Loading…
Cancel
Save