Skip to content

Commit

Permalink
use new caching model for flag color
Browse files Browse the repository at this point in the history
  • Loading branch information
coleji committed Sep 6, 2024
1 parent 672bf65 commit ea8241d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/com/coleji/neptune/Core/CacheableFactory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 11 additions & 32 deletions app/org/sailcbi/APIServer/Api/Endpoints/Public/FlagColor.scala
Original file line number Diff line number Diff line change
@@ -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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
17 changes: 17 additions & 0 deletions app/org/sailcbi/APIServer/Entities/cacheable/FlagColor.scala
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit ea8241d

Please sign in to comment.