Skip to content

Commit e4a3e90

Browse files
committed
update version api cache using atomic reference
1 parent 7d58780 commit e4a3e90

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

core/src/main/scala/app/softnetwork/elastic/client/VersionApi.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,31 @@ package app.softnetwork.elastic.client
1818

1919
import app.softnetwork.elastic.client.result.{ElasticFailure, ElasticResult, ElasticSuccess}
2020

21+
import java.util.concurrent.atomic.AtomicReference
22+
2123
trait VersionApi extends ElasticClientHelpers { _: SerializationApi =>
2224

2325
// ========================================================================
2426
// PUBLIC METHODS
2527
// ========================================================================
2628

2729
// Cache ES version (avoids calling it every time)
28-
@volatile private var cachedVersion: Option[String] = None
30+
private val cachedVersion = new AtomicReference[Option[String]](None)
2931

3032
/** Get Elasticsearch version.
3133
* @return
3234
* the Elasticsearch version
3335
*/
3436
def version: ElasticResult[String] = {
35-
cachedVersion match {
37+
cachedVersion.get match {
3638
case Some(version) =>
3739
ElasticSuccess(version)
3840
case None =>
3941
executeVersion() match {
40-
case success @ ElasticSuccess(version) =>
42+
case ElasticSuccess(version) =>
4143
logger.info(s"✅ Elasticsearch version: $version")
42-
cachedVersion = Some(version)
43-
success
44+
cachedVersion.compareAndSet(None, Some(version))
45+
ElasticSuccess(cachedVersion.get.getOrElse(version))
4446
case failure @ ElasticFailure(error) =>
4547
logger.error(s"❌ Failed to get Elasticsearch version: ${error.message}")
4648
failure

0 commit comments

Comments
 (0)