@Override protected void addResponseImpl(String key, HttpResponse response) { cache.addElement(key, response); }
@Override protected HttpResponse getResponseImpl(String key) { return cache.getElement(key); }
private boolean shouldCache() { return parsedCssCache != null && parsedCssCache.getCapacity() != 0; } }
public CssTree.StyleSheet parseDom(String content, Uri source) throws GadgetException { CssTree.StyleSheet parsedCss = null; boolean shouldCache = shouldCache(); String key = null; if (shouldCache) { // TODO - Consider using the source if its under a certain size key = HashUtil.checksum(content.getBytes()); parsedCss = parsedCssCache.getElement(key); } if (parsedCss == null) { try { parsedCss = parseImpl(content, source); if (shouldCache) { parsedCssCache.addElement(key, parsedCss); } } catch (ParseException pe) { // Bad input; not server's fault. throw new GadgetException(GadgetException.Code.CSS_PARSE_ERROR, pe, HttpResponse.SC_BAD_REQUEST); } } if (shouldCache) { return (CssTree.StyleSheet)parsedCss.clone(); } return parsedCss; }
@Test public void getNamedCache() throws Exception { Cache<String, String> cache = defaultProvider.createCache("testcache"); Cache<String, String> cache2 = defaultProvider.createCache("testcache"); Assert.assertNotNull(cache); Assert.assertEquals(cache, cache2); Assert.assertNull(cache.getElement("test")); cache.addElement("test", "value1"); Assert.assertEquals("value1", cache.getElement("test")); cache.removeElement("test"); Assert.assertNull(cache.getElement("test")); Assert.assertEquals(cache.getCapacity(), cache2.getCapacity()); Assert.assertEquals(cache.getSize(), cache2.getSize()); }
@Test public void removeResponse() { HttpRequest request = new HttpRequest(DEFAULT_URI); HttpResponse response = new HttpResponse("response"); String key = httpCache.createKey(request); cache.addElement(key, response); assertEquals(response, httpCache.removeResponse(request)); assertEquals(0, cache.getSize()); } }
@Test public void testInvalidateUsers() throws Exception { service.invalidateUserResources(ImmutableSet.of("example.org:1", "example.org:2"), appxToken); service.invalidateUserResources(ImmutableSet.of("example.org:1", "example.org:2"), appyToken); assertEquals(4, cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME).getSize()); assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME) .getElement("INV_TOK:AppX:1")); assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME) .getElement("INV_TOK:AppX:2")); assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME) .getElement("INV_TOK:AppY:1")); assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME) .getElement("INV_TOK:AppY:2")); }
@Test public void ignoreCacheDoesNotStore() throws Exception { bundleFactory.getBundle(gadgetSpec, new Locale("all", "ALL"), true, ContainerConfig.DEFAULT_CONTAINER, null); assertEquals(0, cache.getSize()); }
@Override protected HttpResponse removeResponseImpl(String key) { return cache.removeElement(key); } }
public List<Object> parse(String content) throws GadgetException { List<Object> parsedCss = null; boolean shouldCache = shouldCache(); String key = null; if (shouldCache) { // TODO - Consider using the source if its under a certain size key = HashUtil.checksum(content.getBytes()); parsedCss = parsedCssCache.getElement(key); } if (parsedCss == null) { parsedCss = parseImpl(content); if (shouldCache) { parsedCssCache.addElement(key, parsedCss); } } if (shouldCache) { List<Object> cloned = Lists.newArrayListWithCapacity(parsedCss.size()); for (Object o : parsedCss) { if (o instanceof ImportDecl) { cloned.add(new ImportDecl(((ImportDecl) o).getUri())); } else if (o instanceof UriDecl) { cloned.add(new UriDecl(((UriDecl) o).getUri())); } else { cloned.add(o); } } return cloned; } return parsedCss; }
@Test public void getNamedCache() throws Exception { Cache<String, String> cache = defaultProvider.createCache("testcache"); Cache<String, String> cache2 = defaultProvider.createCache("testcache"); Assert.assertNotNull(cache); Assert.assertEquals(cache, cache2); Assert.assertNull(cache.getElement("test")); cache.addElement("test", "value1"); Assert.assertEquals("value1", cache.getElement("test")); cache.removeElement("test"); Assert.assertNull(cache.getElement("test")); Assert.assertEquals(cache.getCapacity(), cache2.getCapacity()); Assert.assertEquals(cache.getSize(), cache2.getSize()); }
@Test public void removeResponse() { HttpRequest request = new HttpRequest(DEFAULT_URI); HttpResponse response = new HttpResponse("response"); String key = httpCache.createKey(request); cache.addElement(key, response); assertEquals(response, httpCache.removeResponse(request)); assertEquals(0, cache.getSize()); } }
@Test public void testInvalidateUsers() throws Exception { service.invalidateUserResources(ImmutableSet.of("example.org:1", "example.org:2"), appxToken); service.invalidateUserResources(ImmutableSet.of("example.org:1", "example.org:2"), appyToken); assertEquals(4, cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME).getSize()); assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME) .getElement("INV_TOK:AppX:1")); assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME) .getElement("INV_TOK:AppX:2")); assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME) .getElement("INV_TOK:AppY:1")); assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME) .getElement("INV_TOK:AppY:2")); }
@Test public void ignoreCacheDoesNotStore() throws Exception { bundleFactory.getBundle(gadgetSpec, new Locale("all", "ALL"), true, ContainerConfig.DEFAULT_CONTAINER, null); assertEquals(0, cache.getSize()); }
@Override protected HttpResponse removeResponseImpl(String key) { return cache.removeElement(key); } }
public List<Object> parse(String content) throws GadgetException { List<Object> parsedCss = null; boolean shouldCache = shouldCache(); String key = null; if (shouldCache) { // TODO - Consider using the source if its under a certain size key = HashUtil.checksum(content.getBytes()); parsedCss = parsedCssCache.getElement(key); } if (parsedCss == null) { parsedCss = parseImpl(content); if (shouldCache) { parsedCssCache.addElement(key, parsedCss); } } if (shouldCache) { List<Object> cloned = Lists.newArrayListWithCapacity(parsedCss.size()); for (Object o : parsedCss) { if (o instanceof ImportDecl) { cloned.add(new ImportDecl(((ImportDecl) o).getUri())); } else if (o instanceof UriDecl) { cloned.add(new UriDecl(((UriDecl) o).getUri())); } else { cloned.add(o); } } return cloned; } return parsedCss; }
@Test public void getNamedCache() throws Exception { Cache<String, String> cache = defaultProvider.createCache("testcache"); Cache<String, String> cache2 = defaultProvider.createCache("testcache"); Assert.assertNotNull(cache); Assert.assertEquals(cache, cache2); Assert.assertNull(cache.getElement("test")); cache.addElement("test", "value1"); Assert.assertEquals("value1", cache.getElement("test")); cache.removeElement("test"); Assert.assertNull(cache.getElement("test")); Assert.assertEquals(cache.getCapacity(), cache2.getCapacity()); Assert.assertEquals(cache.getSize(), cache2.getSize()); }
public void put(String expression, Tree tree) { resolvedTreeCache.addElement(expression, tree); } };
@Override protected HttpResponse getResponseImpl(String key) { return cache.getElement(key); }
@Test public void removeResponse() { HttpRequest request = new HttpRequest(DEFAULT_URI); HttpResponse response = new HttpResponse("response"); String key = httpCache.createKey(request); cache.addElement(key, response); assertEquals(response, httpCache.removeResponse(request)); assertEquals(0, cache.getSize()); } }