Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 何时使用新缓存名称?_Java_Spring_Caching_Ehcache - Fatal编程技术网

Java 何时使用新缓存名称?

Java 何时使用新缓存名称?,java,spring,caching,ehcache,Java,Spring,Caching,Ehcache,我应该什么时候在Ehcache中重用缓存,什么时候创建一个新的缓存 例1: 我有以下方法: public Dog getBestDog(String name) { //Find the best dog with the provided name } public Dog getBestBrownDog(String name) { //Find the best brown dog with the provided name } public Dog getBestD

我应该什么时候在Ehcache中重用缓存,什么时候创建一个新的缓存

例1:

我有以下方法:

public Dog getBestDog(String name) {
    //Find the best dog with the provided name
}

public Dog getBestBrownDog(String name) {
    //Find the best brown dog with the provided name
}
public Dog getBestDogByName(String name) {
    //Find the best dog with the provided name
}

public Dog getBestDogByColour(String colour) {
    //Find the best dog with the provided colour
}
对于给定的字符串(例如“rover”),这两种方法可能返回不同的Dog对象

我应该用
@Cacheable(cacheName=“dogs”)
注释它们,还是将它们放在两个不同的缓存中,“bestDogs”和“bestBrownDogs”

例2:

我有以下方法:

public Dog getBestDog(String name) {
    //Find the best dog with the provided name
}

public Dog getBestBrownDog(String name) {
    //Find the best brown dog with the provided name
}
public Dog getBestDogByName(String name) {
    //Find the best dog with the provided name
}

public Dog getBestDogByColour(String colour) {
    //Find the best dog with the provided colour
}
名称“漫游者”和颜色“狗的颜色”可以返回相同的狗


我应该用
@Cacheable(cacheName=“dogs”)
对它们进行注释,还是应该将它们放在两个不同的缓存中,“dogsByName”和“dogsbycolor”?

每当您遇到同一个键可能导致不同结果的场景时,您可能需要一个单独的缓存

例1:

getBestDog(名称)
-将名称用作“BestDogs”缓存中的键

getBestBrownDog(名称)
-使用该名称作为“best brown dogs”缓存中的键

例2:

getBestDogByName(名称)
-与示例1相同,使用“BestDogs”缓存中的名称作为键

GetBestDogByColor(颜色)
-使用“BestDogByColor”缓存中的颜色作为键

这将为您留下3个缓存,“最佳狗”,“最佳棕色狗”,“最佳颜色狗”


从理论上讲,你可以把“最好的狗”和“颜色最好的狗”合并起来。。。但也许你有一只叫“红”的狗。。因此,这将是一个无法解释的边缘情况。

使用不同的缓存可以工作。您也可以使用相同的缓存,只需通过使用SpEL将它们设置为不同的键,如下所示:

@Cacheable(cacheName = "dogs", key = "'name.'+#name")
public Dog getBestDogByName(String name) {
    //Find the best dog with the provided name
}

@Cacheable(cacheName = "dogs", key = "'colour.'+#colour")
public Dog getBestDogByColour(String colour) {
    //Find the best dog with the provided colour
}

如果两个方法返回的对象具有不同的键,那么这会不会像只存储对象的一个副本那样聪明呢?不会,除非有办法在ehcache中配置它。