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
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using PlexRequests.Store;
|
||||
|
||||
namespace PlexRequests.Core
|
||||
|
@ -33,13 +35,18 @@ namespace PlexRequests.Core
|
|||
public interface IRequestService
|
||||
{
|
||||
long AddRequest(RequestedModel model);
|
||||
Task<int> AddRequestAsync(RequestedModel model);
|
||||
RequestedModel CheckRequest(int providerId);
|
||||
RequestedModel CheckRequest(string musicId);
|
||||
|
||||
void DeleteRequest(RequestedModel request);
|
||||
Task DeleteRequestAsync(RequestedModel request);
|
||||
bool UpdateRequest(RequestedModel model);
|
||||
Task<bool> UpdateRequestAsync(RequestedModel model);
|
||||
RequestedModel Get(int id);
|
||||
Task<RequestedModel> GetAsync(int id);
|
||||
IEnumerable<RequestedModel> GetAll();
|
||||
Task<IEnumerable<RequestedModel>> GetAllAsync();
|
||||
bool BatchUpdate(List<RequestedModel> model);
|
||||
bool BatchDelete(List<RequestedModel> model);
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
@ -57,6 +58,19 @@ namespace PlexRequests.Core
|
|||
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)
|
||||
{
|
||||
var blobs = Repo.GetAll();
|
||||
|
@ -77,12 +91,24 @@ namespace PlexRequests.Core
|
|||
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)
|
||||
{
|
||||
var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id };
|
||||
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)
|
||||
{
|
||||
var blob = Repo.Get(id);
|
||||
|
@ -94,6 +120,17 @@ namespace PlexRequests.Core
|
|||
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()
|
||||
{
|
||||
var blobs = Repo.GetAll();
|
||||
|
@ -102,6 +139,14 @@ namespace PlexRequests.Core
|
|||
.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)
|
||||
{
|
||||
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
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PlexRequests.Helpers
|
||||
{
|
||||
|
@ -40,6 +41,7 @@ namespace PlexRequests.Helpers
|
|||
/// <param name="cacheTime">The amount of time we want to cache the object</param>
|
||||
/// <returns><see cref="T"/></returns>
|
||||
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>
|
||||
/// Gets the specified item from the cache.
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Runtime.Caching;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PlexRequests.Helpers
|
||||
{
|
||||
|
@ -65,6 +66,23 @@ namespace PlexRequests.Helpers
|
|||
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>
|
||||
/// Gets the specified item from the cache.
|
||||
/// </summary>
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
// ************************************************************************/
|
||||
#endregion
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using PlexRequests.Store.Models;
|
||||
|
||||
|
@ -38,13 +39,18 @@ namespace PlexRequests.Store.Repository
|
|||
/// <param name="entity">The entity.</param>
|
||||
long Insert(RequestBlobs entity);
|
||||
|
||||
Task<int> InsertAsync(RequestBlobs entity);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IEnumerable<RequestBlobs> GetAll();
|
||||
|
||||
Task<IEnumerable<RequestBlobs>> GetAllAsync();
|
||||
|
||||
RequestBlobs Get(int id);
|
||||
Task<RequestBlobs> GetAsync(int id);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the specified entity.
|
||||
|
@ -52,8 +58,10 @@ namespace PlexRequests.Store.Repository
|
|||
/// <param name="entity">The entity.</param>
|
||||
/// <returns></returns>
|
||||
bool Delete(RequestBlobs entity);
|
||||
Task<bool> DeleteAsync(RequestBlobs entity);
|
||||
|
||||
bool DeleteAll(IEnumerable<RequestBlobs> entity);
|
||||
Task<bool> DeleteAllAsync(IEnumerable<RequestBlobs> entity);
|
||||
|
||||
/// <summary>
|
||||
/// Updates the specified entity.
|
||||
|
@ -61,7 +69,9 @@ namespace PlexRequests.Store.Repository
|
|||
/// <param name="entity">The entity.</param>
|
||||
/// <returns></returns>
|
||||
bool Update(RequestBlobs entity);
|
||||
Task<bool> UpdateAsync(RequestBlobs entity);
|
||||
|
||||
bool UpdateAll(IEnumerable<RequestBlobs> entity);
|
||||
Task<bool> UpdateAllAsync(IEnumerable<RequestBlobs> entity);
|
||||
}
|
||||
}
|
|
@ -26,6 +26,7 @@
|
|||
#endregion
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
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()
|
||||
{
|
||||
var key = "GetAll";
|
||||
|
@ -70,6 +81,20 @@ namespace PlexRequests.Store.Repository
|
|||
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)
|
||||
{
|
||||
var key = "Get" + id;
|
||||
|
@ -84,6 +109,20 @@ namespace PlexRequests.Store.Repository
|
|||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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()
|
||||
{
|
||||
Cache.Remove("Get");
|
||||
|
@ -123,6 +195,21 @@ namespace PlexRequests.Store.Repository
|
|||
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)
|
||||
{
|
||||
ResetCache();
|
||||
|
|
|
@ -164,8 +164,6 @@ namespace PlexRequests.UI.Modules
|
|||
|
||||
private async Task<Response> ProcessMovies(MovieSearchType searchType, string searchTerm)
|
||||
{
|
||||
|
||||
|
||||
var apiMovies = new List<MovieResult>();
|
||||
await Task.Factory.StartNew(
|
||||
() =>
|
||||
|
@ -208,16 +206,12 @@ namespace PlexRequests.UI.Modules
|
|||
apiMovies = t.Result;
|
||||
});
|
||||
|
||||
Dictionary<int, RequestedModel> dbMovies = new Dictionary<int, RequestedModel>();
|
||||
await Task.Factory.StartNew(() =>
|
||||
{
|
||||
return RequestService.GetAll().Where(x => x.Type == RequestType.Movie);
|
||||
var allResults = await RequestService.GetAllAsync();
|
||||
allResults = allResults.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 plexMovies = Checker.GetPlexMovies();
|
||||
|
@ -287,15 +281,10 @@ namespace PlexRequests.UI.Modules
|
|||
apiTv = t.Result;
|
||||
});
|
||||
|
||||
var dbTv = new Dictionary<int, RequestedModel>();
|
||||
await Task.Factory.StartNew(() =>
|
||||
{
|
||||
return RequestService.GetAll().Where(x => x.Type == RequestType.TvShow);
|
||||
var allResults = await RequestService.GetAllAsync();
|
||||
allResults = allResults.Where(x => x.Type == RequestType.TvShow);
|
||||
|
||||
}).ContinueWith((t) =>
|
||||
{
|
||||
dbTv = t.Result.ToDictionary(x => x.ProviderId);
|
||||
});
|
||||
var dbTv = allResults.ToDictionary(x => x.ProviderId);
|
||||
|
||||
if (!apiTv.Any())
|
||||
{
|
||||
|
@ -366,17 +355,10 @@ namespace PlexRequests.UI.Modules
|
|||
apiAlbums = t.Result.releases ?? new List<Release>();
|
||||
});
|
||||
|
||||
var dbAlbum = new Dictionary<string, RequestedModel>();
|
||||
await Task.Factory.StartNew(() =>
|
||||
{
|
||||
return RequestService.GetAll().Where(x => x.Type == RequestType.Album);
|
||||
|
||||
}).ContinueWith((t) =>
|
||||
{
|
||||
dbAlbum = t.Result.ToDictionary(x => x.MusicBrainzId);
|
||||
});
|
||||
|
||||
var allResults = await RequestService.GetAllAsync();
|
||||
allResults = allResults.Where(x => x.Type == RequestType.Album);
|
||||
|
||||
var dbAlbum = allResults.ToDictionary(x => x.MusicBrainzId);
|
||||
|
||||
var plexAlbums = Checker.GetPlexAlbums();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue