何时使用新的缓存名称?

时间:2022-09-24 20:35:39

When should I reuse a cache in Ehcache and when should I create a new one?

我应该何时在Ehcache中重用缓存,何时应该创建一个新缓存?

Example 1:

例1:

I have the following methods:

我有以下方法:

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
}

For a given String (e.g. "rover"), these two methods could return a different Dog object.

对于给定的String(例如“漫游者”),这两种方法可以返回不同的Dog对象。

Should I annotate them both with @Cacheable(cacheName = "dogs") or should I put them in two different caches, "bestDogs" and "bestBrownDogs"?

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

Example 2:

例2:

I have the following methods:

我有以下方法:

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
}

Name "rover" and colour "doggy-colour" could return the same Dog.

名称“流浪者”和颜色“狗狗颜色”可以返回相同的狗。

Should I annotate them both with @Cacheable(cacheName = "dogs") or should I put them in two different caches, 'dogsByName' and 'dogsByColour'?

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

2 个解决方案

#1


3  

Whenever you have a scenario where the same key can result in different results, then you probably want a separate cache.

每当您遇到相同密钥可能导致不同结果的情况时,您可能需要单独的缓存。

Example 1:

例1:

getBestDog(name) - use the name as the key from 'best-dogs' cache

getBestDog(name) - 使用名称作为'best-dogs'缓存中的键

getBestBrownDog(name) - use the name as the key from 'best-brown-dogs' cache

getBestBrownDog(name) - 使用名称作为'best-brown-dogs'缓存中的键

Example 2:

例2:

getBestDogByName(name) - same as example 1, use name as key from 'best-dogs' cache

getBestDogByName(name) - 与示例1相同,使用name作为“best-dogs”缓存中的键

getBestDogByColour(colour) - use colour as key from 'best-dogs-by-colour' cache

getBestDogByColour(color) - 使用颜色作为“best-dogs-by-color”缓存中的键

Which leaves you with 3 caches, 'best-dogs', 'best-brown-dogs', 'best-dogs-by-colour'

这给你留下3个缓存,'最好的狗','最好的棕色狗','最好的狗 - 颜色'

In theory, you could merge 'best-dogs' and 'best-dogs-by-colour'... but maybe you have a dog who is called 'red'.. so that would be an unaccounted for edge case.

从理论上讲,你可以合并'最好的狗'和'最好的狗 - 逐个颜色'...但也许你有一个被称为'红色'的狗...所以这将是一个不明的边缘情况。

#2


3  

Using differing caches can work. You can also use the same cache, just set them with different keys by using SpEL with something like the following:

使用不同的缓存可以工作。您也可以使用相同的缓存,只需使用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
}

#1


3  

Whenever you have a scenario where the same key can result in different results, then you probably want a separate cache.

每当您遇到相同密钥可能导致不同结果的情况时,您可能需要单独的缓存。

Example 1:

例1:

getBestDog(name) - use the name as the key from 'best-dogs' cache

getBestDog(name) - 使用名称作为'best-dogs'缓存中的键

getBestBrownDog(name) - use the name as the key from 'best-brown-dogs' cache

getBestBrownDog(name) - 使用名称作为'best-brown-dogs'缓存中的键

Example 2:

例2:

getBestDogByName(name) - same as example 1, use name as key from 'best-dogs' cache

getBestDogByName(name) - 与示例1相同,使用name作为“best-dogs”缓存中的键

getBestDogByColour(colour) - use colour as key from 'best-dogs-by-colour' cache

getBestDogByColour(color) - 使用颜色作为“best-dogs-by-color”缓存中的键

Which leaves you with 3 caches, 'best-dogs', 'best-brown-dogs', 'best-dogs-by-colour'

这给你留下3个缓存,'最好的狗','最好的棕色狗','最好的狗 - 颜色'

In theory, you could merge 'best-dogs' and 'best-dogs-by-colour'... but maybe you have a dog who is called 'red'.. so that would be an unaccounted for edge case.

从理论上讲,你可以合并'最好的狗'和'最好的狗 - 逐个颜色'...但也许你有一个被称为'红色'的狗...所以这将是一个不明的边缘情况。

#2


3  

Using differing caches can work. You can also use the same cache, just set them with different keys by using SpEL with something like the following:

使用不同的缓存可以工作。您也可以使用相同的缓存,只需使用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
}