Creating a ehcache cache in Kotlin
我在Kotlin中创建简单的ehcache缓存时遇到问题。我正在尝试使缓存尽可能通用,以便键值对为通用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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>) { } } |
运行此程序将给出以下错误消息:
1 2 3 4 5 6 7 8 9 10 | 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 |
请注意,我也删除了
更新:
我试图通过
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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>) { } } |
不幸的是,私有构造函数仍然存在问题:
1 2 3 4 5 | :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 |
1 | CacheConfigurationBuilder.newCacheConfigurationBuilder(keyClass, valueClass, ResourcePools); |
我相信您要使用的表单已存在于3.0.0的早期版本中,但在发布前已被删除,因为它允许尝试使用缺少必填字段的缓存配置。
仅就整个工作示例而言,这是最终工作的结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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>) { } } |