Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using KendoCRUDService.Data.Models;
using KendoCRUDService.Models;
using KendoCRUDService.Extensions;
using KendoCRUDService.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;

namespace KendoCRUDService.Data.Repositories
Expand All @@ -10,22 +11,28 @@ public class DetailProductRepository
{
private readonly ISession _session;
private readonly IServiceScopeFactory _scopeFactory;
private ConcurrentDictionary<string, IList<DetailProduct>> _detailProducts;
private readonly IUserDataCache _userCache;
private readonly ILogger<DetailProductRepository> _logger;

private static readonly TimeSpan Ttl = TimeSpan.FromMinutes(15);
private const string LogicalName = "DetailProducts";
private IHttpContextAccessor _contextAccessor;

public DetailProductRepository(IHttpContextAccessor httpContextAccessor, IServiceScopeFactory scopeFactory)
public DetailProductRepository(IHttpContextAccessor httpContextAccessor, IServiceScopeFactory scopeFactory, IUserDataCache userCache,
ILogger<DetailProductRepository> logger)
{
_session = httpContextAccessor.HttpContext.Session;
_contextAccessor = httpContextAccessor;
_scopeFactory = scopeFactory;
_detailProducts = new ConcurrentDictionary<string, IList<DetailProduct>>();
_userCache = userCache;
_logger = logger;
}

public IList<DetailProduct> All()
{
var userKey = SessionUtils.GetUserKey(_contextAccessor);

return _detailProducts.GetOrAdd(userKey, key =>
return _userCache.GetOrCreateList<DetailProduct>(userKey, LogicalName, () =>
{
using (var scope = _scopeFactory.CreateScope())
{
Expand All @@ -48,7 +55,19 @@ public IList<DetailProduct> All()
UnitsOnOrder = p.UnitsOnOrder
}).ToList();
}
});
},Ttl, sliding: true);
}

private void UpdateContent(List<DetailProduct> entries)
{
var userKey = SessionUtils.GetUserKey(_contextAccessor);
_userCache.SetList<DetailProduct>(
userKey,
LogicalName,
entries,
Ttl,
sliding: true
);
}

public DetailProduct One(Func<DetailProduct, bool> predicate)
Expand All @@ -58,7 +77,8 @@ public DetailProduct One(Func<DetailProduct, bool> predicate)

public void Insert(DetailProduct product)
{
var first = All().OrderByDescending(p => p.ProductID).FirstOrDefault();
var entries = All().ToList();
var first = entries.OrderByDescending(p => p.ProductID).FirstOrDefault();
if (first != null)
{
product.ProductID = first.ProductID + 1;
Expand All @@ -68,7 +88,8 @@ public void Insert(DetailProduct product)
product.ProductID = 0;
}

All().Insert(0, product);
entries.Insert(0, product);
UpdateContent(entries);
}

public void Insert(IEnumerable<DetailProduct> products)
Expand Down Expand Up @@ -114,7 +135,9 @@ public void Delete(DetailProduct product)
var target = One(p => p.ProductID == product.ProductID);
if (target != null)
{
All().Remove(target);
var entries = All().ToList();
entries.Remove(target);
UpdateContent(entries);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using KendoCRUDService.Data.Models;
using KendoCRUDService.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;

namespace KendoCRUDService.Data.Repositories
Expand All @@ -10,29 +11,47 @@ public class DiagramConnectionsRepository
{
private readonly ISession _session;
private readonly IServiceScopeFactory _scopeFactory;
private ConcurrentDictionary<string, IList<OrgChartConnection>> _connetctions;
private readonly IUserDataCache _userCache;
private readonly ILogger<DiagramConnectionsRepository> _logger;

private static readonly TimeSpan Ttl = TimeSpan.FromMinutes(15);
private const string LogicalName = "OrgChartConnections";
private IHttpContextAccessor _contextAccessor;

public DiagramConnectionsRepository(IHttpContextAccessor httpContextAccessor, IServiceScopeFactory scopeFactory)
public DiagramConnectionsRepository(IHttpContextAccessor httpContextAccessor, IServiceScopeFactory scopeFactory, IUserDataCache userCache,
ILogger<DiagramConnectionsRepository> logger)
{
_session = httpContextAccessor.HttpContext.Session;
_contextAccessor = httpContextAccessor;
_scopeFactory = scopeFactory;
_connetctions = new ConcurrentDictionary<string, IList<OrgChartConnection>>();
_userCache = userCache;
_logger = logger;
}

public IList<OrgChartConnection> All()
{
var userKey = SessionUtils.GetUserKey(_contextAccessor);

return _connetctions.GetOrAdd(userKey, key =>
return _userCache.GetOrCreateList<OrgChartConnection>(userKey, LogicalName, () =>
{
using (var scope = _scopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<DemoDbContext>();
return context.OrgChartConnections.ToList();
}
});
}, Ttl, sliding: true);
}

private void UpdateContent(List<OrgChartConnection> entries)
{
var userKey = SessionUtils.GetUserKey(_contextAccessor);
_userCache.SetList<OrgChartConnection>(
userKey,
LogicalName,
entries,
Ttl,
sliding: true
);
}

public OrgChartConnection One(Func<OrgChartConnection, bool> predicate)
Expand All @@ -50,7 +69,8 @@ public void Insert(IEnumerable<OrgChartConnection> connections)

public void Insert(OrgChartConnection connection)
{
var first = All().OrderByDescending(e => e.Id).FirstOrDefault();
var entries = All();
var first = entries.OrderByDescending(e => e.Id).FirstOrDefault();

long id = 0;

Expand All @@ -61,7 +81,8 @@ public void Insert(OrgChartConnection connection)

connection.Id = id + 1;

All().Insert(0, connection);
entries.Insert(0, connection);
UpdateContent(entries.ToList());
}

public void Update(IEnumerable<OrgChartConnection> connections)
Expand Down Expand Up @@ -101,7 +122,9 @@ public void Delete(OrgChartConnection connection)
var target = One(p => p.Id == connection.Id);
if (target != null)
{
All().Remove(target);
var entries = All().ToList();
entries.Remove(target);
UpdateContent(entries);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using KendoCRUDService.Data.Models;
using KendoCRUDService.Models;
using KendoCRUDService.Extensions;
using KendoCRUDService.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System.Collections.Concurrent;

namespace KendoCRUDService.Data.Repositories
Expand All @@ -11,29 +12,47 @@ public class DiagramShapesRepository
{
private readonly ISession _session;
private readonly IServiceScopeFactory _scopeFactory;
private ConcurrentDictionary<string, IList<OrgChartShape>> _shapes;
private readonly IUserDataCache _userCache;
private readonly ILogger<DiagramShapesRepository> _logger;

private static readonly TimeSpan Ttl = TimeSpan.FromMinutes(15);
private const string LogicalName = "OrgChartShapes";
private IHttpContextAccessor _contextAccessor;

public DiagramShapesRepository(IHttpContextAccessor httpContextAccessor, IServiceScopeFactory scopeFactory)
public DiagramShapesRepository(IHttpContextAccessor httpContextAccessor, IServiceScopeFactory scopeFactory, IUserDataCache userCache,
ILogger<DiagramShapesRepository> logger)
{
_session = httpContextAccessor.HttpContext.Session;
_contextAccessor = httpContextAccessor;
_scopeFactory = scopeFactory;
_shapes = new ConcurrentDictionary<string, IList<OrgChartShape>>();
_userCache = userCache;
_logger = logger;
}

public IList<OrgChartShape> All()
{
var userKey = SessionUtils.GetUserKey(_contextAccessor);

return _shapes.GetOrAdd(userKey, key =>
return _userCache.GetOrCreateList<OrgChartShape>(userKey, LogicalName, () =>
{
using (var scope = _scopeFactory.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService<DemoDbContext>();
return context.OrgChartShapes.ToList();
}
});
}, Ttl, sliding: true);
}

private void UpdateContent(List<OrgChartShape> entries)
{
var userKey = SessionUtils.GetUserKey(_contextAccessor);
_userCache.SetList<OrgChartShape>(
userKey,
LogicalName,
entries,
Ttl,
sliding: true
);
}

public OrgChartShape One(Func<OrgChartShape, bool> predicate)
Expand All @@ -51,7 +70,8 @@ public void Insert(IEnumerable<OrgChartShape> shapes)

public void Insert(OrgChartShape shape)
{
var first = All().OrderByDescending(e => e.Id).FirstOrDefault();
var entries = All().ToList();
var first = entries.OrderByDescending(e => e.Id).FirstOrDefault();

var id = 0;

Expand All @@ -62,7 +82,8 @@ public void Insert(OrgChartShape shape)

shape.Id = id + 1;

All().Insert(0, shape);
entries.Insert(0, shape);
UpdateContent(entries);
}

public void Update(IEnumerable<OrgChartShape> shapes)
Expand Down Expand Up @@ -97,7 +118,9 @@ public void Delete(OrgChartShape shape)
var target = One(p => p.Id == shape.Id);
if (target != null)
{
All().Remove(target);
var entries = All().ToList();
entries.Remove(target);
UpdateContent(entries);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using KendoCRUDService.Data.Models;
using KendoCRUDService.Extensions;
using KendoCRUDService.Models.Request;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using System.Collections.Concurrent;
Expand All @@ -18,15 +19,22 @@ public string ContentPath
return Path.Combine(hostingEnvironment.ContentRootPath, ContentRootPath);
}
}
private ConcurrentDictionary<string, List<FileManagerEntry>> _directories;
private readonly IHttpContextAccessor _contextAccessor;
private readonly IWebHostEnvironment hostingEnvironment;

public DirectoryRepository(IWebHostEnvironment _hostingEnvironment, IHttpContextAccessor contextAccessor)
private readonly IServiceScopeFactory _scopeFactory;
private readonly IUserDataCache _userCache;
private readonly ILogger<DirectoryRepository> _logger;

private static readonly TimeSpan Ttl = TimeSpan.FromMinutes(15);
private const string LogicalName = "directories";

public DirectoryRepository(IWebHostEnvironment _hostingEnvironment, IHttpContextAccessor contextAccessor, IUserDataCache userCache, ILogger<DirectoryRepository> logger)
{
hostingEnvironment = _hostingEnvironment;
_contextAccessor = contextAccessor;
_directories = new ConcurrentDictionary<string, List<FileManagerEntry>>();
_userCache = userCache;
_logger = logger;
}

public ICollection<FileManagerEntry> GetAll(string path, string filter)
Expand Down Expand Up @@ -82,15 +90,19 @@ public IEnumerable<FileManagerEntry> GetContent(string path, string filter)
{
var userKey = SessionUtils.GetUserKey(_contextAccessor);

var entries = _directories.GetOrAdd(userKey, key =>
{
return GetAll(ContentPath, filter).ToList();
});
var entries = _userCache.GetOrCreateList<FileManagerEntry>(
userKey,
LogicalName,
() =>
{
return GetAll(ContentPath, filter).ToList();
},
Ttl,
sliding: true
);

var virtualized = entries.Where(d => TargetMatch(path, d.Path)).Select(VirtualizePath);



return entries.Where(d => TargetMatch(path, d.Path)).Select(VirtualizePath).ToList();
}

Expand Down Expand Up @@ -422,19 +434,28 @@ private bool IsValidFile(string fileName)
return allowedExtensions.Any(e => e.EndsWith(extension, StringComparison.InvariantCultureIgnoreCase));
}

private List<FileManagerEntry> Content()
private List<FileManagerEntry> Content()
{
var userKey = SessionUtils.GetUserKey(_contextAccessor);
List<FileManagerEntry> entries = new List<FileManagerEntry>();
_directories.TryGetValue(SessionUtils.GetUserKey(_contextAccessor), out entries);

return entries;
if (_userCache.TryGetList<FileManagerEntry>(userKey, LogicalName, out var list))
return list;
return _userCache.GetOrCreateList<FileManagerEntry>(
userKey,
LogicalName,
() => new List<FileManagerEntry>(),
Ttl,
sliding: true).ToList();
}

private void UpdateContent(List<FileManagerEntry> entries)
{
var userKey = SessionUtils.GetUserKey(_contextAccessor);
_directories[userKey] = entries;
_userCache.SetList<FileManagerEntry>(
userKey,
LogicalName,
entries,
Ttl,
sliding: true
);
}

protected virtual bool CanAccess(string path)
Expand Down
Loading