2021-10-28 21:26:57 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-10-28 23:05:07 +01:00
|
|
|
using System.Text.Json;
|
2021-10-28 21:26:57 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
2021-10-29 22:35:34 +01:00
|
|
|
|
2021-10-28 23:05:07 +01:00
|
|
|
using StackExchange.Redis;
|
2021-10-28 21:26:57 +01:00
|
|
|
|
|
|
|
namespace Selector.Cache
|
|
|
|
{
|
2021-10-28 23:05:07 +01:00
|
|
|
public class CacheWriter : IConsumer
|
2021-10-28 21:26:57 +01:00
|
|
|
{
|
|
|
|
private readonly IPlayerWatcher Watcher;
|
2021-10-28 23:05:07 +01:00
|
|
|
private readonly IDatabaseAsync Db;
|
|
|
|
private readonly ILogger<CacheWriter> Logger;
|
2021-11-30 20:38:26 +00:00
|
|
|
public TimeSpan CacheExpiry { get; set; } = TimeSpan.FromMinutes(20);
|
2021-10-28 21:26:57 +01:00
|
|
|
|
|
|
|
public CancellationToken CancelToken { get; set; }
|
|
|
|
|
2021-10-28 23:05:07 +01:00
|
|
|
public CacheWriter(
|
2021-10-28 21:26:57 +01:00
|
|
|
IPlayerWatcher watcher,
|
2021-10-28 23:05:07 +01:00
|
|
|
IDatabaseAsync db,
|
|
|
|
ILogger<CacheWriter> logger = null,
|
2021-10-28 21:26:57 +01:00
|
|
|
CancellationToken token = default
|
|
|
|
){
|
|
|
|
Watcher = watcher;
|
2021-10-28 23:05:07 +01:00
|
|
|
Db = db;
|
|
|
|
Logger = logger ?? NullLogger<CacheWriter>.Instance;
|
2021-10-28 21:26:57 +01:00
|
|
|
CancelToken = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Callback(object sender, ListeningChangeEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Current is null) return;
|
2021-10-29 22:35:34 +01:00
|
|
|
|
2021-11-30 20:38:26 +00:00
|
|
|
Task.Run(async () => { await AsyncCallback(e); }, CancelToken);
|
2021-10-28 21:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task AsyncCallback(ListeningChangeEventArgs e)
|
|
|
|
{
|
2021-10-29 22:35:34 +01:00
|
|
|
var payload = JsonSerializer.Serialize((CurrentlyPlayingDTO) e);
|
|
|
|
|
2021-11-05 18:41:45 +00:00
|
|
|
Logger.LogTrace($"Caching current for [{e.Id}/{e.SpotifyUsername}]");
|
2021-10-29 22:35:34 +01:00
|
|
|
|
2021-11-11 19:54:28 +00:00
|
|
|
var resp = await Db.StringSetAsync(Key.CurrentlyPlaying(e.Id), payload, expiry: CacheExpiry);
|
2021-10-29 22:35:34 +01:00
|
|
|
|
2021-11-05 18:41:45 +00:00
|
|
|
Logger.LogDebug($"Cached current for [{e.Id}/{e.SpotifyUsername}], {(resp ? "value set" : "value NOT set")}");
|
2021-10-29 22:35:34 +01:00
|
|
|
|
2021-10-28 21:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Subscribe(IWatcher watch = null)
|
|
|
|
{
|
|
|
|
var watcher = watch ?? Watcher ?? throw new ArgumentNullException("No watcher provided");
|
|
|
|
|
|
|
|
if (watcher is IPlayerWatcher watcherCast)
|
|
|
|
{
|
|
|
|
watcherCast.ItemChange += Callback;
|
2021-11-30 20:38:26 +00:00
|
|
|
watcherCast.PlayingChange += Callback;
|
2021-10-28 21:26:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Provided watcher is not a PlayerWatcher");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Unsubscribe(IWatcher watch = null)
|
|
|
|
{
|
|
|
|
var watcher = watch ?? Watcher ?? throw new ArgumentNullException("No watcher provided");
|
|
|
|
|
|
|
|
if (watcher is IPlayerWatcher watcherCast)
|
|
|
|
{
|
|
|
|
watcherCast.ItemChange -= Callback;
|
2021-11-30 20:38:26 +00:00
|
|
|
watcherCast.PlayingChange -= Callback;
|
2021-10-28 21:26:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Provided watcher is not a PlayerWatcher");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|