-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use new caching model for flag color
- Loading branch information
Showing
4 changed files
with
30 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 11 additions & 32 deletions
43
app/org/sailcbi/APIServer/Api/Endpoints/Public/FlagColor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
app/org/sailcbi/APIServer/Entities/cacheable/FlagColor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |