Search in sources :

Example 1 with CacheOperationInvocationContext

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

the class JCacheAspectSupport method execute.

@SuppressWarnings("unchecked")
@Nullable
private Object execute(CacheOperationInvocationContext<?> context, CacheOperationInvoker invoker) {
    CacheOperationInvoker adapter = new CacheOperationInvokerAdapter(invoker);
    BasicOperation operation = context.getOperation();
    if (operation instanceof CacheResultOperation) {
        Assert.state(this.cacheResultInterceptor != null, "No CacheResultInterceptor");
        return this.cacheResultInterceptor.invoke((CacheOperationInvocationContext<CacheResultOperation>) context, adapter);
    } else if (operation instanceof CachePutOperation) {
        Assert.state(this.cachePutInterceptor != null, "No CachePutInterceptor");
        return this.cachePutInterceptor.invoke((CacheOperationInvocationContext<CachePutOperation>) context, adapter);
    } else if (operation instanceof CacheRemoveOperation) {
        Assert.state(this.cacheRemoveEntryInterceptor != null, "No CacheRemoveEntryInterceptor");
        return this.cacheRemoveEntryInterceptor.invoke((CacheOperationInvocationContext<CacheRemoveOperation>) context, adapter);
    } else if (operation instanceof CacheRemoveAllOperation) {
        Assert.state(this.cacheRemoveAllInterceptor != null, "No CacheRemoveAllInterceptor");
        return this.cacheRemoveAllInterceptor.invoke((CacheOperationInvocationContext<CacheRemoveAllOperation>) context, adapter);
    } else {
        throw new IllegalArgumentException("Cannot handle " + operation);
    }
}
Also used : CacheOperationInvocationContext(org.springframework.cache.interceptor.CacheOperationInvocationContext) BasicOperation(org.springframework.cache.interceptor.BasicOperation) CacheOperationInvoker(org.springframework.cache.interceptor.CacheOperationInvoker) Nullable(org.springframework.lang.Nullable)

Example 2 with CacheOperationInvocationContext

use of org.springframework.cache.interceptor.CacheOperationInvocationContext in project mica2 by obiba.

the class DatasetCacheResolver method resolveCaches.

@Override
public synchronized Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> cacheOperationInvocationContext) {
    Collection<Cache> res = Lists.newArrayList();
    Optional<Object> dataset = Arrays.stream(cacheOperationInvocationContext.getArgs()).filter(o -> o instanceof Dataset).findFirst();
    if (dataset.isPresent()) {
        String cacheName = "dataset-" + ((Dataset) dataset.get()).getId();
        Cache datasetCache = springCacheManager.getCache(cacheName);
        if (datasetCache == null) {
            CacheConfiguration conf = cacheManager.getEhcache("dataset-variables").getCacheConfiguration().clone();
            conf.setName(cacheName);
            cacheManager.addCache(new net.sf.ehcache.Cache(conf));
            net.sf.ehcache.Cache cache = cacheManager.getCache(cacheName);
            cacheManager.replaceCacheWithDecoratedCache(cache, InstrumentedEhcache.instrument(metricRegistry, cache));
            datasetCache = new EhCacheCache(cacheManager.getEhcache(cacheName));
        }
        res.add(datasetCache);
    }
    return res;
}
Also used : Arrays(java.util.Arrays) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) MetricRegistry(com.codahale.metrics.MetricRegistry) InstrumentedEhcache(com.codahale.metrics.ehcache.InstrumentedEhcache) Cache(org.springframework.cache.Cache) CacheOperationInvocationContext(org.springframework.cache.interceptor.CacheOperationInvocationContext) Collection(java.util.Collection) Dataset(org.obiba.mica.dataset.domain.Dataset) CacheResolver(org.springframework.cache.interceptor.CacheResolver) Inject(javax.inject.Inject) Component(org.springframework.stereotype.Component) Lists(com.google.common.collect.Lists) CacheManager(org.springframework.cache.CacheManager) EhCacheCache(org.springframework.cache.ehcache.EhCacheCache) Optional(java.util.Optional) Dataset(org.obiba.mica.dataset.domain.Dataset) EhCacheCache(org.springframework.cache.ehcache.EhCacheCache) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Cache(org.springframework.cache.Cache) EhCacheCache(org.springframework.cache.ehcache.EhCacheCache)

Aggregations

CacheOperationInvocationContext (org.springframework.cache.interceptor.CacheOperationInvocationContext)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 InstrumentedEhcache (com.codahale.metrics.ehcache.InstrumentedEhcache)1 Lists (com.google.common.collect.Lists)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Optional (java.util.Optional)1 Inject (javax.inject.Inject)1 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)1 Dataset (org.obiba.mica.dataset.domain.Dataset)1 Cache (org.springframework.cache.Cache)1 CacheManager (org.springframework.cache.CacheManager)1 EhCacheCache (org.springframework.cache.ehcache.EhCacheCache)1 BasicOperation (org.springframework.cache.interceptor.BasicOperation)1 CacheOperationInvoker (org.springframework.cache.interceptor.CacheOperationInvoker)1 CacheResolver (org.springframework.cache.interceptor.CacheResolver)1 Nullable (org.springframework.lang.Nullable)1 Component (org.springframework.stereotype.Component)1