Using json serialiser source generator, removed unused cache components
This commit is contained in:
parent
7a56f7f586
commit
e5d93aa27d
@ -45,7 +45,7 @@ namespace Selector.Cache
|
|||||||
|
|
||||||
public async Task AsyncCacheCallback(AnalysedTrack e)
|
public async Task AsyncCacheCallback(AnalysedTrack e)
|
||||||
{
|
{
|
||||||
var payload = JsonSerializer.Serialize(e.Features);
|
var payload = JsonSerializer.Serialize(e.Features, JsonContext.Default.TrackAudioFeatures);
|
||||||
|
|
||||||
Logger.LogTrace($"Caching current for [{e.Track.DisplayString()}]");
|
Logger.LogTrace($"Caching current for [{e.Track.DisplayString()}]");
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ namespace Selector.Cache
|
|||||||
|
|
||||||
public async Task AsyncCallback(ListeningChangeEventArgs e)
|
public async Task AsyncCallback(ListeningChangeEventArgs e)
|
||||||
{
|
{
|
||||||
var payload = JsonSerializer.Serialize((CurrentlyPlayingDTO) e);
|
var payload = JsonSerializer.Serialize((CurrentlyPlayingDTO) e, JsonContext.Default.CurrentlyPlayingDTO);
|
||||||
|
|
||||||
Logger.LogTrace($"Caching current for [{e.Id}/{e.SpotifyUsername}]");
|
Logger.LogTrace($"Caching current for [{e.Id}/{e.SpotifyUsername}]");
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ namespace Selector.Cache
|
|||||||
|
|
||||||
public async Task AsyncCallback(ListeningChangeEventArgs e)
|
public async Task AsyncCallback(ListeningChangeEventArgs e)
|
||||||
{
|
{
|
||||||
var payload = JsonSerializer.Serialize((CurrentlyPlayingDTO) e);
|
var payload = JsonSerializer.Serialize((CurrentlyPlayingDTO) e, JsonContext.Default.CurrentlyPlayingDTO);
|
||||||
|
|
||||||
Logger.LogTrace($"Publishing current for [{e.Id}/{e.SpotifyUsername}]");
|
Logger.LogTrace($"Publishing current for [{e.Id}/{e.SpotifyUsername}]");
|
||||||
|
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Selector.Cache
|
|
||||||
{
|
|
||||||
public interface ICache<TKey>
|
|
||||||
{
|
|
||||||
public Task<string> Get(TKey key);
|
|
||||||
public Task<bool> Set(TKey key, string value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Is this unnecessary?
|
|
||||||
/// </summary>
|
|
||||||
/// <typeparam name="T"></typeparam>
|
|
||||||
/// <typeparam name="TKey"></typeparam>
|
|
||||||
public interface ICacheSerialiser<T, TKey>
|
|
||||||
{
|
|
||||||
public Task<bool> Write(TKey key, T obj, ICache<TKey> cache);
|
|
||||||
public Task<T> Read(TKey key, ICache<TKey> cache);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.Json;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
using SpotifyAPI.Web;
|
|
||||||
|
|
||||||
namespace Selector.Cache
|
|
||||||
{
|
|
||||||
public static class JsonSerialiser
|
|
||||||
{
|
|
||||||
public static async Task<T> Read<T>(this ICache<string> cache, string key)
|
|
||||||
=> JsonSerializer.Deserialize<T>(await cache.Get(key));
|
|
||||||
|
|
||||||
public static async Task<bool> Write<T>(this ICache<string> cache, string key, T obj)
|
|
||||||
=> await cache.Set(key, JsonSerializer.Serialize(obj));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Selector.Cache
|
|
||||||
{
|
|
||||||
public class CurrentPlaying
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
using StackExchange.Redis;
|
|
||||||
|
|
||||||
namespace Selector.Cache
|
|
||||||
{
|
|
||||||
public class RedisCache : ICache<string>
|
|
||||||
{
|
|
||||||
private readonly IDatabaseAsync Db;
|
|
||||||
|
|
||||||
public RedisCache(
|
|
||||||
IDatabaseAsync db
|
|
||||||
) {
|
|
||||||
Db = db;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<string> Get(string key) => (await Db.StringGetAsync(key)).ToString();
|
|
||||||
|
|
||||||
public async Task<bool> Set(string key, string value) => await Db.StringSetAsync(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
@ -42,7 +42,7 @@ namespace Selector.Cache
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var deserialised = JsonSerializer.Deserialize<TrackAudioFeatures>(track);
|
var deserialised = JsonSerializer.Deserialize(track, JsonContext.Default.TrackAudioFeatures);
|
||||||
return deserialised;
|
return deserialised;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
Selector.Event/CacheJsonContext.cs
Normal file
11
Selector.Event/CacheJsonContext.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace Selector.Events
|
||||||
|
{
|
||||||
|
[JsonSerializable(typeof(LastfmChange))]
|
||||||
|
[JsonSerializable(typeof(SpotifyLinkChange))]
|
||||||
|
[JsonSerializable(typeof((string, CurrentlyPlayingDTO)))]
|
||||||
|
public partial class CacheJsonContext: JsonSerializerContext
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@ -41,7 +41,7 @@ namespace Selector.Events
|
|||||||
{
|
{
|
||||||
var userId = Key.Param(message.Channel);
|
var userId = Key.Param(message.Channel);
|
||||||
|
|
||||||
var deserialised = JsonSerializer.Deserialize<LastfmChange>(message.Message);
|
var deserialised = JsonSerializer.Deserialize(message.Message, CacheJsonContext.Default.LastfmChange);
|
||||||
Logger.LogDebug("Received new Last.fm username event for [{userId}]", deserialised.UserId);
|
Logger.LogDebug("Received new Last.fm username event for [{userId}]", deserialised.UserId);
|
||||||
|
|
||||||
if (!userId.Equals(deserialised.UserId))
|
if (!userId.Equals(deserialised.UserId))
|
||||||
@ -83,7 +83,7 @@ namespace Selector.Events
|
|||||||
|
|
||||||
UserEvent.LastfmCredChange += async (o, e) =>
|
UserEvent.LastfmCredChange += async (o, e) =>
|
||||||
{
|
{
|
||||||
var payload = JsonSerializer.Serialize(e);
|
var payload = JsonSerializer.Serialize(e, CacheJsonContext.Default.LastfmChange);
|
||||||
await Subscriber.PublishAsync(Key.UserLastfm(e.UserId), payload);
|
await Subscriber.PublishAsync(Key.UserLastfm(e.UserId), payload);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -34,10 +34,10 @@ namespace Selector.Events
|
|||||||
{
|
{
|
||||||
var userId = Key.Param(message.Channel);
|
var userId = Key.Param(message.Channel);
|
||||||
|
|
||||||
var deserialised = JsonSerializer.Deserialize<CurrentlyPlayingDTO>(message.Message);
|
var deserialised = JsonSerializer.Deserialize(message.Message, JsonContext.Default.CurrentlyPlayingDTO);
|
||||||
Logger.LogDebug("Received new currently playing [{username}]", deserialised.Username);
|
Logger.LogDebug("Received new currently playing [{username}]", deserialised.Username);
|
||||||
|
|
||||||
UserEvent.OnCurrentlyPlayingChange(this, userId, deserialised);
|
UserEvent.OnCurrentlyPlayingChange(this, deserialised);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
@ -71,10 +71,8 @@ namespace Selector.Events
|
|||||||
|
|
||||||
UserEvent.CurrentlyPlaying += async (o, e) =>
|
UserEvent.CurrentlyPlaying += async (o, e) =>
|
||||||
{
|
{
|
||||||
(string id, CurrentlyPlayingDTO args) = e;
|
var payload = JsonSerializer.Serialize(e, JsonContext.Default.CurrentlyPlayingDTO);
|
||||||
|
await Subscriber.PublishAsync(Key.CurrentlyPlaying(e.UserId), payload);
|
||||||
var payload = JsonSerializer.Serialize(e);
|
|
||||||
await Subscriber.PublishAsync(Key.CurrentlyPlaying(id), payload);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
|
@ -41,7 +41,7 @@ namespace Selector.Events
|
|||||||
{
|
{
|
||||||
var userId = Key.Param(message.Channel);
|
var userId = Key.Param(message.Channel);
|
||||||
|
|
||||||
var deserialised = JsonSerializer.Deserialize<SpotifyLinkChange>(message.Message);
|
var deserialised = JsonSerializer.Deserialize(message.Message, CacheJsonContext.Default.SpotifyLinkChange);
|
||||||
Logger.LogDebug("Received new Spotify link event for [{userId}]", deserialised.UserId);
|
Logger.LogDebug("Received new Spotify link event for [{userId}]", deserialised.UserId);
|
||||||
|
|
||||||
if (!userId.Equals(deserialised.UserId))
|
if (!userId.Equals(deserialised.UserId))
|
||||||
@ -87,7 +87,7 @@ namespace Selector.Events
|
|||||||
|
|
||||||
UserEvent.SpotifyLinkChange += async (o, e) =>
|
UserEvent.SpotifyLinkChange += async (o, e) =>
|
||||||
{
|
{
|
||||||
var payload = JsonSerializer.Serialize(e);
|
var payload = JsonSerializer.Serialize(e, CacheJsonContext.Default.SpotifyLinkChange);
|
||||||
await Subscriber.PublishAsync(Key.UserSpotify(e.UserId), payload);
|
await Subscriber.PublishAsync(Key.UserSpotify(e.UserId), payload);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Logging.Abstractions;
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
|
|
||||||
using Selector.Events;
|
namespace Selector.Events
|
||||||
|
|
||||||
namespace Selector
|
|
||||||
{
|
{
|
||||||
public class UserEventFirer : IConsumer
|
public class UserEventFirer : IConsumer
|
||||||
{
|
{
|
||||||
@ -47,7 +45,7 @@ namespace Selector
|
|||||||
{
|
{
|
||||||
Logger.LogDebug("Firing now playing event on user bus [{username}/{userId}]", e.SpotifyUsername, e.Id);
|
Logger.LogDebug("Firing now playing event on user bus [{username}/{userId}]", e.SpotifyUsername, e.Id);
|
||||||
|
|
||||||
UserEvent.OnCurrentlyPlayingChange(this, e.Id, (CurrentlyPlayingDTO) e);
|
UserEvent.OnCurrentlyPlayingChange(this, (CurrentlyPlayingDTO) e);
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
using System;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Selector.Events;
|
|
||||||
|
|
||||||
namespace Selector
|
namespace Selector.Events
|
||||||
{
|
{
|
||||||
public interface IUserEventFirerFactory
|
public interface IUserEventFirerFactory
|
||||||
{
|
{
|
||||||
|
@ -12,7 +12,7 @@ namespace Selector.Events
|
|||||||
public event EventHandler<SpotifyLinkChange> SpotifyLinkChange;
|
public event EventHandler<SpotifyLinkChange> SpotifyLinkChange;
|
||||||
public event EventHandler<LastfmChange> LastfmCredChange;
|
public event EventHandler<LastfmChange> LastfmCredChange;
|
||||||
|
|
||||||
public event EventHandler<(string, CurrentlyPlayingDTO)> CurrentlyPlaying;
|
public event EventHandler<CurrentlyPlayingDTO> CurrentlyPlaying;
|
||||||
|
|
||||||
public UserEventBus(ILogger<UserEventBus> logger)
|
public UserEventBus(ILogger<UserEventBus> logger)
|
||||||
{
|
{
|
||||||
@ -37,10 +37,10 @@ namespace Selector.Events
|
|||||||
LastfmCredChange?.Invoke(sender, args);
|
LastfmCredChange?.Invoke(sender, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnCurrentlyPlayingChange(object sender, string userId, CurrentlyPlayingDTO args)
|
public void OnCurrentlyPlayingChange(object sender, CurrentlyPlayingDTO args)
|
||||||
{
|
{
|
||||||
Logger.LogTrace("Firing currently playing event [{usernamne}/{userId}]", args?.Username, userId);
|
Logger.LogTrace("Firing currently playing event [{usernamne}/{userId}]", args?.Username, args.UserId);
|
||||||
CurrentlyPlaying?.Invoke(sender, (userId, args));
|
CurrentlyPlaying?.Invoke(sender, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ namespace Selector.Web.Hubs
|
|||||||
var nowPlaying = await Cache.StringGetAsync(Key.CurrentlyPlaying(Context.UserIdentifier));
|
var nowPlaying = await Cache.StringGetAsync(Key.CurrentlyPlaying(Context.UserIdentifier));
|
||||||
if (nowPlaying != RedisValue.Null)
|
if (nowPlaying != RedisValue.Null)
|
||||||
{
|
{
|
||||||
var deserialised = JsonSerializer.Deserialize<CurrentlyPlayingDTO>(nowPlaying);
|
var deserialised = JsonSerializer.Deserialize(nowPlaying, JsonContext.Default.CurrentlyPlayingDTO);
|
||||||
await Clients.Caller.OnNewPlaying(deserialised);
|
await Clients.Caller.OnNewPlaying(deserialised);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,10 +28,9 @@ namespace Selector.Web.Service
|
|||||||
|
|
||||||
UserEvent.CurrentlyPlaying += async (o, args) =>
|
UserEvent.CurrentlyPlaying += async (o, args) =>
|
||||||
{
|
{
|
||||||
(string id, CurrentlyPlayingDTO e) = args;
|
Logger.LogDebug("Passing now playing event to SignalR hub [{userId}]", args.UserId);
|
||||||
Logger.LogDebug("Passing now playing event to SignalR hub [{userId}]", id);
|
|
||||||
|
|
||||||
await Hub.Clients.User(id).OnNewPlaying(e);
|
await Hub.Clients.User(args.UserId).OnNewPlaying(args);
|
||||||
};
|
};
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
|
12
Selector/JsonContext.cs
Normal file
12
Selector/JsonContext.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using SpotifyAPI.Web;
|
||||||
|
|
||||||
|
namespace Selector
|
||||||
|
{
|
||||||
|
[JsonSerializable(typeof(CurrentlyPlayingDTO))]
|
||||||
|
[JsonSerializable(typeof(TrackAudioFeatures))]
|
||||||
|
[JsonSerializable(typeof(ListeningChangeEventArgs))]
|
||||||
|
public partial class JsonContext: JsonSerializerContext
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,7 @@ namespace Selector {
|
|||||||
public class CurrentlyPlayingDTO {
|
public class CurrentlyPlayingDTO {
|
||||||
public CurrentlyPlayingContextDTO Context { get; set; }
|
public CurrentlyPlayingContextDTO Context { get; set; }
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
|
public string UserId { get; set; }
|
||||||
|
|
||||||
public FullTrack Track { get; set; }
|
public FullTrack Track { get; set; }
|
||||||
public FullEpisode Episode { get; set; }
|
public FullEpisode Episode { get; set; }
|
||||||
@ -19,7 +20,8 @@ namespace Selector {
|
|||||||
{
|
{
|
||||||
Context = e.Current,
|
Context = e.Current,
|
||||||
Username = e.SpotifyUsername,
|
Username = e.SpotifyUsername,
|
||||||
Track = track
|
Track = track,
|
||||||
|
UserId = e.Id
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
else if (e.Current.Item is FullEpisode episode)
|
else if (e.Current.Item is FullEpisode episode)
|
||||||
@ -28,7 +30,8 @@ namespace Selector {
|
|||||||
{
|
{
|
||||||
Context = e.Current,
|
Context = e.Current,
|
||||||
Username = e.SpotifyUsername,
|
Username = e.SpotifyUsername,
|
||||||
Episode = episode
|
Episode = episode,
|
||||||
|
UserId = e.Id
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
Loading…
Reference in New Issue
Block a user