在Kotlin中创建ehcache缓存

时间:2017-03-10 18:58:24

标签: 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

2 个答案:

答案 0 :(得分:1)

CacheConfigurationBuilder还有一个问题:它没有公共构造函数,您需要使用:

CacheConfigurationBuilder.newCacheConfigurationBuilder(keyClass, valueClass, ResourcePools);

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

答案 1 :(得分: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>) {
    }
}