Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9615 from gurkankaymak/8605-AtomicCacheApi
Updates cache api to make getOrElseUpdate methods atomic
- Loading branch information
Showing
8 changed files
with
150 additions
and
40 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
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
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
41 changes: 41 additions & 0 deletions
41
cache/play-caffeine-cache/src/main/scala/play/api/cache/caffeine/DefaultCaffeineExpiry.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,41 @@ | ||
/* | ||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package play.api.cache.caffeine | ||
|
||
import akka.annotation.InternalApi | ||
import com.github.benmanes.caffeine.cache.Expiry | ||
|
||
import scala.concurrent.duration.Duration | ||
import scala.concurrent.duration._ | ||
|
||
@InternalApi | ||
private[caffeine] class DefaultCaffeineExpiry extends Expiry[String, ExpirableCacheValue[Any]] { | ||
|
||
def expireAfterCreate(key: String, value: ExpirableCacheValue[Any], currentTime: Long): Long = { | ||
calculateExpirationTime(value.durationMaybe) | ||
} | ||
|
||
def expireAfterUpdate( | ||
key: String, | ||
value: ExpirableCacheValue[Any], | ||
currentTime: Long, | ||
currentDuration: Long | ||
): Long = { | ||
calculateExpirationTime(value.durationMaybe) | ||
} | ||
|
||
def expireAfterRead(key: String, value: ExpirableCacheValue[Any], currentTime: Long, currentDuration: Long): Long = { | ||
currentDuration | ||
} | ||
|
||
private def calculateExpirationTime(durationMaybe: Option[Duration]): Long = { | ||
durationMaybe match { | ||
case Some(duration) if duration.isFinite && duration.lteq(0.second) => 1.second.toNanos | ||
case Some(duration) if duration.isFinite => duration.toNanos | ||
case _ => Long.MaxValue | ||
} | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
cache/play-caffeine-cache/src/main/scala/play/api/cache/caffeine/ExpirableCacheValue.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,12 @@ | ||
/* | ||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package play.api.cache.caffeine | ||
|
||
import akka.annotation.InternalApi | ||
|
||
import scala.concurrent.duration.Duration | ||
|
||
@InternalApi | ||
private[caffeine] case class ExpirableCacheValue[V](value: V, durationMaybe: Option[Duration] = None) |
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
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
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