Search in sources :

Example 1 with QueryCache

use of org.apache.cayenne.cache.QueryCache in project cayenne by apache.

the class CacheInvalidationFilter method onSync.

public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType, DataChannelFilterChain filterChain) {
    try {
        GraphDiff result = filterChain.onSync(originatingContext, changes, syncType);
        // no exceptions, flush...
        Collection<CacheGroupDescriptor> groupSet = groups.get();
        if (groupSet != null && !groupSet.isEmpty()) {
            QueryCache cache = cacheProvider.get();
            for (CacheGroupDescriptor group : groupSet) {
                if (group.getKeyType() != Void.class) {
                    cache.removeGroup(group.getCacheGroupName(), group.getKeyType(), group.getValueType());
                } else {
                    cache.removeGroup(group.getCacheGroupName());
                }
            }
        }
        return result;
    } finally {
        groups.set(null);
    }
}
Also used : QueryCache(org.apache.cayenne.cache.QueryCache) GraphDiff(org.apache.cayenne.graph.GraphDiff)

Example 2 with QueryCache

use of org.apache.cayenne.cache.QueryCache in project cayenne by apache.

the class DataDomainQueryActionIT method testCachedQuery.

@Test
public void testCachedQuery() {
    DataDomain domain = runtime.getDataDomain();
    Painting p = context.newObject(Painting.class);
    p.setPaintingTitle("sample");
    SelectQuery query = new SelectQuery(Painting.class);
    query.addPrefetch(Painting.TO_GALLERY.disjoint());
    query.addPrefetch(Painting.TO_ARTIST.disjoint());
    query.addOrdering(Painting.PAINTING_TITLE.asc());
    query.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
    query.setPageSize(5);
    QueryCache cache = domain.queryCache;
    domain.queryCache = new MockQueryCache() {

        @Override
        public List<?> get(QueryMetadata metadata, QueryCacheEntryFactory factory) {
            Object results = factory.createObject();
            assertTrue("Query cache is not serializable.", results instanceof Serializable);
            return null;
        }

        @SuppressWarnings("all")
        @Override
        public void put(QueryMetadata metadata, List results) {
            assertTrue("Query cache is not serializable.", results instanceof Serializable);
        }
    };
    DataDomainQueryAction action = new DataDomainQueryAction(context, domain, query);
    action.execute();
    domain.queryCache = cache;
}
Also used : MockQueryCache(org.apache.cayenne.cache.MockQueryCache) QueryCache(org.apache.cayenne.cache.QueryCache) Serializable(java.io.Serializable) MockQueryCache(org.apache.cayenne.cache.MockQueryCache) QueryMetadata(org.apache.cayenne.query.QueryMetadata) QueryCacheEntryFactory(org.apache.cayenne.cache.QueryCacheEntryFactory) Painting(org.apache.cayenne.testdo.testmap.Painting) SelectQuery(org.apache.cayenne.query.SelectQuery) List(java.util.List) Test(org.junit.Test)

Example 3 with QueryCache

use of org.apache.cayenne.cache.QueryCache in project cayenne by apache.

the class CacheInvalidationCacheGroupsHandlerIT method buildCustomModule.

@Override
protected Module buildCustomModule() {
    // Proxy query cache that will count methods calls
    final QueryCache cache = new MapQueryCache() {

        @Override
        public void removeGroup(String groupKey) {
            removeGroupUntypedCounter.incrementAndGet();
            super.removeGroup(groupKey);
        }

        @Override
        public void removeGroup(String groupKey, Class<?> keyType, Class<?> valueType) {
            removeGroupTypedCounter.incrementAndGet();
            super.removeGroup(groupKey, keyType, valueType);
        }
    };
    return binder -> binder.bind(QueryCache.class).toInstance(cache);
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) E1(org.apache.cayenne.cache.invalidation.db.E1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) E2(org.apache.cayenne.cache.invalidation.db.E2) QueryCache(org.apache.cayenne.cache.QueryCache) Module(org.apache.cayenne.di.Module) ObjectSelect(org.apache.cayenne.query.ObjectSelect) Test(org.junit.Test) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) QueryCache(org.apache.cayenne.cache.QueryCache) MapQueryCache(org.apache.cayenne.cache.MapQueryCache)

Example 4 with QueryCache

use of org.apache.cayenne.cache.QueryCache in project cayenne by apache.

the class ObjectContextQueryAction method interceptLocalCache.

/**
 * @since 3.0
 */
protected boolean interceptLocalCache() {
    if (metadata.getCacheKey() == null) {
        return !DONE;
    }
    // ignore local cache unless this context originated the query...
    if (!queryOriginator) {
        return !DONE;
    }
    boolean cache = QueryCacheStrategy.LOCAL_CACHE == metadata.getCacheStrategy();
    boolean cacheOrCacheRefresh = cache || QueryCacheStrategy.LOCAL_CACHE_REFRESH == metadata.getCacheStrategy();
    if (!cacheOrCacheRefresh) {
        return !DONE;
    }
    QueryCache queryCache = getQueryCache();
    QueryCacheEntryFactory factory = getCacheObjectFactory();
    if (cache) {
        List cachedResults = queryCache.get(metadata, factory);
        // there was a preexisting cache entry
        if (response == null) {
            response = new ListResponse(cachedResults);
        }
    } else {
        // on cache-refresh request, fetch without blocking and fill the cache
        queryCache.put(metadata, factory.createObject());
    }
    return DONE;
}
Also used : QueryCache(org.apache.cayenne.cache.QueryCache) ArrayList(java.util.ArrayList) List(java.util.List) QueryCacheEntryFactory(org.apache.cayenne.cache.QueryCacheEntryFactory)

Example 5 with QueryCache

use of org.apache.cayenne.cache.QueryCache in project cayenne by apache.

the class BaseContextTest method testAttachToRuntimeIfNeeded.

@Test
public void testAttachToRuntimeIfNeeded() {
    final DataChannel channel = mock(DataChannel.class);
    final QueryCache cache = mock(QueryCache.class);
    Module testModule = binder -> {
        binder.bind(DataChannel.class).toInstance(channel);
        binder.bind(QueryCache.class).toInstance(cache);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    BaseContext context = new MockBaseContext();
    assertNull(context.channel);
    assertNull(context.queryCache);
    Injector oldInjector = CayenneRuntime.getThreadInjector();
    try {
        CayenneRuntime.bindThreadInjector(injector);
        assertTrue(context.attachToRuntimeIfNeeded());
        assertSame(channel, context.channel);
        assertFalse(context.attachToRuntimeIfNeeded());
        assertFalse(context.attachToRuntimeIfNeeded());
    } finally {
        CayenneRuntime.bindThreadInjector(oldInjector);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) Map(java.util.Map) QueryCache(org.apache.cayenne.cache.QueryCache) Module(org.apache.cayenne.di.Module) Test(org.junit.Test) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Assert(org.junit.Assert) CayenneRuntime(org.apache.cayenne.configuration.CayenneRuntime) Mockito.mock(org.mockito.Mockito.mock) QueryCache(org.apache.cayenne.cache.QueryCache) Injector(org.apache.cayenne.di.Injector) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Aggregations

QueryCache (org.apache.cayenne.cache.QueryCache)6 List (java.util.List)3 QueryCacheEntryFactory (org.apache.cayenne.cache.QueryCacheEntryFactory)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 Module (org.apache.cayenne.di.Module)2 Serializable (java.io.Serializable)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ObjectContext (org.apache.cayenne.ObjectContext)1 MapQueryCache (org.apache.cayenne.cache.MapQueryCache)1 MockQueryCache (org.apache.cayenne.cache.MockQueryCache)1 E1 (org.apache.cayenne.cache.invalidation.db.E1)1 E2 (org.apache.cayenne.cache.invalidation.db.E2)1 CayenneRuntime (org.apache.cayenne.configuration.CayenneRuntime)1 DIBootstrap (org.apache.cayenne.di.DIBootstrap)1 Injector (org.apache.cayenne.di.Injector)1 GraphDiff (org.apache.cayenne.graph.GraphDiff)1 ObjectSelect (org.apache.cayenne.query.ObjectSelect)1 QueryMetadata (org.apache.cayenne.query.QueryMetadata)1