From ea8241dc4841ff410c5c66b1e1cf81c901eed9ef Mon Sep 17 00:00:00 2001 From: Jonathan Cole Date: Fri, 6 Sep 2024 17:48:40 -0400 Subject: [PATCH] use new caching model for flag color --- .../neptune/Core/CacheableFactory.scala | 2 +- .../Api/Endpoints/Public/FlagColor.scala | 43 +++++-------------- .../Entities/cacheable/CacheKeys.scala | 1 + .../Entities/cacheable/FlagColor.scala | 17 ++++++++ 4 files changed, 30 insertions(+), 33 deletions(-) create mode 100644 app/org/sailcbi/APIServer/Entities/cacheable/FlagColor.scala diff --git a/app/com/coleji/neptune/Core/CacheableFactory.scala b/app/com/coleji/neptune/Core/CacheableFactory.scala index d6eb3e73..7c4b6bee 100644 --- a/app/com/coleji/neptune/Core/CacheableFactory.scala +++ b/app/com/coleji/neptune/Core/CacheableFactory.scala @@ -29,7 +29,7 @@ abstract class CacheableFactory[T_KeyConfig, T_Result] { val cb = rc.cb val key = calculateKey(config) // TODO: some way to sync per cache key - synchronized { + this.synchronized { val cacheMetadata = getMetaData(cb, key) if (isExpired(cacheMetadata) || !cb.peek(key)) { populate(rc, config) diff --git a/app/org/sailcbi/APIServer/Api/Endpoints/Public/FlagColor.scala b/app/org/sailcbi/APIServer/Api/Endpoints/Public/FlagColor.scala index 01aed190..0944eea5 100644 --- a/app/org/sailcbi/APIServer/Api/Endpoints/Public/FlagColor.scala +++ b/app/org/sailcbi/APIServer/Api/Endpoints/Public/FlagColor.scala @@ -1,43 +1,22 @@ package org.sailcbi.APIServer.Api.Endpoints.Public -import com.coleji.neptune.API.{CacheableResultFromPreparedQuery, ParamsObject} -import com.coleji.neptune.Core.{CacheBroker, ParsedRequest, PermissionsAuthority} -import org.sailcbi.APIServer.Api.Endpoints.Public.FlagColor.FlagColorParamsObject -import org.sailcbi.APIServer.IO.PreparedQueries.Public.{GetFlagColor, GetFlagColorResult} +import com.coleji.neptune.Core.{ParsedRequest, PermissionsAuthority} import org.sailcbi.APIServer.UserTypes.PublicRequestCache -import play.api.mvc.{Action, AnyContent} +import play.api.mvc.{Action, AnyContent, InjectedController} -import java.time.LocalDateTime import javax.inject.Inject -import scala.concurrent.ExecutionContext +import scala.concurrent.{ExecutionContext, Future} -class FlagColor @Inject()(implicit val exec: ExecutionContext) - extends CacheableResultFromPreparedQuery[FlagColorParamsObject, GetFlagColorResult] { +class FlagColor @Inject()(implicit val exec: ExecutionContext) extends InjectedController { def get()(implicit PA: PermissionsAuthority): Action[AnyContent] = Action.async { request => PA.withRequestCache(PublicRequestCache)(None, ParsedRequest(request), rc => { - val cb: CacheBroker = rc.cb - - getFuture(cb, rc, new FlagColorParamsObject, new GetFlagColor).map(s => { - val r = ".*\\[\\[\"([A-Z])\"\\]\\].*".r - val f = s.toString match { - case r(flag: String) => flag - case _ => "C" - } - - Ok("var FLAG_COLOR = \"" + f + "\"") - }) + val flagColor = org.sailcbi.APIServer.Entities.cacheable.FlagColor.get(rc, null)._1 match { + case "G" => "G" + case "Y" => "Y" + case "R" => "R" + case _ => "C" + } + Future(Ok("var FLAG_COLOR = \"" + flagColor + "\"")) }) } - - def getCacheBrokerKey(params: FlagColorParamsObject): CacheKey = "flag-color" - - def getExpirationTime: LocalDateTime = { - LocalDateTime.now.plusSeconds(5) - } -} - -object FlagColor { - - class FlagColorParamsObject extends ParamsObject - } diff --git a/app/org/sailcbi/APIServer/Entities/cacheable/CacheKeys.scala b/app/org/sailcbi/APIServer/Entities/cacheable/CacheKeys.scala index f5a693e0..88427aca 100644 --- a/app/org/sailcbi/APIServer/Entities/cacheable/CacheKeys.scala +++ b/app/org/sailcbi/APIServer/Entities/cacheable/CacheKeys.scala @@ -24,4 +24,5 @@ object CacheKeys { def sunset(config: SunsetCacheKey): String = s"sunset-${config.year}-${config.month}-${config.day.getOrElse("none")}" def datetimeRange(config: DatetimeRangeCacheKey): String = s"datetime-range-${config.startDate.format(DateTimeFormatter.ISO_DATE)}-${config.endDate.format(DateTimeFormatter.ISO_DATE)}-${config.rangeType}" def yearlyDateAndItem(config: YearlyDateAndItemCacheKey): String = s"yearly-date-${config.year}-${config.itemAlias}" + def flagColor = "flag-color" } diff --git a/app/org/sailcbi/APIServer/Entities/cacheable/FlagColor.scala b/app/org/sailcbi/APIServer/Entities/cacheable/FlagColor.scala new file mode 100644 index 00000000..b0a06df5 --- /dev/null +++ b/app/org/sailcbi/APIServer/Entities/cacheable/FlagColor.scala @@ -0,0 +1,17 @@ +package org.sailcbi.APIServer.Entities.cacheable + +import com.coleji.neptune.Core.{CacheableFactory, PermissionsAuthority, RequestCache, UnlockedRequestCache} +import org.sailcbi.APIServer.IO.PreparedQueries.Public.GetFlagColor + +import java.time.Duration + +object FlagColor extends CacheableFactory[Null, String] { + override protected val lifetime: Duration = Duration.ofSeconds(5) + + override protected def calculateKey(config: Null): String = CacheKeys.flagColor + + override protected def generateResult(rc: RequestCache, config: Null): String = { + println("CALCULATING FLAG") + rc.executePreparedQueryForSelect(new GetFlagColor).map(_.color).head + } +}