-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
102 additions
and
12 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
21 changes: 21 additions & 0 deletions
21
oni/src/main/scala/org/mbari/oni/etc/jdk/CloseableLock.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,21 @@ | ||
/* | ||
* Copyright (c) Monterey Bay Aquarium Research Institute 2024 | ||
* | ||
* oni code is non-public software. Unauthorized copying of this file, | ||
* via any medium is strictly prohibited. Proprietary and confidential. | ||
*/ | ||
|
||
package org.mbari.oni.etc.jdk | ||
|
||
import java.util.concurrent.locks.ReentrantLock | ||
|
||
class CloseableLock extends ReentrantLock with AutoCloseable { | ||
|
||
def lockAndGet: CloseableLock = { | ||
lock(); | ||
this | ||
} | ||
|
||
override def close(): Unit = unlock() | ||
|
||
} |
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
64 changes: 64 additions & 0 deletions
64
oni/src/main/scala/org/mbari/oni/services/ConceptCache.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,64 @@ | ||
/* | ||
* Copyright (c) Monterey Bay Aquarium Research Institute 2024 | ||
* | ||
* oni code is non-public software. Unauthorized copying of this file, | ||
* via any medium is strictly prohibited. Proprietary and confidential. | ||
*/ | ||
|
||
package org.mbari.oni.services | ||
|
||
import com.github.benmanes.caffeine.cache.{Cache, Caffeine} | ||
import org.mbari.oni.domain.ConceptMetadata | ||
import org.mbari.oni.etc.jdk.Loggers.{*, given} | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
class ConceptCache(conceptService: ConceptService, conceptNameService: ConceptNameService) { | ||
|
||
private val log = System.getLogger(getClass.getName) | ||
|
||
private val nameCache: Cache[String, ConceptMetadata] = Caffeine | ||
.newBuilder() | ||
.expireAfterWrite(15, TimeUnit.MINUTES) | ||
.build[String, ConceptMetadata]() | ||
|
||
private val allNamesCache: Cache[String, Seq[String]] = Caffeine | ||
.newBuilder() | ||
.expireAfterWrite(15, TimeUnit.MINUTES) | ||
.build[String, Seq[String]]() | ||
|
||
def findByName(name: String): Either[Throwable, ConceptMetadata] = { | ||
Option(nameCache.getIfPresent(name)) match { | ||
case Some(node) => Right(node) | ||
case None => | ||
conceptService.findByName(name) match | ||
case Left(e) => | ||
log.atInfo.withCause(e).log(s"Failed to find concept by name: $name") | ||
Left(e) | ||
case Right(conceptNode) => | ||
nameCache.put(name, conceptNode) | ||
Right(conceptNode) | ||
} | ||
} | ||
|
||
def findAllNames(limit: Int, offset: Int): Either[Throwable, Seq[String]] = { | ||
val allNames = Option(allNamesCache.getIfPresent(ConceptCache.AllNamesCacheKey)) | ||
if (allNames.isDefined && allNames.get.nonEmpty) { | ||
Right(allNames.get.slice(offset, offset + limit)) | ||
} | ||
else { | ||
conceptNameService.findAllNames(1000000, 0) match | ||
case Left(e) => | ||
log.atError.withCause(e).log("Failed to find all concept names") | ||
Left(e) | ||
case Right(names) => | ||
allNamesCache.put(ConceptCache.AllNamesCacheKey, names) | ||
Right(names.slice(offset, offset + limit)) | ||
} | ||
} | ||
|
||
} | ||
|
||
object ConceptCache { | ||
val AllNamesCacheKey = "all-names" | ||
} |
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