gpt4 book ai didi

kotlin - 在 Kotlin 中创建 ehcache 缓存

转载 作者:行者123 更新时间:2023-12-02 13:39:26 24 4
gpt4 key购买 nike

我无法创建一个简单的 ehcache在 Kotlin 中缓存。我试图使缓存尽可能通用,因此键值对是泛型 PosMove .

import org.ehcache.CacheManager
import org.ehcache.Cache
import org.ehcache.config.builders.CacheManagerBuilder
import org.ehcache.config.builders.CacheConfigurationBuilder
import org.ehcache.config.builders.ResourcePoolsBuilder

class Main<reified Pos, reified Move> {

val cm : CacheManager?
val solvedPositions : Cache<Pos, Move>?

init {
cm = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("solved", CacheConfigurationBuilder<Pos, Move>()
.withResourcePools(ResourcePoolsBuilder.heap(10)))
.buildConfig(Class<Pos>::class.java, Class<Move>::class.java)
.build(true);
solvedPositions = cm.getCache("solved", Class<Pos>::class.java, Class<Move>::class.java);
}


fun main(args: Array<String>) {
}
}

运行此程序会给出以下错误消息:
e: /Users/christophersumnicht/CacheIssue/src/main/kotlin/Main.kt: (7, 12): Only type parameters of inline functions can be reified
e: /Users/christophersumnicht/CacheIssue/src/main/kotlin/Main.kt: (7, 25): Only type parameters of inline functions can be reified
e: /Users/christophersumnicht/CacheIssue/src/main/kotlin/Main.kt: (14, 53): None of the following functions can be called with the arguments supplied:
private constructor CacheConfigurationBuilder<K : Any!, V : Any!>(keyType: Class<Pos!>!, valueType: Class<Move!>!, resourcePools: ResourcePools!) defined in org.ehcache.config.builders.CacheConfigurationBuilder
private constructor CacheConfigurationBuilder<K : Any!, V : Any!>(other: CacheConfigurationBuilder<Pos!, Move!>!) defined in org.ehcache.config.builders.CacheConfigurationBuilder
e: /Users/christophersumnicht/CacheIssue/src/main/kotlin/Main.kt: (16, 45): Only classes are allowed on the left hand side of a class literal
e: /Users/christophersumnicht/CacheIssue/src/main/kotlin/Main.kt: (16, 69): Only classes are allowed on the left hand side of a class literal
e: /Users/christophersumnicht/CacheIssue/src/main/kotlin/Main.kt: (18, 53): Only classes are allowed on the left hand side of a class literal
e: /Users/christophersumnicht/CacheIssue/src/main/kotlin/Main.kt: (18, 77): Only classes are allowed on the left hand side of a class literal
:compileKotlin FAILED

请注意,我还删除了 reified我得到了类似的错误。如何解决这些错误?

更新:

我试图通过 Class实例作为构造函数如下:
import org.ehcache.CacheManager
import org.ehcache.Cache
import org.ehcache.config.builders.CacheManagerBuilder
import org.ehcache.config.builders.CacheConfigurationBuilder
import org.ehcache.config.builders.ResourcePoolsBuilder

class Main<Pos, Move>(p: Class<Pos>, m: Class<Move>) {

val cm : CacheManager?
val solvedPositions : Cache<Class<Pos>, Class<Move>>?

init {
cm = CacheManagerBuilder.newCacheManagerBuilder()
.withCache("solved", CacheConfigurationBuilder<Pos, Move>()
.withResourcePools(ResourcePoolsBuilder.heap(10)))
.buildConfig(p.javaClass, m.javaClass)
.build(true);
solvedPositions = cm.getCache("solved", p.javaClass, m.javaClass);
}


fun main(args: Array<String>) {
}
}

不幸的是,我仍然对私有(private)构造函数有疑问:
:compileKotlin
e: /Users/christophersumnicht/CacheIssue/src/main/kotlin/Main.kt: (14, 54): None of the following functions can be called with the arguments supplied:
private constructor CacheConfigurationBuilder<K : Any!, V : Any!>(keyType: Class<Pos!>!, valueType: Class<Move!>!, resourcePools: ResourcePools!) defined in org.ehcache.config.builders.CacheConfigurationBuilder
private constructor CacheConfigurationBuilder<K : Any!, V : Any!>(other: CacheConfigurationBuilder<Pos!, Move!>!) defined in org.ehcache.config.builders.CacheConfigurationBuilder
:compileKotlin FAILED

最佳答案

您对 CacheConfigurationBuilder 有疑问:它没有公共(public)构造函数,您需要改为使用:

CacheConfigurationBuilder.newCacheConfigurationBuilder(keyClass, valueClass, ResourcePools);

我相信您尝试使用的表单存在于 3.0.0 的早期版本中,但在发布之前已被删除,因为它允许尝试构建缺少必填字段的缓存配置。

关于kotlin - 在 Kotlin 中创建 ehcache 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42725818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com