Skip to content

Commit 44b6111

Browse files
committed
Cache message-lookup result longer if a message was found
1 parent 3c3614a commit 44b6111

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

app/controllers/Api.scala

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@ import play.api.cache.Cached
99
import play.api.libs.json.Json._
1010
import play.api.mvc._
1111

12+
import scala.Function.unlift
1213
import scala.concurrent.ExecutionContext.Implicits.global
13-
import scala.concurrent.Future
14+
import scala.concurrent.duration._
15+
import scala.language.postfixOps
1416

1517
class Api @Inject() (cached: Cached) extends Controller {
1618

17-
def messageLookup(repoId: RepoId, query: String) = cached(s"$repoId $query") {
18-
Action.async {
19-
for {
20-
messagesOpt <- Project.byRepoId(repoId).mailingList.lookupMessage(query)
21-
} yield Ok(toJson(messagesOpt: Seq[MessageSummary]))
19+
val MessageFoundHeader = "X-submitGit-MessageFound"
20+
21+
val CacheForLongerIfMessageFound = unlift[ResponseHeader, Duration](_.headers.get(MessageFoundHeader).map(_ => 100 days))
22+
23+
def messageLookup(repoId: RepoId, query: String) = {
24+
cached.apply((_ => s"$repoId $query"): (RequestHeader => String), CacheForLongerIfMessageFound).default(3 seconds) {
25+
Action.async {
26+
for {
27+
messagesOpt <- Project.byRepoId(repoId).mailingList.lookupMessage(query)
28+
} yield {
29+
Ok(toJson(messagesOpt: Seq[MessageSummary])).withHeaders(messagesOpt.map(_ => MessageFoundHeader -> "true") : _*)
30+
}
31+
}
2232
}
2333
}
2434

0 commit comments

Comments
 (0)