Изменено кэширование
This commit is contained in:
@@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
import ru.serega6531.packmate.model.Pattern;
|
||||
import ru.serega6531.packmate.service.PatternService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/pattern/")
|
||||
@@ -19,7 +19,7 @@ public class PatternController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<Pattern> getPatterns() {
|
||||
public Collection<Pattern> getPatterns() {
|
||||
return service.findAll();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
import ru.serega6531.packmate.model.CtfService;
|
||||
import ru.serega6531.packmate.service.ServicesService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/service/")
|
||||
@@ -19,7 +19,7 @@ public class ServiceController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<CtfService> getServices() {
|
||||
public Collection<CtfService> getServices() {
|
||||
return service.findAll();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package ru.serega6531.packmate.repository;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.serega6531.packmate.model.Packet;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package ru.serega6531.packmate.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.serega6531.packmate.model.Packet;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
@@ -19,13 +18,12 @@ public class PacketService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Cacheable(value = "packets", key = "#stream.id")
|
||||
public List<Packet> getPacketsForStream(Stream stream) {
|
||||
return repository.findAllByStream(stream);
|
||||
}
|
||||
|
||||
public Packet save(Packet packet) {
|
||||
return repository.save(packet);
|
||||
public List<Packet> saveAll(List<Packet> packets) {
|
||||
return repository.saveAll(packets);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,10 +11,7 @@ import ru.serega6531.packmate.model.PatternType;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
import ru.serega6531.packmate.repository.PatternRepository;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@@ -24,28 +21,33 @@ public class PatternService {
|
||||
private final PatternRepository repository;
|
||||
private final StreamService streamService;
|
||||
|
||||
private Map<String, java.util.regex.Pattern> compiledPatterns = new HashMap<>();
|
||||
private final Map<Integer, Pattern> patterns = new HashMap<>();
|
||||
private final Map<String, java.util.regex.Pattern> compiledPatterns = new HashMap<>();
|
||||
|
||||
@Autowired
|
||||
public PatternService(PatternRepository repository, @Lazy StreamService streamService) {
|
||||
this.repository = repository;
|
||||
this.streamService = streamService;
|
||||
repository.findAll().forEach(p -> patterns.put(p.getId(), p));
|
||||
log.info("Loaded {} patterns", patterns.size());
|
||||
}
|
||||
|
||||
public List<Pattern> findAll() {
|
||||
return repository.findAll();
|
||||
public Collection<Pattern> findAll() {
|
||||
return patterns.values();
|
||||
}
|
||||
|
||||
public List<Pattern> findMatching(byte[] bytes, boolean incoming) {
|
||||
String content = new String(bytes);
|
||||
|
||||
return repository.findAllByTypeEqualsOrTypeEquals(incoming ? PatternType.INPUT : PatternType.OUTPUT, PatternType.BOTH).stream()
|
||||
return patterns.values().stream()
|
||||
.filter(p -> p.getType() == (incoming ? PatternType.INPUT : PatternType.OUTPUT)
|
||||
|| p.getType() == PatternType.BOTH)
|
||||
.filter(pattern -> matches(pattern, content))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private boolean matches(Pattern pattern, String content) {
|
||||
if(pattern.isRegex()) {
|
||||
if (pattern.isRegex()) {
|
||||
final java.util.regex.Pattern regex = compilePattern(pattern);
|
||||
return regex.matcher(content).find();
|
||||
} else {
|
||||
@@ -56,7 +58,7 @@ public class PatternService {
|
||||
@Transactional
|
||||
public void deleteById(int id) {
|
||||
final Optional<Pattern> optional = repository.findById(id);
|
||||
if(optional.isPresent()) {
|
||||
if (optional.isPresent()) {
|
||||
final Pattern pattern = optional.get();
|
||||
log.info("Удален паттерн {} со значением {}", pattern.getName(), pattern.getValue());
|
||||
|
||||
@@ -66,12 +68,15 @@ public class PatternService {
|
||||
}
|
||||
|
||||
pattern.getMatchedStreams().clear();
|
||||
patterns.remove(pattern.getId());
|
||||
compiledPatterns.remove(pattern.getValue());
|
||||
repository.delete(pattern);
|
||||
}
|
||||
}
|
||||
|
||||
public Pattern save(Pattern pattern) {
|
||||
log.info("Добавлен новый паттерн {} со значением {}", pattern.getName(), pattern.getValue());
|
||||
patterns.put(pattern.getId(), pattern);
|
||||
return repository.save(pattern);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,14 +2,13 @@ package ru.serega6531.packmate.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.serega6531.packmate.model.CtfService;
|
||||
import ru.serega6531.packmate.repository.ServiceRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@@ -17,40 +16,42 @@ import java.util.Optional;
|
||||
public class ServicesService {
|
||||
|
||||
private final ServiceRepository repository;
|
||||
private final Map<Integer, CtfService> services = new HashMap<>();
|
||||
|
||||
@Autowired
|
||||
public ServicesService(ServiceRepository repository) {
|
||||
this.repository = repository;
|
||||
repository.findAll().forEach(s -> services.put(s.getPort(), s));
|
||||
log.info("Loaded {} services", services.size());
|
||||
}
|
||||
|
||||
public Optional<CtfService> findService(String localIp, String firstIp, int firstPort, String secondIp, int secondPort) {
|
||||
if(firstIp.equals(localIp)) {
|
||||
if (firstIp.equals(localIp)) {
|
||||
return findByPort(firstPort);
|
||||
} else if(secondIp.equals(localIp)) {
|
||||
} else if (secondIp.equals(localIp)) {
|
||||
return findByPort(secondPort);
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Cacheable("services")
|
||||
public Optional<CtfService> findByPort(int port) {
|
||||
return repository.findById(port);
|
||||
return Optional.ofNullable(services.get(port));
|
||||
}
|
||||
|
||||
public List<CtfService> findAll() {
|
||||
return repository.findAll();
|
||||
public Collection<CtfService> findAll() {
|
||||
return services.values();
|
||||
}
|
||||
|
||||
@CacheEvict("services")
|
||||
public void deleteByPort(int port) {
|
||||
log.info("Удален сервис на порту {}", port);
|
||||
services.remove(port);
|
||||
repository.deleteById(port);
|
||||
}
|
||||
|
||||
@CachePut("services")
|
||||
public CtfService save(CtfService service) {
|
||||
log.info("Добавлен новый сервис {} на порту {}", service.getName(), service.getPort());
|
||||
services.put(service.getPort(), service);
|
||||
return repository.save(service);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,6 @@ import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cache.annotation.CachePut;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -176,17 +174,15 @@ public class StreamService {
|
||||
|
||||
Stream savedStream = save(stream);
|
||||
|
||||
List<ru.serega6531.packmate.model.Packet> savedPackets = new ArrayList<>();
|
||||
Set<Pattern> matches = new HashSet<>();
|
||||
|
||||
for (ru.serega6531.packmate.model.Packet packet : packets) {
|
||||
packet.setStream(savedStream);
|
||||
savedPackets.add(packetService.save(packet));
|
||||
matches.addAll(patternService.findMatching(packet.getContent(), packet.isIncoming()));
|
||||
}
|
||||
|
||||
savedStream.setFoundPatterns(new ArrayList<>(matches));
|
||||
savedStream.setPackets(savedPackets);
|
||||
savedStream.setPackets(packetService.saveAll(packets));
|
||||
savedStream = save(savedStream);
|
||||
|
||||
subscriptionService.broadcastNewStream(savedStream);
|
||||
@@ -227,12 +223,10 @@ public class StreamService {
|
||||
return null;
|
||||
}
|
||||
|
||||
@CachePut(value = "streams", key = "#stream.id")
|
||||
public Stream save(Stream stream) {
|
||||
Stream saved;
|
||||
if (stream.getId() == null) {
|
||||
saved = repository.save(stream);
|
||||
cachePackets(saved);
|
||||
log.debug("Создан стрим с id {}", saved.getId());
|
||||
} else {
|
||||
saved = repository.save(stream);
|
||||
@@ -241,13 +235,6 @@ public class StreamService {
|
||||
return saved;
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
@CachePut(value = "packets", key = "#stream.id")
|
||||
public List<Packet> cachePackets(Stream stream) {
|
||||
return stream.getPackets();
|
||||
}
|
||||
|
||||
@Cacheable("streams")
|
||||
public Optional<Stream> find(long id) {
|
||||
return repository.findById(id);
|
||||
}
|
||||
@@ -261,7 +248,6 @@ public class StreamService {
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
@Transactional
|
||||
@CachePut(value = "streams", key = "#id")
|
||||
public Stream setFavorite(long id, boolean favorite) {
|
||||
final Optional<Stream> streamOptional = repository.findById(id);
|
||||
if (streamOptional.isPresent()) {
|
||||
|
||||
Reference in New Issue
Block a user