mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-10 15:32:37 -07:00
Made more async goodness
This commit is contained in:
parent
8550cc4c5e
commit
166e0f81cf
7 changed files with 181 additions and 30 deletions
|
@ -26,6 +26,8 @@
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using PlexRequests.Store;
|
using PlexRequests.Store;
|
||||||
|
|
||||||
namespace PlexRequests.Core
|
namespace PlexRequests.Core
|
||||||
|
@ -33,13 +35,18 @@ namespace PlexRequests.Core
|
||||||
public interface IRequestService
|
public interface IRequestService
|
||||||
{
|
{
|
||||||
long AddRequest(RequestedModel model);
|
long AddRequest(RequestedModel model);
|
||||||
|
Task<int> AddRequestAsync(RequestedModel model);
|
||||||
RequestedModel CheckRequest(int providerId);
|
RequestedModel CheckRequest(int providerId);
|
||||||
RequestedModel CheckRequest(string musicId);
|
RequestedModel CheckRequest(string musicId);
|
||||||
|
|
||||||
void DeleteRequest(RequestedModel request);
|
void DeleteRequest(RequestedModel request);
|
||||||
|
Task DeleteRequestAsync(RequestedModel request);
|
||||||
bool UpdateRequest(RequestedModel model);
|
bool UpdateRequest(RequestedModel model);
|
||||||
|
Task<bool> UpdateRequestAsync(RequestedModel model);
|
||||||
RequestedModel Get(int id);
|
RequestedModel Get(int id);
|
||||||
|
Task<RequestedModel> GetAsync(int id);
|
||||||
IEnumerable<RequestedModel> GetAll();
|
IEnumerable<RequestedModel> GetAll();
|
||||||
|
Task<IEnumerable<RequestedModel>> GetAllAsync();
|
||||||
bool BatchUpdate(List<RequestedModel> model);
|
bool BatchUpdate(List<RequestedModel> model);
|
||||||
bool BatchDelete(List<RequestedModel> model);
|
bool BatchDelete(List<RequestedModel> model);
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
@ -57,6 +58,19 @@ namespace PlexRequests.Core
|
||||||
return result ? id : -1;
|
return result ? id : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<int> AddRequestAsync(RequestedModel model)
|
||||||
|
{
|
||||||
|
var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId };
|
||||||
|
var id = await Repo.InsertAsync(entity);
|
||||||
|
|
||||||
|
model.Id = id;
|
||||||
|
|
||||||
|
entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = id, MusicId = model.MusicBrainzId };
|
||||||
|
var result = await Repo.UpdateAsync(entity);
|
||||||
|
|
||||||
|
return result ? id : -1;
|
||||||
|
}
|
||||||
|
|
||||||
public RequestedModel CheckRequest(int providerId)
|
public RequestedModel CheckRequest(int providerId)
|
||||||
{
|
{
|
||||||
var blobs = Repo.GetAll();
|
var blobs = Repo.GetAll();
|
||||||
|
@ -77,12 +91,24 @@ namespace PlexRequests.Core
|
||||||
Repo.Delete(blob);
|
Repo.Delete(blob);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task DeleteRequestAsync(RequestedModel request)
|
||||||
|
{
|
||||||
|
var blob = await Repo.GetAsync(request.Id);
|
||||||
|
await Repo.DeleteAsync(blob);
|
||||||
|
}
|
||||||
|
|
||||||
public bool UpdateRequest(RequestedModel model)
|
public bool UpdateRequest(RequestedModel model)
|
||||||
{
|
{
|
||||||
var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id };
|
var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id };
|
||||||
return Repo.Update(entity);
|
return Repo.Update(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> UpdateRequestAsync(RequestedModel model)
|
||||||
|
{
|
||||||
|
var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id };
|
||||||
|
return await Repo.UpdateAsync(entity);
|
||||||
|
}
|
||||||
|
|
||||||
public RequestedModel Get(int id)
|
public RequestedModel Get(int id)
|
||||||
{
|
{
|
||||||
var blob = Repo.Get(id);
|
var blob = Repo.Get(id);
|
||||||
|
@ -94,6 +120,17 @@ namespace PlexRequests.Core
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<RequestedModel> GetAsync(int id)
|
||||||
|
{
|
||||||
|
var blob = await Repo.GetAsync(id);
|
||||||
|
if (blob == null)
|
||||||
|
{
|
||||||
|
return new RequestedModel();
|
||||||
|
}
|
||||||
|
var model = ByteConverterHelper.ReturnObject<RequestedModel>(blob.Content);
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
public IEnumerable<RequestedModel> GetAll()
|
public IEnumerable<RequestedModel> GetAll()
|
||||||
{
|
{
|
||||||
var blobs = Repo.GetAll();
|
var blobs = Repo.GetAll();
|
||||||
|
@ -102,6 +139,14 @@ namespace PlexRequests.Core
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<RequestedModel>> GetAllAsync()
|
||||||
|
{
|
||||||
|
var blobs = await Repo.GetAllAsync();
|
||||||
|
return blobs.Select(b => Encoding.UTF8.GetString(b.Content))
|
||||||
|
.Select(JsonConvert.DeserializeObject<RequestedModel>)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
public bool BatchUpdate(List<RequestedModel> model)
|
public bool BatchUpdate(List<RequestedModel> model)
|
||||||
{
|
{
|
||||||
var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList();
|
var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList();
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
// ************************************************************************/
|
// ************************************************************************/
|
||||||
#endregion
|
#endregion
|
||||||
using System;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PlexRequests.Helpers
|
namespace PlexRequests.Helpers
|
||||||
{
|
{
|
||||||
|
@ -40,6 +41,7 @@ namespace PlexRequests.Helpers
|
||||||
/// <param name="cacheTime">The amount of time we want to cache the object</param>
|
/// <param name="cacheTime">The amount of time we want to cache the object</param>
|
||||||
/// <returns><see cref="T"/></returns>
|
/// <returns><see cref="T"/></returns>
|
||||||
T GetOrSet<T>(string key, Func<T> itemCallback, int cacheTime = 20) where T : class;
|
T GetOrSet<T>(string key, Func<T> itemCallback, int cacheTime = 20) where T : class;
|
||||||
|
Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> itemCallback, int cacheTime = 20) where T : class;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the specified item from the cache.
|
/// Gets the specified item from the cache.
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.Caching;
|
using System.Runtime.Caching;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PlexRequests.Helpers
|
namespace PlexRequests.Helpers
|
||||||
{
|
{
|
||||||
|
@ -65,6 +66,23 @@ namespace PlexRequests.Helpers
|
||||||
return item.CloneJson();
|
return item.CloneJson();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> itemCallback, int cacheTime = 20) where T : class
|
||||||
|
{
|
||||||
|
var item = Get<T>(key);
|
||||||
|
if (item == null)
|
||||||
|
{
|
||||||
|
item = await itemCallback();
|
||||||
|
if (item != null)
|
||||||
|
{
|
||||||
|
Set(key, item, cacheTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a copy, not the stored cache reference
|
||||||
|
// The cached object will not change
|
||||||
|
return item.CloneJson();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the specified item from the cache.
|
/// Gets the specified item from the cache.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
// ************************************************************************/
|
// ************************************************************************/
|
||||||
#endregion
|
#endregion
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using PlexRequests.Store.Models;
|
using PlexRequests.Store.Models;
|
||||||
|
|
||||||
|
@ -38,13 +39,18 @@ namespace PlexRequests.Store.Repository
|
||||||
/// <param name="entity">The entity.</param>
|
/// <param name="entity">The entity.</param>
|
||||||
long Insert(RequestBlobs entity);
|
long Insert(RequestBlobs entity);
|
||||||
|
|
||||||
|
Task<int> InsertAsync(RequestBlobs entity);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets all.
|
/// Gets all.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IEnumerable<RequestBlobs> GetAll();
|
IEnumerable<RequestBlobs> GetAll();
|
||||||
|
|
||||||
|
Task<IEnumerable<RequestBlobs>> GetAllAsync();
|
||||||
|
|
||||||
RequestBlobs Get(int id);
|
RequestBlobs Get(int id);
|
||||||
|
Task<RequestBlobs> GetAsync(int id);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Deletes the specified entity.
|
/// Deletes the specified entity.
|
||||||
|
@ -52,8 +58,10 @@ namespace PlexRequests.Store.Repository
|
||||||
/// <param name="entity">The entity.</param>
|
/// <param name="entity">The entity.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
bool Delete(RequestBlobs entity);
|
bool Delete(RequestBlobs entity);
|
||||||
|
Task<bool> DeleteAsync(RequestBlobs entity);
|
||||||
|
|
||||||
bool DeleteAll(IEnumerable<RequestBlobs> entity);
|
bool DeleteAll(IEnumerable<RequestBlobs> entity);
|
||||||
|
Task<bool> DeleteAllAsync(IEnumerable<RequestBlobs> entity);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Updates the specified entity.
|
/// Updates the specified entity.
|
||||||
|
@ -61,7 +69,9 @@ namespace PlexRequests.Store.Repository
|
||||||
/// <param name="entity">The entity.</param>
|
/// <param name="entity">The entity.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
bool Update(RequestBlobs entity);
|
bool Update(RequestBlobs entity);
|
||||||
|
Task<bool> UpdateAsync(RequestBlobs entity);
|
||||||
|
|
||||||
bool UpdateAll(IEnumerable<RequestBlobs> entity);
|
bool UpdateAll(IEnumerable<RequestBlobs> entity);
|
||||||
|
Task<bool> UpdateAllAsync(IEnumerable<RequestBlobs> entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -26,6 +26,7 @@
|
||||||
#endregion
|
#endregion
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Dapper.Contrib.Extensions;
|
using Dapper.Contrib.Extensions;
|
||||||
|
|
||||||
|
@ -56,6 +57,16 @@ namespace PlexRequests.Store.Repository
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<int> InsertAsync(RequestBlobs entity)
|
||||||
|
{
|
||||||
|
ResetCache();
|
||||||
|
using (var con = Db.DbConnection())
|
||||||
|
{
|
||||||
|
var id = await con.InsertAsync(entity);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public IEnumerable<RequestBlobs> GetAll()
|
public IEnumerable<RequestBlobs> GetAll()
|
||||||
{
|
{
|
||||||
var key = "GetAll";
|
var key = "GetAll";
|
||||||
|
@ -70,6 +81,20 @@ namespace PlexRequests.Store.Repository
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<RequestBlobs>> GetAllAsync()
|
||||||
|
{
|
||||||
|
var key = "GetAll";
|
||||||
|
var item = await Cache.GetOrSetAsync(key, async() =>
|
||||||
|
{
|
||||||
|
using (var con = Db.DbConnection())
|
||||||
|
{
|
||||||
|
var page = await con.GetAllAsync<RequestBlobs>();
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
}, 5);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
public RequestBlobs Get(int id)
|
public RequestBlobs Get(int id)
|
||||||
{
|
{
|
||||||
var key = "Get" + id;
|
var key = "Get" + id;
|
||||||
|
@ -84,6 +109,20 @@ namespace PlexRequests.Store.Repository
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<RequestBlobs> GetAsync(int id)
|
||||||
|
{
|
||||||
|
var key = "Get" + id;
|
||||||
|
var item = await Cache.GetOrSetAsync(key, async () =>
|
||||||
|
{
|
||||||
|
using (var con = Db.DbConnection())
|
||||||
|
{
|
||||||
|
var page = await con.GetAsync<RequestBlobs>(id);
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
}, 5);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
public bool Delete(RequestBlobs entity)
|
public bool Delete(RequestBlobs entity)
|
||||||
{
|
{
|
||||||
ResetCache();
|
ResetCache();
|
||||||
|
@ -93,6 +132,30 @@ namespace PlexRequests.Store.Repository
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DeleteAsync(RequestBlobs entity)
|
||||||
|
{
|
||||||
|
ResetCache();
|
||||||
|
using (var con = Db.DbConnection())
|
||||||
|
{
|
||||||
|
return await con.DeleteAsync(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DeleteAllAsync(IEnumerable<RequestBlobs> entity)
|
||||||
|
{
|
||||||
|
ResetCache();
|
||||||
|
var result = new HashSet<bool>();
|
||||||
|
using (var db = Db.DbConnection())
|
||||||
|
{
|
||||||
|
db.Open();
|
||||||
|
foreach (var e in entity)
|
||||||
|
{
|
||||||
|
result.Add(await db.DeleteAsync(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.All(x => true);
|
||||||
|
}
|
||||||
|
|
||||||
public bool Update(RequestBlobs entity)
|
public bool Update(RequestBlobs entity)
|
||||||
{
|
{
|
||||||
ResetCache();
|
ResetCache();
|
||||||
|
@ -102,6 +165,15 @@ namespace PlexRequests.Store.Repository
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> UpdateAsync(RequestBlobs entity)
|
||||||
|
{
|
||||||
|
ResetCache();
|
||||||
|
using (var con = Db.DbConnection())
|
||||||
|
{
|
||||||
|
return await con.UpdateAsync(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void ResetCache()
|
private void ResetCache()
|
||||||
{
|
{
|
||||||
Cache.Remove("Get");
|
Cache.Remove("Get");
|
||||||
|
@ -123,6 +195,21 @@ namespace PlexRequests.Store.Repository
|
||||||
return result.All(x => true);
|
return result.All(x => true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> UpdateAllAsync(IEnumerable<RequestBlobs> entity)
|
||||||
|
{
|
||||||
|
ResetCache();
|
||||||
|
var result = new HashSet<bool>();
|
||||||
|
using (var db = Db.DbConnection())
|
||||||
|
{
|
||||||
|
db.Open();
|
||||||
|
foreach (var e in entity)
|
||||||
|
{
|
||||||
|
result.Add(await db.UpdateAsync(e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.All(x => true);
|
||||||
|
}
|
||||||
|
|
||||||
public bool DeleteAll(IEnumerable<RequestBlobs> entity)
|
public bool DeleteAll(IEnumerable<RequestBlobs> entity)
|
||||||
{
|
{
|
||||||
ResetCache();
|
ResetCache();
|
||||||
|
|
|
@ -164,8 +164,6 @@ namespace PlexRequests.UI.Modules
|
||||||
|
|
||||||
private async Task<Response> ProcessMovies(MovieSearchType searchType, string searchTerm)
|
private async Task<Response> ProcessMovies(MovieSearchType searchType, string searchTerm)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
var apiMovies = new List<MovieResult>();
|
var apiMovies = new List<MovieResult>();
|
||||||
await Task.Factory.StartNew(
|
await Task.Factory.StartNew(
|
||||||
() =>
|
() =>
|
||||||
|
@ -208,16 +206,12 @@ namespace PlexRequests.UI.Modules
|
||||||
apiMovies = t.Result;
|
apiMovies = t.Result;
|
||||||
});
|
});
|
||||||
|
|
||||||
Dictionary<int, RequestedModel> dbMovies = new Dictionary<int, RequestedModel>();
|
var allResults = await RequestService.GetAllAsync();
|
||||||
await Task.Factory.StartNew(() =>
|
allResults = allResults.Where(x => x.Type == RequestType.Movie);
|
||||||
{
|
|
||||||
return RequestService.GetAll().Where(x => x.Type == RequestType.Movie);
|
var distinctResults = allResults.DistinctBy(x => x.ProviderId);
|
||||||
|
var dbMovies = distinctResults.ToDictionary(x => x.ProviderId);
|
||||||
|
|
||||||
}).ContinueWith((t) =>
|
|
||||||
{
|
|
||||||
var distinctResults = t.Result.DistinctBy(x => x.ProviderId);
|
|
||||||
dbMovies = distinctResults.ToDictionary(x => x.ProviderId);
|
|
||||||
});
|
|
||||||
|
|
||||||
var cpCached = CpCacher.QueuedIds();
|
var cpCached = CpCacher.QueuedIds();
|
||||||
var plexMovies = Checker.GetPlexMovies();
|
var plexMovies = Checker.GetPlexMovies();
|
||||||
|
@ -287,15 +281,10 @@ namespace PlexRequests.UI.Modules
|
||||||
apiTv = t.Result;
|
apiTv = t.Result;
|
||||||
});
|
});
|
||||||
|
|
||||||
var dbTv = new Dictionary<int, RequestedModel>();
|
var allResults = await RequestService.GetAllAsync();
|
||||||
await Task.Factory.StartNew(() =>
|
allResults = allResults.Where(x => x.Type == RequestType.TvShow);
|
||||||
{
|
|
||||||
return RequestService.GetAll().Where(x => x.Type == RequestType.TvShow);
|
|
||||||
|
|
||||||
}).ContinueWith((t) =>
|
var dbTv = allResults.ToDictionary(x => x.ProviderId);
|
||||||
{
|
|
||||||
dbTv = t.Result.ToDictionary(x => x.ProviderId);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!apiTv.Any())
|
if (!apiTv.Any())
|
||||||
{
|
{
|
||||||
|
@ -366,18 +355,11 @@ namespace PlexRequests.UI.Modules
|
||||||
apiAlbums = t.Result.releases ?? new List<Release>();
|
apiAlbums = t.Result.releases ?? new List<Release>();
|
||||||
});
|
});
|
||||||
|
|
||||||
var dbAlbum = new Dictionary<string, RequestedModel>();
|
var allResults = await RequestService.GetAllAsync();
|
||||||
await Task.Factory.StartNew(() =>
|
allResults = allResults.Where(x => x.Type == RequestType.Album);
|
||||||
{
|
|
||||||
return RequestService.GetAll().Where(x => x.Type == RequestType.Album);
|
|
||||||
|
|
||||||
}).ContinueWith((t) =>
|
|
||||||
{
|
|
||||||
dbAlbum = t.Result.ToDictionary(x => x.MusicBrainzId);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var dbAlbum = allResults.ToDictionary(x => x.MusicBrainzId);
|
||||||
|
|
||||||
var plexAlbums = Checker.GetPlexAlbums();
|
var plexAlbums = Checker.GetPlexAlbums();
|
||||||
|
|
||||||
var viewAlbum = new List<SearchMusicViewModel>();
|
var viewAlbum = new List<SearchMusicViewModel>();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue