Search in sources :

Example 6 with CacheContent

use of com.adobe.acs.commons.httpcache.engine.CacheContent in project acs-aem-commons by Adobe-Consulting-Services.

the class JCRHttpCacheStoreImpl method getIfPresent.

@Override
public CacheContent getIfPresent(final CacheKey key) {
    final long currentTime = System.currentTimeMillis();
    incrementRequestCount();
    return withSession(new Function<Session, CacheContent>() {

        @Override
        public CacheContent apply(Session session) throws Exception {
            final BucketNodeFactory factory = new BucketNodeFactory(session, cacheRootPath, key, bucketTreeDepth);
            final Node bucketNode = factory.getBucketNode();
            if (bucketNode != null) {
                final Node entryNode = new BucketNodeHandler(bucketNode, dclm).getEntryIfExists(key);
                final CacheContent content = new EntryNodeToCacheContentHandler(entryNode).get();
                if (content != null) {
                    incrementTotalLookupTime(System.currentTimeMillis() - currentTime);
                    incrementHitCount();
                    return content;
                }
            }
            incrementTotalLookupTime(System.currentTimeMillis() - currentTime);
            incrementMissCount();
            return null;
        }
    });
}
Also used : BucketNodeFactory(com.adobe.acs.commons.httpcache.store.jcr.impl.writer.BucketNodeFactory) CacheContent(com.adobe.acs.commons.httpcache.engine.CacheContent) Node(javax.jcr.Node) EntryNodeToCacheContentHandler(com.adobe.acs.commons.httpcache.store.jcr.impl.handler.EntryNodeToCacheContentHandler) CacheMBeanException(com.adobe.acs.commons.util.impl.exception.CacheMBeanException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) HttpCacheDataStreamException(com.adobe.acs.commons.httpcache.exception.HttpCacheDataStreamException) OpenDataException(javax.management.openmbean.OpenDataException) IOException(java.io.IOException) BucketNodeHandler(com.adobe.acs.commons.httpcache.store.jcr.impl.handler.BucketNodeHandler) Session(javax.jcr.Session)

Example 7 with CacheContent

use of com.adobe.acs.commons.httpcache.engine.CacheContent in project acs-aem-commons by Adobe-Consulting-Services.

the class EntryNodeMapVisitor method entering.

protected void entering(final Node node, int level) throws RepositoryException {
    super.entering(node, level);
    if (isCacheEntryNode(node)) {
        CacheKey cacheKey;
        try {
            cacheKey = getCacheKey(node);
            CacheContent content = getCacheContent(node);
            cache.put(cacheKey, content);
        } catch (Exception e) {
            log.error("Error in reading cache node!", e);
        }
    }
}
Also used : CacheContent(com.adobe.acs.commons.httpcache.engine.CacheContent) CacheKey(com.adobe.acs.commons.httpcache.keys.CacheKey) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException)

Example 8 with CacheContent

use of com.adobe.acs.commons.httpcache.engine.CacheContent in project acs-aem-commons by Adobe-Consulting-Services.

the class EntryNodeMapVisitorTest method test5entries.

@Test
public void test5entries() throws Exception {
    final RootNodeMockFactory.Settings settings = new RootNodeMockFactory.Settings();
    settings.setEntryNodeCount(5);
    final Node rootNode = new RootNodeMockFactory(settings).build();
    final EntryNodeMapVisitor visitor = getMockedNodeMapVisitor();
    visitor.visit(rootNode);
    final Map<CacheKey, CacheContent> cache = visitor.getCache();
    assertEquals(5, cache.size());
}
Also used : RootNodeMockFactory(com.adobe.acs.commons.httpcache.store.jcr.impl.visitor.mock.RootNodeMockFactory) Node(javax.jcr.Node) CacheContent(com.adobe.acs.commons.httpcache.engine.CacheContent) CacheKey(com.adobe.acs.commons.httpcache.keys.CacheKey) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with CacheContent

use of com.adobe.acs.commons.httpcache.engine.CacheContent in project acs-aem-commons by Adobe-Consulting-Services.

the class EntryNodeMapVisitorTest method test10entries.

@Test
public void test10entries() throws Exception {
    final RootNodeMockFactory.Settings settings = new RootNodeMockFactory.Settings();
    settings.setEntryNodeCount(10);
    final Node rootNode = new RootNodeMockFactory(settings).build();
    final EntryNodeMapVisitor visitor = getMockedNodeMapVisitor();
    visitor.visit(rootNode);
    final Map<CacheKey, CacheContent> cache = visitor.getCache();
    assertEquals(10, cache.size());
}
Also used : RootNodeMockFactory(com.adobe.acs.commons.httpcache.store.jcr.impl.visitor.mock.RootNodeMockFactory) Node(javax.jcr.Node) CacheContent(com.adobe.acs.commons.httpcache.engine.CacheContent) CacheKey(com.adobe.acs.commons.httpcache.keys.CacheKey) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with CacheContent

use of com.adobe.acs.commons.httpcache.engine.CacheContent in project acs-aem-commons by Adobe-Consulting-Services.

the class HttpCacheEngineImpl method cacheResponse.

@Override
public void cacheResponse(SlingHttpServletRequest request, SlingHttpServletResponse response, HttpCacheConfig cacheConfig) throws HttpCacheKeyCreationException, HttpCacheDataStreamException, HttpCachePersistenceException {
    // TODO - This can be made asynchronous to avoid performance penalty on response cache.
    CacheContent cacheContent = null;
    try {
        // Construct the cache content.
        HttpCacheServletResponseWrapper responseWrapper = null;
        if (response instanceof HttpCacheServletResponseWrapper) {
            responseWrapper = (HttpCacheServletResponseWrapper) response;
        } else {
            throw new AssertionError("Programming error.");
        }
        CacheKey cacheKey = cacheConfig.buildCacheKey(request);
        cacheContent = new CacheContent().build(responseWrapper);
        // Persist in cache.
        if (isRequestCachableAccordingToHandlingRules(request, response, cacheConfig, cacheContent)) {
            getCacheStore(cacheConfig).put(cacheKey, cacheContent);
            log.debug("Response for the URI cached - {}", request.getRequestURI());
        }
    } finally {
        // Close the temp sink input stream.
        if (null != cacheContent) {
            IOUtils.closeQuietly(cacheContent.getInputDataStream());
        }
    }
}
Also used : HttpCacheServletResponseWrapper(com.adobe.acs.commons.httpcache.engine.HttpCacheServletResponseWrapper) CacheContent(com.adobe.acs.commons.httpcache.engine.CacheContent) CacheKey(com.adobe.acs.commons.httpcache.keys.CacheKey)

Aggregations

CacheContent (com.adobe.acs.commons.httpcache.engine.CacheContent)10 CacheKey (com.adobe.acs.commons.httpcache.keys.CacheKey)6 Node (javax.jcr.Node)5 RootNodeMockFactory (com.adobe.acs.commons.httpcache.store.jcr.impl.visitor.mock.RootNodeMockFactory)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 IOException (java.io.IOException)2 HttpCacheServletResponseWrapper (com.adobe.acs.commons.httpcache.engine.HttpCacheServletResponseWrapper)1 HttpCacheDataStreamException (com.adobe.acs.commons.httpcache.exception.HttpCacheDataStreamException)1 BucketNodeHandler (com.adobe.acs.commons.httpcache.store.jcr.impl.handler.BucketNodeHandler)1 EntryNodeToCacheContentHandler (com.adobe.acs.commons.httpcache.store.jcr.impl.handler.EntryNodeToCacheContentHandler)1 BucketNodeFactory (com.adobe.acs.commons.httpcache.store.jcr.impl.writer.BucketNodeFactory)1 MemTempSinkImpl (com.adobe.acs.commons.httpcache.store.mem.impl.MemTempSinkImpl)1 CacheMBeanException (com.adobe.acs.commons.util.impl.exception.CacheMBeanException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)1 OpenDataException (javax.management.openmbean.OpenDataException)1