mirror of
https://github.com/BtbN/ClanServer.git
synced 2026-04-24 15:38:03 -05:00
Cache recommended songs so they persist one credit
This commit is contained in:
parent
2abe996c90
commit
09307826e8
12
ClanServer/CacheKeys.cs
Normal file
12
ClanServer/CacheKeys.cs
Normal 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}";
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
||||
|
|
|
|||
|
|
@ -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 =>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user