Add torrent caching.
This commit is contained in:
parent
9a7aefaa71
commit
7872836ae7
1 changed files with 135 additions and 56 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using RdtClient.Data.Models.Data;
|
using RdtClient.Data.Models.Data;
|
||||||
|
|
@ -21,6 +22,9 @@ namespace RdtClient.Data.Data
|
||||||
|
|
||||||
public class TorrentData : ITorrentData
|
public class TorrentData : ITorrentData
|
||||||
{
|
{
|
||||||
|
private static IList<Torrent> _torrentCache;
|
||||||
|
private static readonly SemaphoreSlim _torrentCacheLock = new SemaphoreSlim(1);
|
||||||
|
|
||||||
private readonly DataContext _dataContext;
|
private readonly DataContext _dataContext;
|
||||||
|
|
||||||
public TorrentData(DataContext dataContext)
|
public TorrentData(DataContext dataContext)
|
||||||
|
|
@ -30,12 +34,32 @@ namespace RdtClient.Data.Data
|
||||||
|
|
||||||
public async Task<IList<Torrent>> Get()
|
public async Task<IList<Torrent>> Get()
|
||||||
{
|
{
|
||||||
var results = await _dataContext.Torrents
|
await _torrentCacheLock.WaitAsync();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (_torrentCache == null)
|
||||||
|
{
|
||||||
|
_torrentCache = await _dataContext.Torrents
|
||||||
.AsNoTracking()
|
.AsNoTracking()
|
||||||
.Include(m => m.Downloads)
|
.Include(m => m.Downloads)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
return results.OrderByDescending(m => m.Added).ToList();
|
foreach (var torrent in _torrentCache)
|
||||||
|
{
|
||||||
|
foreach (var download in torrent.Downloads)
|
||||||
|
{
|
||||||
|
download.Torrent = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return _torrentCache.OrderByDescending(m => m.Added).ToList();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_torrentCacheLock.Release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Torrent> GetById(Guid torrentId)
|
public async Task<Torrent> GetById(Guid torrentId)
|
||||||
|
|
@ -79,6 +103,10 @@ namespace RdtClient.Data.Data
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Torrent> Add(String realDebridId, String hash, String category, Boolean autoDelete, String fileOrMagnetContents, Boolean isFile)
|
public async Task<Torrent> Add(String realDebridId, String hash, String category, Boolean autoDelete, String fileOrMagnetContents, Boolean isFile)
|
||||||
|
{
|
||||||
|
await _torrentCacheLock.WaitAsync();
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var torrent = new Torrent
|
var torrent = new Torrent
|
||||||
{
|
{
|
||||||
|
|
@ -96,10 +124,21 @@ namespace RdtClient.Data.Data
|
||||||
|
|
||||||
await _dataContext.SaveChangesAsync();
|
await _dataContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
_torrentCache = null;
|
||||||
|
|
||||||
return torrent;
|
return torrent;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_torrentCacheLock.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async Task UpdateRdData(Torrent torrent)
|
public async Task UpdateRdData(Torrent torrent)
|
||||||
|
{
|
||||||
|
await _torrentCacheLock.WaitAsync();
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var dbTorrent = await _dataContext.Torrents.FirstOrDefaultAsync(m => m.TorrentId == torrent.TorrentId);
|
var dbTorrent = await _dataContext.Torrents.FirstOrDefaultAsync(m => m.TorrentId == torrent.TorrentId);
|
||||||
|
|
||||||
|
|
@ -126,9 +165,20 @@ namespace RdtClient.Data.Data
|
||||||
}
|
}
|
||||||
|
|
||||||
await _dataContext.SaveChangesAsync();
|
await _dataContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
_torrentCache = null;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_torrentCacheLock.Release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateCategory(Guid torrentId, String category)
|
public async Task UpdateCategory(Guid torrentId, String category)
|
||||||
|
{
|
||||||
|
await _torrentCacheLock.WaitAsync();
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var dbTorrent = await _dataContext.Torrents.FirstOrDefaultAsync(m => m.TorrentId == torrentId);
|
var dbTorrent = await _dataContext.Torrents.FirstOrDefaultAsync(m => m.TorrentId == torrentId);
|
||||||
|
|
||||||
|
|
@ -140,18 +190,40 @@ namespace RdtClient.Data.Data
|
||||||
dbTorrent.Category = category;
|
dbTorrent.Category = category;
|
||||||
|
|
||||||
await _dataContext.SaveChangesAsync();
|
await _dataContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
_torrentCache = null;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_torrentCacheLock.Release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task UpdateComplete(Guid torrentId, DateTimeOffset? datetime)
|
public async Task UpdateComplete(Guid torrentId, DateTimeOffset? datetime)
|
||||||
|
{
|
||||||
|
await _torrentCacheLock.WaitAsync();
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var dbTorrent = await _dataContext.Torrents.FirstOrDefaultAsync(m => m.TorrentId == torrentId);
|
var dbTorrent = await _dataContext.Torrents.FirstOrDefaultAsync(m => m.TorrentId == torrentId);
|
||||||
|
|
||||||
dbTorrent.Completed = datetime;
|
dbTorrent.Completed = datetime;
|
||||||
|
|
||||||
await _dataContext.SaveChangesAsync();
|
await _dataContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
_torrentCache = null;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_torrentCacheLock.Release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Delete(Guid torrentId)
|
public async Task Delete(Guid torrentId)
|
||||||
|
{
|
||||||
|
await _torrentCacheLock.WaitAsync();
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
var dbTorrent = await _dataContext.Torrents.FirstOrDefaultAsync(m => m.TorrentId == torrentId);
|
var dbTorrent = await _dataContext.Torrents.FirstOrDefaultAsync(m => m.TorrentId == torrentId);
|
||||||
|
|
||||||
|
|
@ -163,6 +235,13 @@ namespace RdtClient.Data.Data
|
||||||
_dataContext.Torrents.Remove(dbTorrent);
|
_dataContext.Torrents.Remove(dbTorrent);
|
||||||
|
|
||||||
await _dataContext.SaveChangesAsync();
|
await _dataContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
_torrentCache = null;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_torrentCacheLock.Release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue