Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
#12 Caching photostocks
Browse files Browse the repository at this point in the history
Added caching to stocks data

Co-Authored-By: Grigory Sazanov  <56090134+SazanovGrigory@users.noreply.github.com>
  • Loading branch information
rhiskey and SazanovGrigory committed May 1, 2021
1 parent aaeeabf commit a0e933f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
3 changes: 1 addition & 2 deletions Client/Components/LastPostedTracks.razor
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
<td>@GetPlaylistNameFromId(row.PlaylistId)</td>
<td>@row.Trackname</td>
<td>@row.Date.ToString("dd.MM.yyy HH:mm")</td>

</tr>
}
</tbody>
Expand All @@ -32,7 +31,7 @@
<br />

@code {
string[] headings = { "ID", "MediaID", "OwnerID", "Playlist", "Trackname", "Date" };
string[] headings = { "ID", "MediaID", "OwnerID", "Playlist", "Trackname", "Date"};

[Parameter]
public IEnumerable<PostedTrack> PostedTracks { get; set; }
Expand Down
28 changes: 24 additions & 4 deletions Server/Controllers/ConsolePhotostocksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Microsoft.EntityFrameworkCore;
using HVMDash.Server.Context;
using vkaudioposter_ef.parser;

using Microsoft.Extensions.Caching.Memory;

namespace HVMDash.Server.Controllers
{
Expand All @@ -17,17 +17,35 @@ namespace HVMDash.Server.Controllers
public class ConsolePhotostocksController : ControllerBase
{
private readonly ConsolePhotostockContext _context;
private readonly IMemoryCache memoryCache;
private readonly string cacheKey = "AllPhotostocks";

private readonly MemoryCacheEntryOptions cacheExpiryOptions = new MemoryCacheEntryOptions
{
AbsoluteExpiration = DateTime.Now.AddMinutes(5),
Priority = CacheItemPriority.High,
SlidingExpiration = TimeSpan.FromMinutes(2)
};

public ConsolePhotostocksController(ConsolePhotostockContext context)
public ConsolePhotostocksController(IMemoryCache memoryCache, ConsolePhotostockContext context)
{
this.memoryCache = memoryCache;
_context = context;
}

// GET: api/ConsolePhotostocks
[HttpGet]
public async Task<ActionResult<IEnumerable<ConsolePhotostock>>> GetPhotostocks()
{
return await _context.Photostocks.ToListAsync();
List<ConsolePhotostock> stock1 = new List<ConsolePhotostock>();
if (!memoryCache.TryGetValue(cacheKey, out IEnumerable<ConsolePhotostock> stock))
{
stock1 = await _context.Photostocks.ToListAsync();
memoryCache.Set(cacheKey, stock1, cacheExpiryOptions);
return stock1;
}
memoryCache.TryGetValue(cacheKey, out stock1);
return stock1;
}

// GET: api/ConsolePhotostocks/5
Expand Down Expand Up @@ -71,7 +89,7 @@ public async Task<IActionResult> PutConsolePhotostock(int id, ConsolePhotostock
throw;
}
}

memoryCache.Remove(cacheKey);
return NoContent();
}

Expand All @@ -80,6 +98,7 @@ public async Task<IActionResult> PutConsolePhotostock(int id, ConsolePhotostock
[HttpPost]
public async Task<ActionResult<ConsolePhotostock>> PostConsolePhotostock(ConsolePhotostock consolePhotostock)
{
memoryCache.Remove(cacheKey);
_context.Photostocks.Add(consolePhotostock);
await _context.SaveChangesAsync();

Expand All @@ -90,6 +109,7 @@ public async Task<ActionResult<ConsolePhotostock>> PostConsolePhotostock(Console
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteConsolePhotostock(int id)
{
memoryCache.Remove(cacheKey);
var consolePhotostock = await _context.Photostocks.FindAsync(id);
if (consolePhotostock == null)
{
Expand Down

0 comments on commit a0e933f

Please sign in to comment.