Search in sources :

Example 21 with CacheOperation

use of org.springframework.cache.interceptor.CacheOperation in project spring-framework by spring-projects.

the class AnnotationCacheOperationSourceTests method customCacheManagerInherited.

@Test
public void customCacheManagerInherited() {
    Collection<CacheOperation> ops = getOps(AnnotatedClass.class, "customCacheManagerInherited", 1);
    CacheOperation cacheOperation = ops.iterator().next();
    assertEquals("Custom cache manager not set", "custom", cacheOperation.getCacheManager());
}
Also used : CacheOperation(org.springframework.cache.interceptor.CacheOperation) Test(org.junit.Test)

Example 22 with CacheOperation

use of org.springframework.cache.interceptor.CacheOperation in project spring-framework by spring-projects.

the class CacheAdviceParser method parseDefinitionSource.

private RootBeanDefinition parseDefinitionSource(Element definition, ParserContext parserContext) {
    Props prop = new Props(definition);
    // add cacheable first
    ManagedMap<TypedStringValue, Collection<CacheOperation>> cacheOpMap = new ManagedMap<>();
    cacheOpMap.setSource(parserContext.extractSource(definition));
    List<Element> cacheableCacheMethods = DomUtils.getChildElementsByTagName(definition, CACHEABLE_ELEMENT);
    for (Element opElement : cacheableCacheMethods) {
        String name = prop.merge(opElement, parserContext.getReaderContext());
        TypedStringValue nameHolder = new TypedStringValue(name);
        nameHolder.setSource(parserContext.extractSource(opElement));
        CacheableOperation.Builder builder = prop.merge(opElement, parserContext.getReaderContext(), new CacheableOperation.Builder());
        builder.setUnless(getAttributeValue(opElement, "unless", ""));
        builder.setSync(Boolean.valueOf(getAttributeValue(opElement, "sync", "false")));
        Collection<CacheOperation> col = cacheOpMap.get(nameHolder);
        if (col == null) {
            col = new ArrayList<>(2);
            cacheOpMap.put(nameHolder, col);
        }
        col.add(builder.build());
    }
    List<Element> evictCacheMethods = DomUtils.getChildElementsByTagName(definition, CACHE_EVICT_ELEMENT);
    for (Element opElement : evictCacheMethods) {
        String name = prop.merge(opElement, parserContext.getReaderContext());
        TypedStringValue nameHolder = new TypedStringValue(name);
        nameHolder.setSource(parserContext.extractSource(opElement));
        CacheEvictOperation.Builder builder = prop.merge(opElement, parserContext.getReaderContext(), new CacheEvictOperation.Builder());
        String wide = opElement.getAttribute("all-entries");
        if (StringUtils.hasText(wide)) {
            builder.setCacheWide(Boolean.valueOf(wide.trim()));
        }
        String after = opElement.getAttribute("before-invocation");
        if (StringUtils.hasText(after)) {
            builder.setBeforeInvocation(Boolean.valueOf(after.trim()));
        }
        Collection<CacheOperation> col = cacheOpMap.get(nameHolder);
        if (col == null) {
            col = new ArrayList<>(2);
            cacheOpMap.put(nameHolder, col);
        }
        col.add(builder.build());
    }
    List<Element> putCacheMethods = DomUtils.getChildElementsByTagName(definition, CACHE_PUT_ELEMENT);
    for (Element opElement : putCacheMethods) {
        String name = prop.merge(opElement, parserContext.getReaderContext());
        TypedStringValue nameHolder = new TypedStringValue(name);
        nameHolder.setSource(parserContext.extractSource(opElement));
        CachePutOperation.Builder builder = prop.merge(opElement, parserContext.getReaderContext(), new CachePutOperation.Builder());
        builder.setUnless(getAttributeValue(opElement, "unless", ""));
        Collection<CacheOperation> col = cacheOpMap.get(nameHolder);
        if (col == null) {
            col = new ArrayList<>(2);
            cacheOpMap.put(nameHolder, col);
        }
        col.add(builder.build());
    }
    RootBeanDefinition attributeSourceDefinition = new RootBeanDefinition(NameMatchCacheOperationSource.class);
    attributeSourceDefinition.setSource(parserContext.extractSource(definition));
    attributeSourceDefinition.getPropertyValues().add("nameMap", cacheOpMap);
    return attributeSourceDefinition;
}
Also used : Element(org.w3c.dom.Element) CachePutOperation(org.springframework.cache.interceptor.CachePutOperation) CacheableOperation(org.springframework.cache.interceptor.CacheableOperation) CacheEvictOperation(org.springframework.cache.interceptor.CacheEvictOperation) CacheOperation(org.springframework.cache.interceptor.CacheOperation) Collection(java.util.Collection) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) TypedStringValue(org.springframework.beans.factory.config.TypedStringValue) ManagedMap(org.springframework.beans.factory.support.ManagedMap)

Aggregations

CacheOperation (org.springframework.cache.interceptor.CacheOperation)22 Test (org.junit.Test)21 CacheableOperation (org.springframework.cache.interceptor.CacheableOperation)5 CacheEvictOperation (org.springframework.cache.interceptor.CacheEvictOperation)3 Collection (java.util.Collection)1 TypedStringValue (org.springframework.beans.factory.config.TypedStringValue)1 ManagedMap (org.springframework.beans.factory.support.ManagedMap)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 CachePutOperation (org.springframework.cache.interceptor.CachePutOperation)1 Element (org.w3c.dom.Element)1