Skip to content

Commit

Permalink
还是得接着修
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-chu committed Jan 17, 2025
1 parent a66f4a7 commit e4d7522
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@

import com.ghostchu.btn.sparkle.module.torrent.internal.Torrent;
import com.ghostchu.btn.sparkle.module.torrent.internal.TorrentRepository;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

@Service
public class TorrentService {
private final Cache<String, Torrent> torrentCache = CacheBuilder
.newBuilder()
.concurrencyLevel(20)
.expireAfterAccess(5, TimeUnit.MINUTES)
.maximumSize(1000)
.build();
private final TorrentRepository torrentRepository;

public TorrentService(TorrentRepository torrentRepository) {
Expand All @@ -19,13 +29,15 @@ public TorrentService(TorrentRepository torrentRepository) {
@Transactional(propagation = Propagation.NOT_SUPPORTED)
//@Cacheable(value = "torrent#600000", key = "#torrentIdentifier+'-'+#torrentSize")
public Torrent createOrGetTorrent(String torrentIdentifier, long torrentSize, Boolean isPrivate) {
Torrent t;
var torrentOptional = torrentRepository.findByIdentifier(torrentIdentifier);
if (torrentOptional.isEmpty()) {
t = new Torrent(null, torrentIdentifier, torrentSize, isPrivate);
t = torrentRepository.save(t);
} else {
t = torrentOptional.get();
var t = torrentCache.getIfPresent(torrentIdentifier + "@" + torrentSize);
if (t == null) {
var torrentOptional = torrentRepository.findByIdentifier(torrentIdentifier);
if (torrentOptional.isPresent()) {
t = torrentOptional.get();
} else {
t = new Torrent(null, torrentIdentifier, torrentSize, isPrivate);
t = torrentRepository.save(t);
}
}
if (t.getSize() == -1 && torrentSize != -1) {
t.setSize(torrentSize);
Expand All @@ -35,6 +47,7 @@ public Torrent createOrGetTorrent(String torrentIdentifier, long torrentSize, Bo
t.setPrivateTorrent(isPrivate);
t = torrentRepository.save(t);
}
torrentCache.put(torrentIdentifier + "@" + torrentSize, t);
return t;
}

Expand Down

0 comments on commit e4d7522

Please sign in to comment.