Cache recommended songs so they persist one credit

This commit is contained in:
BtbN 2019-06-06 18:27:55 +02:00
parent 2abe996c90
commit 09307826e8
4 changed files with 44 additions and 17 deletions

12
ClanServer/CacheKeys.cs Normal file
View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ClanServer
{
public static class CacheKeys
{
public static string GetRecommendedSongsKey(int jid) => $"_RecommendedSongs_{jid}";
}
}

View File

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using eAmuseCore.KBinXML;
@ -19,9 +20,11 @@ namespace ClanServer.Controllers.L44
public class GametopController : ControllerBase
{
private readonly ClanServerContext ctx;
private readonly IMemoryCache cache;
public GametopController(ClanServerContext ctx)
public GametopController(IMemoryCache cache, ClanServerContext ctx)
{
this.cache = cache;
this.ctx = ctx;
}
@ -158,12 +161,10 @@ namespace ClanServer.Controllers.L44
var profile = player.JubeatProfile;
bool changed = false;
await ctx.Entry(profile).Reference(p => p.ClanData).LoadAsync();
await ctx.Entry(profile).Reference(p => p.ClanSettings).LoadAsync();
bool freshProfile = false;
cache.Remove(CacheKeys.GetRecommendedSongsKey(profile.ID));
if (profile.ClanData == null)
{
Random rng = new Random();
@ -171,10 +172,10 @@ namespace ClanServer.Controllers.L44
profile.ClanData = new JubeatClanProfileData()
{
Team = (byte)(rng.Next(1, 5)),
Street = rng.Next(1, 19),
Street = rng.Next(1, 9),
Section = rng.Next(1, 5),
HouseNo1 = (short)(rng.Next(1, 25)),
HouseNo2 = (short)(rng.Next(1, 241)),
HouseNo1 = (short)(rng.Next(1, 10)),
HouseNo2 = (short)(rng.Next(1, 100)),
TuneCount = 0,
ClearCount = 0,

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using eAmuseCore.KBinXML;
@ -15,17 +16,27 @@ namespace ClanServer.Controllers.L44
[ApiController, Route("L44")]
public class RecommendController : ControllerBase
{
private readonly Random rng = new Random();
private readonly IMemoryCache cache;
public RecommendController(IMemoryCache cache)
{
this.cache = cache;
}
[HttpPost, Route("8"), XrpcCall("recommend.get_recommend")]
public async Task<ActionResult<EamuseXrpcData>> GetRecommend([FromBody] EamuseXrpcData data)
{
XElement recommend = data.Document.Element("call").Element("recommend");
XElement player = recommend.Element("data").Element("player");
_ = int.Parse(player.Element("jid").Value);
int jid = int.Parse(player.Element("jid").Value);
ClanMusicInfo mInfo = await ClanMusicInfo.Instance;
List<int> recommendedMusic = mInfo.GetRandomSongs(10);
Random rng = new Random();
List<int> recommendedMusic =
await cache.GetOrCreateAsync(CacheKeys.GetRecommendedSongsKey(jid), async entry =>
{
entry.SlidingExpiration = TimeSpan.FromMinutes(30);
return (await ClanMusicInfo.Instance).GetRandomSongs(10);
});
XElement musicList = new XElement("music_list");

View File

@ -26,10 +26,13 @@ namespace ClanServer
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ClanServerContext>(options =>
{
options.UseSqlite("Data Source=clanserver.db");
});
services.AddMemoryCache();
services
.AddDbContext<ClanServerContext>(options =>
{
options.UseSqlite("Data Source=clanserver.db");
});
services
.AddMvc(options =>