Search in sources :

Example 1 with QueryCacheEntryFactory

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

the class DataDomainQueryAction method getCacheObjectFactory.

private QueryCacheEntryFactory getCacheObjectFactory() {
    return new QueryCacheEntryFactory() {

        @Override
        public List createObject() {
            runQueryInTransaction();
            List list = response.firstList();
            if (list != null) {
                // make an immutable list to make sure callers don't mess it
                // up
                list = Collections.unmodifiableList(list);
                // include prefetches in the cached result
                if (prefetchResultsByPath != null) {
                    list = new ListWithPrefetches(list, prefetchResultsByPath);
                }
            }
            return list;
        }
    };
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) QueryCacheEntryFactory(org.apache.cayenne.cache.QueryCacheEntryFactory)

Example 2 with QueryCacheEntryFactory

use of org.apache.cayenne.cache.QueryCacheEntryFactory 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 QueryCacheEntryFactory

use of org.apache.cayenne.cache.QueryCacheEntryFactory 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 4 with QueryCacheEntryFactory

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

the class DataDomainQueryAction method interceptSharedCache.

/*
     * Wraps execution in shared cache checks
     */
private final boolean interceptSharedCache() {
    if (metadata.getCacheKey() == null) {
        return !DONE;
    }
    boolean cache = QueryCacheStrategy.SHARED_CACHE == metadata.getCacheStrategy();
    boolean cacheOrCacheRefresh = cache || QueryCacheStrategy.SHARED_CACHE_REFRESH == metadata.getCacheStrategy();
    if (!cacheOrCacheRefresh) {
        return !DONE;
    }
    QueryCache queryCache = domain.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);
        }
        if (cachedResults instanceof ListWithPrefetches) {
            this.prefetchResultsByPath = ((ListWithPrefetches) cachedResults).getPrefetchResultsByPath();
        }
    } 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) ListResponse(org.apache.cayenne.util.ListResponse) ArrayList(java.util.ArrayList) List(java.util.List) QueryCacheEntryFactory(org.apache.cayenne.cache.QueryCacheEntryFactory)

Aggregations

List (java.util.List)4 QueryCacheEntryFactory (org.apache.cayenne.cache.QueryCacheEntryFactory)4 ArrayList (java.util.ArrayList)3 QueryCache (org.apache.cayenne.cache.QueryCache)3 Serializable (java.io.Serializable)1 MockQueryCache (org.apache.cayenne.cache.MockQueryCache)1 QueryMetadata (org.apache.cayenne.query.QueryMetadata)1 SelectQuery (org.apache.cayenne.query.SelectQuery)1 Painting (org.apache.cayenne.testdo.testmap.Painting)1 ListResponse (org.apache.cayenne.util.ListResponse)1 Test (org.junit.Test)1