Made more async goodness

This commit is contained in:
tidusjar 2016-05-27 15:07:07 +01:00
commit 166e0f81cf
7 changed files with 181 additions and 30 deletions

View file

@ -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);
}
}

View file

@ -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();