Selector/Selector.Cache/Consumer/CacheWriterConsumer.cs

84 lines
2.6 KiB
C#
Raw Normal View History

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