From 166e0f81cf2ab3f46f20999fc4ddd13a1d066d38 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Fri, 27 May 2016 15:07:07 +0100 Subject: [PATCH] Made more async goodness --- PlexRequests.Core/IRequestService.cs | 7 ++ PlexRequests.Core/JsonRequestService.cs | 45 ++++++++++ PlexRequests.Helpers/ICacheProvider.cs | 2 + PlexRequests.Helpers/MemoryCacheProvider.cs | 18 ++++ .../Repository/IRequestRepository.cs | 10 +++ .../Repository/RequestJsonRepository.cs | 87 +++++++++++++++++++ PlexRequests.UI/Modules/SearchModule.cs | 42 +++------ 7 files changed, 181 insertions(+), 30 deletions(-) diff --git a/PlexRequests.Core/IRequestService.cs b/PlexRequests.Core/IRequestService.cs index cd8427ac5..dabade7ae 100644 --- a/PlexRequests.Core/IRequestService.cs +++ b/PlexRequests.Core/IRequestService.cs @@ -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 AddRequestAsync(RequestedModel model); RequestedModel CheckRequest(int providerId); RequestedModel CheckRequest(string musicId); void DeleteRequest(RequestedModel request); + Task DeleteRequestAsync(RequestedModel request); bool UpdateRequest(RequestedModel model); + Task UpdateRequestAsync(RequestedModel model); RequestedModel Get(int id); + Task GetAsync(int id); IEnumerable GetAll(); + Task> GetAllAsync(); bool BatchUpdate(List model); bool BatchDelete(List model); } diff --git a/PlexRequests.Core/JsonRequestService.cs b/PlexRequests.Core/JsonRequestService.cs index 770b41444..5a5ec2e4f 100644 --- a/PlexRequests.Core/JsonRequestService.cs +++ b/PlexRequests.Core/JsonRequestService.cs @@ -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 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 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 GetAsync(int id) + { + var blob = await Repo.GetAsync(id); + if (blob == null) + { + return new RequestedModel(); + } + var model = ByteConverterHelper.ReturnObject(blob.Content); + return model; + } + public IEnumerable GetAll() { var blobs = Repo.GetAll(); @@ -102,6 +139,14 @@ namespace PlexRequests.Core .ToList(); } + public async Task> GetAllAsync() + { + var blobs = await Repo.GetAllAsync(); + return blobs.Select(b => Encoding.UTF8.GetString(b.Content)) + .Select(JsonConvert.DeserializeObject) + .ToList(); + } + public bool BatchUpdate(List model) { var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList(); diff --git a/PlexRequests.Helpers/ICacheProvider.cs b/PlexRequests.Helpers/ICacheProvider.cs index fa957315d..00fde411b 100644 --- a/PlexRequests.Helpers/ICacheProvider.cs +++ b/PlexRequests.Helpers/ICacheProvider.cs @@ -25,6 +25,7 @@ // ************************************************************************/ #endregion using System; +using System.Threading.Tasks; namespace PlexRequests.Helpers { @@ -40,6 +41,7 @@ namespace PlexRequests.Helpers /// The amount of time we want to cache the object /// T GetOrSet(string key, Func itemCallback, int cacheTime = 20) where T : class; + Task GetOrSetAsync(string key, Func> itemCallback, int cacheTime = 20) where T : class; /// /// Gets the specified item from the cache. diff --git a/PlexRequests.Helpers/MemoryCacheProvider.cs b/PlexRequests.Helpers/MemoryCacheProvider.cs index fdb78f291..c85ef40f0 100644 --- a/PlexRequests.Helpers/MemoryCacheProvider.cs +++ b/PlexRequests.Helpers/MemoryCacheProvider.cs @@ -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 GetOrSetAsync(string key, Func> itemCallback, int cacheTime = 20) where T : class + { + var item = Get(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(); + } + /// /// Gets the specified item from the cache. /// diff --git a/PlexRequests.Store/Repository/IRequestRepository.cs b/PlexRequests.Store/Repository/IRequestRepository.cs index 650dc9b4b..440fe715e 100644 --- a/PlexRequests.Store/Repository/IRequestRepository.cs +++ b/PlexRequests.Store/Repository/IRequestRepository.cs @@ -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 /// The entity. long Insert(RequestBlobs entity); + Task InsertAsync(RequestBlobs entity); + /// /// Gets all. /// /// IEnumerable GetAll(); + Task> GetAllAsync(); + RequestBlobs Get(int id); + Task GetAsync(int id); /// /// Deletes the specified entity. @@ -52,8 +58,10 @@ namespace PlexRequests.Store.Repository /// The entity. /// bool Delete(RequestBlobs entity); + Task DeleteAsync(RequestBlobs entity); bool DeleteAll(IEnumerable entity); + Task DeleteAllAsync(IEnumerable entity); /// /// Updates the specified entity. @@ -61,7 +69,9 @@ namespace PlexRequests.Store.Repository /// The entity. /// bool Update(RequestBlobs entity); + Task UpdateAsync(RequestBlobs entity); bool UpdateAll(IEnumerable entity); + Task UpdateAllAsync(IEnumerable entity); } } \ No newline at end of file diff --git a/PlexRequests.Store/Repository/RequestJsonRepository.cs b/PlexRequests.Store/Repository/RequestJsonRepository.cs index bad8736da..07ac3c0bd 100644 --- a/PlexRequests.Store/Repository/RequestJsonRepository.cs +++ b/PlexRequests.Store/Repository/RequestJsonRepository.cs @@ -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 InsertAsync(RequestBlobs entity) + { + ResetCache(); + using (var con = Db.DbConnection()) + { + var id = await con.InsertAsync(entity); + return id; + } + } + public IEnumerable GetAll() { var key = "GetAll"; @@ -70,6 +81,20 @@ namespace PlexRequests.Store.Repository return item; } + public async Task> GetAllAsync() + { + var key = "GetAll"; + var item = await Cache.GetOrSetAsync(key, async() => + { + using (var con = Db.DbConnection()) + { + var page = await con.GetAllAsync(); + 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 GetAsync(int id) + { + var key = "Get" + id; + var item = await Cache.GetOrSetAsync(key, async () => + { + using (var con = Db.DbConnection()) + { + var page = await con.GetAsync(id); + return page; + } + }, 5); + return item; + } + public bool Delete(RequestBlobs entity) { ResetCache(); @@ -93,6 +132,30 @@ namespace PlexRequests.Store.Repository } } + public async Task DeleteAsync(RequestBlobs entity) + { + ResetCache(); + using (var con = Db.DbConnection()) + { + return await con.DeleteAsync(entity); + } + } + + public async Task DeleteAllAsync(IEnumerable entity) + { + ResetCache(); + var result = new HashSet(); + 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 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 UpdateAllAsync(IEnumerable entity) + { + ResetCache(); + var result = new HashSet(); + 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 entity) { ResetCache(); diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index f517a1387..0979c1c6a 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -164,8 +164,6 @@ namespace PlexRequests.UI.Modules private async Task ProcessMovies(MovieSearchType searchType, string searchTerm) { - - var apiMovies = new List(); await Task.Factory.StartNew( () => @@ -208,16 +206,12 @@ namespace PlexRequests.UI.Modules apiMovies = t.Result; }); - Dictionary dbMovies = new Dictionary(); - 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(); - 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,18 +355,11 @@ namespace PlexRequests.UI.Modules apiAlbums = t.Result.releases ?? new List(); }); - var dbAlbum = new Dictionary(); - 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(); var viewAlbum = new List();