forked from akka/akka-http
/
CachingSettings.scala
77 lines (66 loc) · 2.59 KB
/
CachingSettings.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright (C) 2017-2019 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.http.caching.scaladsl
import akka.annotation.{ DoNotInherit, InternalApi }
import akka.http.caching.javadsl
import akka.http.impl.util.SettingsCompanion
import com.typesafe.config.Config
import scala.concurrent.duration.Duration
import akka.http.impl.util._
/**
* Public API but not intended for subclassing
*/
@DoNotInherit
abstract class CachingSettings private[http] () extends javadsl.CachingSettings { self: CachingSettingsImpl ⇒
override def lfuCacheSettings: LfuCacheSettings
// overloads for idiomatic Scala use
def withLfuCacheSettings(newSettings: LfuCacheSettings): CachingSettings =
self.copy(lfuCacheSettings = newSettings)
}
/** INTERNAL API */
@InternalApi
private[http] final case class CachingSettingsImpl(lfuCacheSettings: LfuCacheSettings) extends CachingSettings {
override def productPrefix = "CachingSettings"
}
/**
* Public API but not intended for subclassing
*/
@DoNotInherit
abstract class LfuCacheSettings private[http] () extends javadsl.LfuCacheSettings { self: LfuCacheSettingsImpl ⇒
def maxCapacity: Int
def initialCapacity: Int
def timeToLive: Duration
def timeToIdle: Duration
final def getMaxCapacity: Int = maxCapacity
final def getInitialCapacity: Int = initialCapacity
final def getTimeToLive: Duration = timeToLive
final def getTimeToIdle: Duration = timeToIdle
override def withMaxCapacity(newMaxCapacity: Int): LfuCacheSettings = self.copy(maxCapacity = newMaxCapacity)
override def withInitialCapacity(newInitialCapacity: Int): LfuCacheSettings = self.copy(initialCapacity = newInitialCapacity)
override def withTimeToLive(newTimeToLive: Duration): LfuCacheSettings = self.copy(timeToLive = newTimeToLive)
override def withTimeToIdle(newTimeToIdle: Duration): LfuCacheSettings = self.copy(timeToIdle = newTimeToIdle)
}
/** INTERNAL API */
@InternalApi
private[http] final case class LfuCacheSettingsImpl(
maxCapacity: Int,
initialCapacity: Int,
timeToLive: Duration,
timeToIdle: Duration
) extends LfuCacheSettings {
override def productPrefix = "LfuCacheSettings"
}
object CachingSettings extends SettingsCompanion[CachingSettings]("akka.http.caching") {
def fromSubConfig(root: Config, c: Config) = {
val lfuConfig = c.getConfig("lfu-cache")
CachingSettingsImpl(
LfuCacheSettingsImpl(
lfuConfig getInt "max-capacity",
lfuConfig getInt "initial-capacity",
lfuConfig getPotentiallyInfiniteDuration "time-to-live",
lfuConfig getPotentiallyInfiniteDuration "time-to-idle"
)
)
}
}