Implement caching
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
src/main/resources/static/*
|
||||
|
||||
HELP.md
|
||||
.gradle
|
||||
/build/
|
||||
|
||||
@@ -24,6 +24,7 @@ dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation "org.springframework.boot:spring-boot-starter-security"
|
||||
implementation "org.springframework.boot:spring-boot-starter-websocket"
|
||||
implementation 'org.springframework.boot:spring-boot-starter-cache'
|
||||
implementation 'org.springframework.session:spring-session-core'
|
||||
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.7'
|
||||
compile group: 'commons-io', name: 'commons-io', version: '2.6'
|
||||
|
||||
@@ -26,10 +26,10 @@ public class PacketController {
|
||||
}
|
||||
|
||||
@PostMapping("/{streamId}")
|
||||
public List<Packet> getPacketsForStream(@PathVariable long streamId, @RequestBody Pagination pagination) {
|
||||
public List<Packet> getPacketsForStream(@PathVariable long streamId) {
|
||||
final Optional<Stream> stream = streamService.find(streamId);
|
||||
if(stream.isPresent()) {
|
||||
return packetService.getPacketsForStream(pagination, stream.get());
|
||||
return packetService.getPacketsForStream(stream.get());
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@@ -9,8 +9,6 @@ import java.util.List;
|
||||
|
||||
public interface PacketRepository extends JpaRepository<Packet, Long> {
|
||||
|
||||
List<Packet> findAllByStreamAndIdGreaterThan(Stream stream, long packetId, Pageable pageable);
|
||||
|
||||
List<Packet> findAllByStreamAndIdLessThan(Stream stream, long packetId, Pageable pageable);
|
||||
List<Packet> findAllByStream(Stream stream);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package ru.serega6531.packmate.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.serega6531.packmate.model.Packet;
|
||||
import ru.serega6531.packmate.model.Pagination;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
import ru.serega6531.packmate.repository.PacketRepository;
|
||||
|
||||
@@ -21,14 +19,9 @@ public class PacketService {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
public List<Packet> getPacketsForStream(Pagination pagination, Stream stream) {
|
||||
PageRequest page = PageRequest.of(0, pagination.getPageSize(), pagination.getDirection(), "id");
|
||||
|
||||
if (pagination.getDirection() == Sort.Direction.ASC) { // более новые пакеты
|
||||
return repository.findAllByStreamAndIdGreaterThan(stream, pagination.getStartingFrom(), page);
|
||||
} else { // более старые пакеты
|
||||
return repository.findAllByStreamAndIdLessThan(stream, pagination.getStartingFrom(), page);
|
||||
}
|
||||
@Cacheable(value = "packets", key = "#stream.id")
|
||||
public List<Packet> getPacketsForStream(Stream stream) {
|
||||
return repository.findAllByStream(stream);
|
||||
}
|
||||
|
||||
public Packet save(Packet packet) {
|
||||
|
||||
@@ -2,6 +2,9 @@ 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;
|
||||
@@ -30,6 +33,7 @@ public class ServicesService {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Cacheable("services")
|
||||
public Optional<CtfService> findByPort(int port) {
|
||||
return repository.findById(port);
|
||||
}
|
||||
@@ -38,11 +42,13 @@ public class ServicesService {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
@CacheEvict("services")
|
||||
public void deleteByPort(int port) {
|
||||
log.info("Удален сервис на порту {}", port);
|
||||
repository.deleteById(port);
|
||||
}
|
||||
|
||||
@CachePut("services")
|
||||
public CtfService save(CtfService service) {
|
||||
log.info("Добавлен новый сервис {} на порту {}", service.getName(), service.getPort());
|
||||
return repository.save(service);
|
||||
|
||||
@@ -6,6 +6,8 @@ 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;
|
||||
@@ -225,10 +227,12 @@ 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.info("Создан стрим с id {}", saved.getId());
|
||||
} else {
|
||||
saved = repository.save(stream);
|
||||
@@ -237,6 +241,13 @@ 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);
|
||||
}
|
||||
@@ -248,14 +259,18 @@ public class StreamService {
|
||||
return "" + alphabet[hash % l] + alphabet[(hash / l) % l] + alphabet[(hash / (l * l)) % l];
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
@Transactional
|
||||
public void setFavorite(long id, boolean favorite) {
|
||||
@CachePut(value = "streams", key = "#id")
|
||||
public Stream setFavorite(long id, boolean favorite) {
|
||||
final Optional<Stream> streamOptional = repository.findById(id);
|
||||
if (streamOptional.isPresent()) {
|
||||
final Stream stream = streamOptional.get();
|
||||
stream.setFavorite(favorite);
|
||||
repository.save(stream);
|
||||
return repository.save(stream);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Stream> findFavorites(Pagination pagination) {
|
||||
|
||||
Reference in New Issue
Block a user