Gestione caching come filter x WebAPI

This commit is contained in:
Samuele E. Locatelli
2020-09-22 22:44:36 +02:00
parent 304fbb6ba0
commit 4d39ca94f5
3 changed files with 77 additions and 6 deletions
+23
View File
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http.Filters;
namespace MP_IO.Controllers.WebAPI
{
public class CacheFilter : ActionFilterAttribute
{
public int TimeDuration { get; set; }
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
actionExecutedContext.Response.Headers.CacheControl = new CacheControlHeaderValue
{
MaxAge = TimeSpan.FromSeconds(TimeDuration),
MustRevalidate = true,
Public = true
};
}
}
}
+53 -6
View File
@@ -1,4 +1,6 @@
using System;
using MapoDb;
using SteamWare;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
@@ -9,18 +11,63 @@ namespace MP_IO.Controllers.WebAPI
{
public class IOBController : ApiController
{
// GET api/IOB
//[ResponseCache(Duration = 10)]
/// <summary>
/// GET api/IOB
/// è un check alive del server
/// </summary>
/// <returns></returns>
[CacheFilter(TimeDuration = 10)]
[HttpGet]
public string Get()
{
if (memLayer.ML.CRB("IOB_RedEnab"))
{
// conto la richiesta nel contatore REDIS
long nCall = memLayer.ML.setRCntI(DataLayer.mHash("COUNT:pCall:IOB_INDEX"));
//... se == nCall2Log scrivo su log e resetto
long nCall2Log = memLayer.ML.cdvi("nCall2Log");
if (nCall >= nCall2Log)
{
// loggo
logger.lg.scriviLog(string.Format("IOB_INDEX: effettuate {0} call", nCall), tipoLog.INFO);
// resetto!
memLayer.ML.resetRCnt(DataLayer.mHash("COUNT:pCall:IOB_INDEX"));
}
}
return "OK";
}
// GET api/<controller>/5
public string Get(int id)
/// <summary>
/// GET api/IOB/SIMUL_03
/// Verifica stato enabled di un determinato IOB
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[CacheFilter(TimeDuration = 5)]
[HttpGet]
public string Get(string id)
{
return "value";
string answ = "ND";
// se id nullo --> KO!
if (id == null)
{
answ = "KO";
}
else
{
try
{
DataLayer DataLayerObj = new DataLayer();
// salvo risposta!
answ = DataLayerObj.insEnab(id) ? "OK" : "NO";
}
catch (Exception exc)
{
logger.lg.scriviLog(string.Format("Errore in enabled{0}{1}", Environment.NewLine, exc));
answ = "NO";
}
}
return answ;
}
// POST api/<controller>