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

View File

@ -15,6 +15,9 @@
}, },
"Database": { "Database": {
"enabled": true "enabled": true
},
"Redis": {
"enabled": true
} }
}, },
"Logging": { "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); var connMulti = ConnectionMultiplexer.Connect(config.RedisOptions.ConnectionString);
services.AddSingleton(connMulti); services.AddSingleton(connMulti);
services.AddSingleton<IDatabaseAsync>(connMulti.GetDatabase()); services.AddTransient<IDatabaseAsync>(services => services.GetService<ConnectionMultiplexer>().GetDatabase());
services.AddSingleton<ICache<string>, RedisCache>();
} }
} }

View File

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

View File

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

View File

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