119 lines
3.5 KiB
C#
119 lines
3.5 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using NLog;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Lux.Data.Services
|
|
{
|
|
public class ImageCacheService : BaseServ
|
|
{
|
|
#region Public Constructors
|
|
|
|
public ImageCacheService(IConfiguration configuration) : base(configuration)
|
|
{
|
|
Log.Info("ImageCache Service Started");
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
/// <summary>
|
|
/// Message Dispatcher: oggetto comunicazione pub/sub via REDIS channels corrente
|
|
/// </summary>
|
|
public ISubscriber messageDisp
|
|
{
|
|
get
|
|
{
|
|
ISubscriber answ;
|
|
// se già valorizzato uso oggetto private...
|
|
if (_currSub != null)
|
|
{
|
|
answ = _currSub;
|
|
}
|
|
else
|
|
{
|
|
// init DB (sullo 0)
|
|
answ = redisConn.GetSubscriber();
|
|
_currSub = answ;
|
|
Log.Info($"Apertura di un Redis Message Subscriber");
|
|
}
|
|
// restituisco oggetto DB
|
|
return answ;
|
|
}
|
|
}
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
/// <summary>
|
|
/// Recupera img redis SVG x id porta richiesta
|
|
/// </summary>
|
|
/// <param name="imgId"></param>
|
|
public string LoadSvg(string imgId)
|
|
{
|
|
string rawVal = "";
|
|
// recupero img da cache
|
|
string currKey = $"{redisBaseKey}:{imgId.Replace("/", ":")}";
|
|
rawVal = redisDb.StringGet(currKey);
|
|
return rawVal;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Salva e invia su channel img redis SVG per il doc richiesto
|
|
/// </summary>
|
|
/// <param name="imgId"></param>
|
|
/// <param name="notifyChannel"></param>
|
|
/// <param name="svgContent"></param>
|
|
public void SaveSvg(string imgId, string svgContent)
|
|
{
|
|
string notifyChannel = $"svg-gen:{imgId}";
|
|
// invio notifica nuova imgsvg sul canale
|
|
SendMessage(notifyChannel, svgContent);
|
|
// salvo img in cache
|
|
string currKey = $"{redisBaseKey}:{imgId.Replace("/", ":")}";
|
|
redisDb.StringSet(currKey, svgContent);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Fields
|
|
|
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private string redisBaseKey = "Lux:Images";
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
/// <summary>
|
|
/// Oggetto subscriber x pubblicazione/sottoscrizione canali REDIS
|
|
/// </summary>
|
|
private ISubscriber _currSub { get; set; }
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
/// <summary>
|
|
/// invio su channel REDIS
|
|
/// </summary>
|
|
/// <param name="notifyChannel"></param>
|
|
/// <param name="message"></param>
|
|
private void SendMessage(string notifyChannel, string message)
|
|
{
|
|
// invio notifica tramite il canale richiesto
|
|
RedisChannel pubNotifyChannel = new RedisChannel(notifyChannel, RedisChannel.PatternMode.Literal);
|
|
messageDisp.Publish(pubNotifyChannel, message);
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |