2021-11-29 21:04:15 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
using IF.Lastfm.Core.Api;
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
using SpotifyAPI.Web;
|
|
|
|
|
|
|
|
|
|
namespace Selector.Cache
|
|
|
|
|
{
|
|
|
|
|
public class PlayCounterCaching: PlayCounter
|
|
|
|
|
{
|
|
|
|
|
private readonly IDatabaseAsync Db;
|
2021-12-01 23:34:30 +00:00
|
|
|
|
public TimeSpan CacheExpiry { get; set; } = TimeSpan.FromDays(1);
|
2021-11-29 21:04:15 +00:00
|
|
|
|
|
|
|
|
|
public PlayCounterCaching(
|
|
|
|
|
IPlayerWatcher watcher,
|
|
|
|
|
ITrackApi trackClient,
|
|
|
|
|
IAlbumApi albumClient,
|
|
|
|
|
IArtistApi artistClient,
|
|
|
|
|
IUserApi userClient,
|
|
|
|
|
IDatabaseAsync db,
|
|
|
|
|
LastFmCredentials credentials = null,
|
|
|
|
|
ILogger<PlayCounterCaching> logger = null,
|
|
|
|
|
CancellationToken token = default
|
|
|
|
|
) : base(watcher, trackClient, albumClient, artistClient, userClient, credentials, logger, token)
|
|
|
|
|
{
|
|
|
|
|
Db = db;
|
|
|
|
|
|
|
|
|
|
NewPlayCount += CacheCallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CacheCallback(object sender, PlayCount e)
|
|
|
|
|
{
|
2021-12-18 23:06:21 +00:00
|
|
|
|
Task.Run(async () => {
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await AsyncCacheCallback(e);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogError(e, "Error occured during callback");
|
|
|
|
|
}
|
|
|
|
|
}, CancelToken);
|
2021-11-29 21:04:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task AsyncCacheCallback(PlayCount e)
|
|
|
|
|
{
|
|
|
|
|
var track = e.ListeningEvent.Current.Item as FullTrack;
|
2022-07-01 22:04:17 +01:00
|
|
|
|
Logger.LogTrace("Caching play count for [{track}]", track.DisplayString());
|
2021-11-29 21:04:15 +00:00
|
|
|
|
|
|
|
|
|
var tasks = new Task[]
|
|
|
|
|
{
|
2021-12-19 13:44:22 +00:00
|
|
|
|
Db.StringSetAsync(Key.TrackPlayCount(e.Username, track.Name, track.Artists[0].Name), e.Track, expiry: CacheExpiry),
|
|
|
|
|
Db.StringSetAsync(Key.AlbumPlayCount(e.Username, track.Album.Name, track.Album.Artists[0].Name), e.Album, expiry: CacheExpiry),
|
|
|
|
|
Db.StringSetAsync(Key.ArtistPlayCount(e.Username, track.Artists[0].Name), e.Artist, expiry: CacheExpiry),
|
2021-11-29 21:04:15 +00:00
|
|
|
|
Db.StringSetAsync(Key.UserPlayCount(e.Username), e.User, expiry: CacheExpiry),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks);
|
|
|
|
|
|
2022-07-01 22:04:17 +01:00
|
|
|
|
Logger.LogDebug("Cached play count for [{track}]", track.DisplayString());
|
2021-11-29 21:04:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|