forked from corda/corda
-
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.
* Add named caches and apply to NonInvalidingUnboundCache and all usages. * Add named caches and apply to NonInvalidingCache and all usages. * Add named caches and apply to NonInvalidingWeightBasedCache and all usages. * Move NamedCache to core/internal * Remove type `NamedCache` and `NamedLoadingCache` * Suppressed 'name not used' warning, added comment, and fixed generic parameters on the buildNamed functions. * Use `buildNamed` in all caffeine instances in production code. Not using it for caches that are created in test code. * Add checks for the cache name * Formatting * Minor code review revisions
- Loading branch information
Showing
29 changed files
with
130 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
core/src/main/kotlin/net/corda/core/internal/NamedCache.kt
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 @@ | ||
package net.corda.core.internal | ||
|
||
import com.github.benmanes.caffeine.cache.Cache | ||
import com.github.benmanes.caffeine.cache.CacheLoader | ||
import com.github.benmanes.caffeine.cache.Caffeine | ||
import com.github.benmanes.caffeine.cache.LoadingCache | ||
|
||
/** | ||
* Restrict the allowed characters of a cache name - this ensures that each cache has a name, and that | ||
* the name can be used to create a file name or a metric name. | ||
*/ | ||
internal fun checkCacheName(name: String) { | ||
require(!name.isBlank()) | ||
require(allowedChars.matches(name)) | ||
} | ||
|
||
private val allowedChars = Regex("^[0-9A-Za-z_.]*\$") | ||
|
||
/* buildNamed is the central helper method to build caffeine caches in Corda. | ||
* This allows to easily add tweaks to all caches built in Corda, and also forces | ||
* cache users to give their cache a (meaningful) name that can be used e.g. for | ||
* capturing cache traces etc. | ||
* | ||
* Currently it is not used in this version of CORDA, but there are plans to do so. | ||
*/ | ||
|
||
fun <K, V> Caffeine<in K, in V>.buildNamed(name: String): Cache<K, V> { | ||
checkCacheName(name) | ||
return this.build<K, V>() | ||
} | ||
|
||
fun <K, V> Caffeine<in K, in V>.buildNamed(name: String, loadFunc: (K) -> V): LoadingCache<K, V> { | ||
checkCacheName(name) | ||
return this.build<K, V>(loadFunc) | ||
} | ||
|
||
|
||
fun <K, V> Caffeine<in K, in V>.buildNamed(name: String, loader: CacheLoader<K, V>): LoadingCache<K, V> { | ||
checkCacheName(name) | ||
return this.build<K, V>(loader) | ||
} |
24 changes: 24 additions & 0 deletions
24
core/src/test/kotlin/net/corda/core/internal/NamedCacheTest.kt
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,24 @@ | ||
package net.corda.core.internal | ||
|
||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
class NamedCacheTest { | ||
fun checkNameHelper(name: String, throws: Boolean) { | ||
var exceptionThrown = false | ||
try { | ||
checkCacheName(name) | ||
} catch (e: Exception) { | ||
exceptionThrown = true | ||
} | ||
assertEquals(throws, exceptionThrown) | ||
} | ||
|
||
@Test | ||
fun TestCheckCacheName() { | ||
checkNameHelper("abc_123.234", false) | ||
checkNameHelper("", true) | ||
checkNameHelper("abc 123", true) | ||
checkNameHelper("abc/323", true) | ||
} | ||
} |
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
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
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
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
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
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
Oops, something went wrong.