在Kotlin中创建一个ehcache缓存

我无法在Kotlin中创建一个简单的ehcache缓存。 我试图保持缓存尽可能通用,所以关键值对是泛型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>) { } } 

不幸的是,我仍然有私人构造函数的问题:

 :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有一个问题:它没有公共构造函数,而是需要使用:

 CacheConfigurationBuilder.newCacheConfigurationBuilder(keyClass, valueClass, ResourcePools); 

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

只是为了一个完整的工作例子,结束了工作:

 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 .newCacheConfigurationBuilder(p.javaClass, m.javaClass, ResourcePoolsBuilder.heap(10))).build() solvedPositions = cm.getCache("solved", p.javaClass, m.javaClass) } fun main(args: Array<String>) { } } 
Interesting Posts