Skip to content

Document that SimpleCacheManager requires caches to be initialized when not used as a bean #22988

Closed
@akokskis

Description

@akokskis

When creating a org.springframework.cache.support.SimpleCacheManager and explicitly adding caches to it, the configured object does not maintain the contract of the org.springframework.cache.CacheManager interface.

Specifically, getCacheNames() and getCache(String name) do not behave as one would expect, returning an empty collection and null respectively.

This issue is reproducible with version 5.1.7.RELEASE.

This issue is demonstrated with the following test:

package com.ak;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.Collection;
import java.util.Collections;

import org.junit.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;

public class TestSimpleCacheManager {

    private static final String CACHE_NAME = "this is a cache";
    
    @Test
    public void testGetCacheNames() {
        SimpleCacheManager simpleCacheManager = setUpSimpleCacheManager();
        
        Collection<String> cacheNames = simpleCacheManager.getCacheNames();
        assertEquals(1, cacheNames.size());
        assertTrue(cacheNames.contains(CACHE_NAME));
    }
    
    @Test
    public void testGetCache() {
        SimpleCacheManager simpleCacheManager = setUpSimpleCacheManager();
        
        Cache cache = simpleCacheManager.getCache(CACHE_NAME);
        
        assertNotNull(cache);
    }

    private SimpleCacheManager setUpSimpleCacheManager() {
        Cache cache = new ConcurrentMapCache(CACHE_NAME);
        
        SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
        simpleCacheManager.setCaches(Collections.singleton(cache));
        return simpleCacheManager;
    }
}

using the following pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ak</groupId>
  <artifactId>ak-testing</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>ak-testing</name>
  <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.7.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
      </dependency>
  </dependencies>
</project>

I'd be happy to take a crack at creating a patch for this issue, should this be something that is desired to be fixed.

Thanks

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions