@@ -22,7 +22,9 @@ import java.time.chrono.IsoChronology
22
22
import java .time .format .{DateTimeFormatter , DateTimeFormatterBuilder , ResolverStyle }
23
23
import java .time .temporal .{ChronoField , TemporalAccessor , TemporalQueries }
24
24
import java .util .Locale
25
- import java .util .concurrent .ConcurrentHashMap
25
+ import java .util .concurrent .{Callable , TimeUnit }
26
+
27
+ import com .google .common .cache .CacheBuilder
26
28
27
29
trait DateTimeFormatterHelper {
28
30
protected def toInstantWithZoneId (temporalAccessor : TemporalAccessor , zoneId : ZoneId ): Instant = {
@@ -39,11 +41,12 @@ trait DateTimeFormatterHelper {
39
41
}
40
42
41
43
object DateTimeFormatterHelper {
42
- private val cache = new ConcurrentHashMap [(String , Locale ), DateTimeFormatter ]()
44
+ private val cache = CacheBuilder .newBuilder()
45
+ .initialCapacity(8 )
46
+ .maximumSize(128 )
47
+ .expireAfterAccess(1 , TimeUnit .HOURS )
48
+ .build[(String , Locale ), DateTimeFormatter ]()
43
49
44
- def getFormatter (pattern : String , locale : Locale ): DateTimeFormatter = {
45
- cache.computeIfAbsent((pattern, locale), _ => buildFormatter(pattern, locale))
46
- }
47
50
48
51
private def buildFormatter (pattern : String , locale : Locale ): DateTimeFormatter = {
49
52
new DateTimeFormatterBuilder ()
@@ -58,4 +61,12 @@ object DateTimeFormatterHelper {
58
61
.withChronology(IsoChronology .INSTANCE )
59
62
.withResolverStyle(ResolverStyle .STRICT )
60
63
}
64
+
65
+ def getFormatter (pattern : String , locale : Locale ): DateTimeFormatter = {
66
+ cache.get(
67
+ (pattern, locale),
68
+ new Callable [DateTimeFormatter ]() {
69
+ override def call = buildFormatter(pattern, locale)
70
+ })
71
+ }
61
72
}
0 commit comments