简体   繁体   English

Spring缓存,不同缓存上的相同密钥

[英]Spring cache, same key on different caches

I'm trying out to have two caches to store two different items, User and Group, with the same id. 我正在尝试使用两个缓存来存储两个不同的项目,User和Group,具有相同的ID。 I expect I should be getting different values since they are two different caches. 我希望我应该得到不同的值,因为它们是两个不同的缓存。 But it is not. 但事实并非如此。

@Bean
public JedisConnectionFactory jedisConnectionFactory() {
    LOG.debug("redis host: {}, redis port: {}", redisHost, redisPort);
    JedisConnectionFactory factory = new JedisConnectionFactory();
    factory.setUseSsl(true);
    factory.setHostName(redisHost);
    factory.setPort(redisPort);
    factory.setUsePool(true);
    factory.setPassword(primaryKey);
    return factory;
}

/**
 *
 * @return
 */
@Bean(name = "redisTemplate")
public RedisTemplate redisTemplate() {
    RedisTemplate template = new RedisTemplate();
    template.setConnectionFactory(jedisConnectionFactory());
    return template;
}

/**
 *
 * @return
 */
@Bean
public CacheManager cacheManager() {
    RedisCacheManager manager = new RedisCacheManager(redisTemplate());
    manager.setCacheNames(Arrays.asList("users", "groups"));
    return manager;
}

@Cacheable(cacheNames = "users", key = "#id")
public User findUserById(String id) {
    return null;
}

@Cacheable(cacheNames = "users", key = "#user.id")
public User updateUser(User user) {
    return user;
}

@Cacheable(cacheNames = "groups", key = "#id")
public Group findGroupById(String id) {
    return null;
}

@CachePut(cacheNames = "groups", key = "#group.id")
public Group updateGroup(Group group) {
    return group;
}

JUnit. JUnit的。

    service.removeCaches();

    User user = new User();
    user.setName("oldUser");
    user.setId("1");


    // USER
    assertNull(service.findUserById("1"));

    service.updateUser(user);

    User userCached = service.findUserById("1");
    LOG.debug(userCached);
    assertNotNull(userCached);
    assertEquals(userCached.getName(), "oldUser");

    // GROUP
    assertNull(service.findGroupById("1")); // <== here, im actually getting user instead of null. cache has been cleared before the test starts. 

Maybe I misunderstand the concept of different caches here. 也许我在这里误解了不同缓存的概念。 Or maybe key should be unique even within different caches? 或者即使在不同的缓存中,密钥也应该是唯一的?

Logs 日志

 Results :

Tests in error: 
  testCache(TestRedisServiceImpl): User cannot be cast to Group

This basically indicates that a user is returned from the cache. 这基本上表示从缓存返回用户。

Actually, CacheManager.setCacheNames() is to provide alias names for the same cache , it is not two different caches. 实际上, CacheManager.setCacheNames()为同一个缓存提供别名 ,它不是两个不同的缓存。 You can look the API here : 您可以在这里查看API:

void setCacheNames(Collection cacheNames) Specify the set of cache names for this CacheManager's 'static' mode. void setCacheNames(Collection cacheNames)指定此CacheManager的“静态”模式的缓存名称集。

You can think a CacheManager is simply like a Map object where one key will store one object (as value) only, so when you try to insert the second object with the same key, first object will be replaced with the second object. 您可以认为CacheManager就像一个Map对象,其中一个键只存储一个对象(作为值),因此当您尝试使用相同的键插入第二个对象时,第一个对象将替换为第二个对象。

So, what is happening is that the user object in the CacheManager is getting replaced with the group object because both of them are stored with the same key. 因此,正在发生的事情是CacheManager中的user对象被替换为group对象,因为它们都使用相同的密钥存储。

To make the cache keys unique in the cachemanager, you can use cacheManager.setUsePrefix() so that each key will be prefixed with the respective cachename and the values will not get replaced. 要在缓存管理器中使缓存键唯一,可以使用cacheManager.setUsePrefix()以便每个键都以相应的cachename为前缀,并且不会替换值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM