2016-10-07 4 views
2

キャッシュを有効にしたスプリングブートアプリケーションを使用しています。EnableCachingを使ったスプリングブートアプリケーションguavaキャッシュの内容を取得する方法

環境(のpom.xml):

春:

org.springframework.boot:spring-boot-starter-amqp:jar:1.3.3.RELEASE 
org.springframework:spring-messaging:jar:4.2.5.RELEASE 
org.springframework.amqp:spring-rabbit:jar:1.5.4.RELEASE 
org.springframework.retry:spring-retry:jar:1.1.2.RELEASE 
org.springframework:spring-core:jar:4.2.5.RELEASE:compile 
org.springframework.cloud:spring-cloud-aws-context:jar:1.0.4.RELEASE 
org.springframework:spring-context:jar:4.2.5.RELEASE 
org.springframework.data:spring-data-jpa:jar:1.9.4.RELEASE 
org.springframework:spring-context-support:jar:4.2.5.RELEASE 

休止

org.hibernate:hibernate-validator:jar:5.2.2.Final 
org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final 
com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:jar:2.6.5 
org.hibernate:hibernate-entitymanager:jar:5.1.0.Final 
org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final 
org.hibernate:hibernate-java8:jar:5.1.0.Final 
org.hibernate:hibernate-envers:jar:5.1.0.Final 

構成キャッシュ(春ブートアプリケーション上):

Hibernateのエンティティ:

@Entity 
@Table(name = Configuration.TABLE_NAME) 
@DynamicUpdate 
public class Configuration implements Serializable { 

    public static final String TABLE_NAME = "configuration"; 

    private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name = "id") 
    @Convert(converter = ConfigurationConverter.class) 
    private ConfigurationEnum id; 

    @Column(name = "service", nullable = false) 
    @NotNull 
    @Convert(converter = ServiceConverter.class) 
    private ServiceEnum service; 

} 

リポジトリ(春データ):

public interface ConfigurationRepository extends PagingAndSortingRepository<Configuration, Integer>, 
     JpaSpecificationExecutor<Configuration> { 

    @Cacheable(ApplicationCacheConfig.CONFIGURATION_TABLE_FIND_BY_ID_CACHE_NAME) 
    Configuration findById(ConfigurationEnum configurationEnum); 

    @Cacheable(ApplicationCacheConfig.CONFIGURATION_TABLE_FIND_SERVICE_ID_CACHE_NAME) 
    List<Configuration> findByService(ServiceEnum service); 

} 

構成列挙:

@Getter 
@AllArgsConstructor 
public enum ConfigurationEnum { 

    CONFIG_1(1), 
    CONFIG_2(2); 

    private int id; 
} 

設定コンバータ:

@Converter 
public class ConfigurationConverter implements AttributeConverter<ConfigurationEnum, Integer> { 

    @Override 
    public Integer convertToDatabaseColumn(ConfigurationEnum key) { 
     return key == null ? null : (int) key.getId(); 
    } 

    @Override 
    public ConfigurationEnum convertToEntityAttribute(Integer key) { 

     return key == null ? null : Stream.of(ConfigurationEnum.values()) 
        .filter(step -> key.equals(step.getId())) 
        .findFirst() 
        .orElse(null); 
    } 
} 

テストIT:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = ApplicationIT.class) 
@WebAppConfiguration 
@Transactional 
public class ConfigurationCacheIT { 

    @Autowired 
    ConfigurationRepository configurationRepository; 

    @Autowired 
    protected CacheManager cacheManager; 

    @Test 
    public void configuration_findById_cache_success() { 

     Configuration config = configurationRepository.findById(ConfigurationEnum.CONFIG_1); 
     // An ORM request is performed - CHECK 
     Assert.assertNotNull(step); // TEST OK 
     Cache.ValueWrapper entry = getCacheEntry(ApplicationCacheConfig.CONFIGURATION_TABLE_FIND_BY_ID_CACHE_NAME, ConfigurationEnum.CONFIG_1.getId()); 
     Assert.assertNull(entry); OK 

     config = configurationRepository.findById(ConfigurationEnum.CONFIG_1); 
     // No ORM request is performed - CHECK 
     Assert.assertNotNull(step); // TEST OK 

     entry = getCacheEntry(ApplicationCacheConfig.CONFIGURATION_TABLE_FIND_BY_ID_CACHE_NAME, ConfigurationEnum.CONFIG_1.getId()); 
     Assert.assertNotNull(entry); **// TEST FAIL !!!** 

     entry = getCacheEntry(ApplicationCacheConfig.CONFIGURATION_TABLE_FIND_BY_ID_CACHE_NAME, ConfigurationEnum.CONFIG_1.name()); 
     Assert.assertNotNull(entry); **// TEST FAIL !!!** 

     entry = getCacheEntry(ApplicationCacheConfig.CONFIGURATION_TABLE_FIND_BY_ID_CACHE_NAME, ConfigurationEnum.CONFIG_1); 
     Assert.assertNotNull(entry); **// TEST FAIL !!!** 
    } 

    protected Cache.ValueWrapper getCacheEntry(String cacheName, Object key) { 
     return cacheManager.getCache(cacheName).get(key); 
    } 

    @Test 
    public void configuration_findByAll_without_cache_success() { 

    ArrayList<Configuration> list1 = Lists.newArrayList(configurationRepository.findAll()); 
    // An ORM request is executed 
    Assert.assertNotNull(list1); 
    Assert.assertEquals(ConfigurationEnum.values().length, list1.size()); 

    ArrayList<Configuration> list2 = Lists.newArrayList(configurationRepository.findAll()); 
    // Another ORM request is executed 
    Assert.assertNotNull(list2); 
    Assert.assertEquals(ConfigurationEnum.values().length, list2.size()); 
    } 

} 

私の質問は私のテストが失敗する理由ですか?

+0

キーはので、これらのテストは、単にいつも失敗します列挙型ではない名前またはIDです。明らかにあなたのテストでは、キャッシングは有効になっていません。 –

+0

2番目のリクエストに表示されているように、「いいえORMリクエストが実行されました - CHECK'' – Leonel

+0

キャッシュとは何の関係もありません。それは第1レベルのキャッシュ、つまり 'EntityManager'から直接得られます。 –

答えて

0

実際これは問題ではありませんでした。

  • のApp-MDW(ミドルウェア層)(@EnableCaching注釈付き春ブーツAPP)
  • のApp-WS(Webサービス層)(春:

    私は休閑アーキテクチャを使用しています@EnableCaching注釈なしのブートAPP)

上記のテストは、アプリケーションのApp-WSと上で実行されましたアノテーションは継承されないため、キャッシングが機能しませんでした。

右アサートされました:

entry = getCacheEntry(ApplicationCacheConfig.CONFIGURATION_TABLE_FIND_BY_ID_CACHE_NAME, ConfigurationEnum.CONFIG_1); 
     Assert.assertNotNull(entry) 
関連する問題