not using icache? skeleton cache consumer, watcher username

This commit is contained in:
andy 2021-10-28 21:26:57 +01:00
parent 1714e3f911
commit 78fbc27e3a
7 changed files with 110 additions and 18 deletions

View File

@ -45,6 +45,7 @@ namespace Selector.CLI
//services.AddSingleton<IRefreshTokenFactoryProvider, RefreshTokenFactoryProvider>();
services.AddSingleton<IRefreshTokenFactoryProvider, CachingRefreshTokenFactoryProvider>();
// DB
if (config.DatabaseOptions.Enabled)
{
Console.WriteLine("> Adding Databse Context...");
@ -53,6 +54,7 @@ namespace Selector.CLI
);
}
// REDIS
if (config.RedisOptions.Enabled)
{
Console.WriteLine("> Configuring Redis...");
@ -65,11 +67,10 @@ namespace Selector.CLI
var connMulti = ConnectionMultiplexer.Connect(config.RedisOptions.ConnectionString);
services.AddSingleton(connMulti);
services.AddSingleton<IDatabaseAsync>(connMulti.GetDatabase());
services.AddSingleton<ICache<string>, RedisCache>();
services.AddTransient<IDatabaseAsync>(services => services.GetService<ConnectionMultiplexer>().GetDatabase());
}
// EQUAL
switch (config.Equality)
{
case EqualityChecker.Uri:

View File

@ -15,6 +15,9 @@
},
"Database": {
"enabled": true
},
"Redis": {
"enabled": true
}
},
"Logging": {

View File

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using SpotifyAPI.Web;
namespace Selector.Cache
{
public class CacheUpdater : IConsumer
{
private readonly IPlayerWatcher Watcher;
private readonly ILogger<CacheUpdater> Logger;
public CancellationToken CancelToken { get; set; }
public CacheUpdater(
IPlayerWatcher watcher,
ILogger<CacheUpdater> logger = null,
CancellationToken token = default
){
Watcher = watcher;
Logger = logger ?? NullLogger<CacheUpdater>.Instance;
CancelToken = token;
}
public void Callback(object sender, ListeningChangeEventArgs e)
{
if (e.Current is null) return;
Task.Run(() => { return AsyncCallback(e); }, CancelToken);
}
public async Task AsyncCallback(ListeningChangeEventArgs e)
{
if (e.Current.Item is FullTrack track)
{
}
else if (e.Current.Item is FullEpisode episode)
{
}
else
{
Logger.LogError($"Unknown item pulled from API [{e.Current.Item}]");
}
}
public void Subscribe(IWatcher watch = null)
{
var watcher = watch ?? Watcher ?? throw new ArgumentNullException("No watcher provided");
if (watcher is IPlayerWatcher watcherCast)
{
watcherCast.ItemChange += 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;
}
else
{
throw new ArgumentException("Provided watcher is not a PlayerWatcher");
}
}
}
}

View File

@ -109,9 +109,7 @@ namespace Selector.Web
var connMulti = ConnectionMultiplexer.Connect(config.RedisOptions.ConnectionString);
services.AddSingleton(connMulti);
services.AddSingleton<IDatabaseAsync>(connMulti.GetDatabase());
services.AddSingleton<ICache<string>, RedisCache>();
services.AddTransient<IDatabaseAsync>(services => services.GetService<ConnectionMultiplexer>().GetDatabase());
}
}

View File

@ -1,10 +1,15 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Selector": {
"Redis": {
"enabled": true
}
},
"AllowedHosts": "*"
}

View File

@ -13,6 +13,7 @@ namespace Selector
public abstract class BaseWatcher: IWatcher
{
protected readonly ILogger<BaseWatcher> Logger;
public string Username { get; set; }
public BaseWatcher(ILogger<BaseWatcher> logger = null)
{

View File

@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using SpotifyAPI.Web;
namespace Selector
@ -27,12 +27,16 @@ namespace Selector
var config = await spotifyFactory.GetConfig();
var client = new SpotifyClient(config);
var user = await client.UserProfile.Current();
return new PlayerWatcher(
client.Player,
Equal,
LoggerFactory?.CreateLogger<PlayerWatcher>(),
LoggerFactory?.CreateLogger<PlayerWatcher>() ?? NullLogger<PlayerWatcher>.Instance,
pollPeriod: pollPeriod
);
) {
Username = user.DisplayName
};
}
//else if (typeof(T).IsAssignableFrom(typeof(PlaylistWatcher)))
//{