You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ie...@apache.org on 2012/10/31 06:57:04 UTC

svn commit: r1403995 - in /sling/whiteboard/ieb/cache: api/src/main/java/org/apache/sling/commons/cache/api/ ehcache/src/main/java/org/apache/sling/commons/cache/ehcache/ impl/src/main/java/org/apache/sling/commons/cache/impl/ infinispan/src/main/java/...

Author: ieb
Date: Wed Oct 31 05:57:03 2012
New Revision: 1403995

URL: http://svn.apache.org/viewvc?rev=1403995&view=rev
Log:
SLING-2555 Implemented a version of the Portal Cache API that binds down to the proposed commons cache API and streamlined the latter to avoid copying lists unecessarily.

Added:
    sling/whiteboard/ieb/cache/portal/
    sling/whiteboard/ieb/cache/portal/pom.xml   (with props)
    sling/whiteboard/ieb/cache/portal/src/
    sling/whiteboard/ieb/cache/portal/src/main/
    sling/whiteboard/ieb/cache/portal/src/main/java/
    sling/whiteboard/ieb/cache/portal/src/main/java/org/
    sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/
    sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/
    sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/
    sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/
    sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/Payload.java   (with props)
    sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/PortalCache.java   (with props)
Modified:
    sling/whiteboard/ieb/cache/api/src/main/java/org/apache/sling/commons/cache/api/Cache.java
    sling/whiteboard/ieb/cache/ehcache/src/main/java/org/apache/sling/commons/cache/ehcache/CacheImpl.java
    sling/whiteboard/ieb/cache/impl/src/main/java/org/apache/sling/commons/cache/impl/MapCacheImpl.java
    sling/whiteboard/ieb/cache/infinispan/src/main/java/org/apache/sling/commons/cache/infinispan/CacheImpl.java

Modified: sling/whiteboard/ieb/cache/api/src/main/java/org/apache/sling/commons/cache/api/Cache.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/cache/api/src/main/java/org/apache/sling/commons/cache/api/Cache.java?rev=1403995&r1=1403994&r2=1403995&view=diff
==============================================================================
--- sling/whiteboard/ieb/cache/api/src/main/java/org/apache/sling/commons/cache/api/Cache.java (original)
+++ sling/whiteboard/ieb/cache/api/src/main/java/org/apache/sling/commons/cache/api/Cache.java Wed Oct 31 05:57:03 2012
@@ -19,7 +19,7 @@
 
 package org.apache.sling.commons.cache.api;
 
-import java.util.List;
+import java.util.Collection;
 
 /**
  * A Cache managed by the cache manager.
@@ -69,7 +69,7 @@ public interface Cache<V> {
 	 * @param key
 	 *            The cache key.
 	 */
-	void remove(String key);
+	boolean remove(String key);
 
 	/**
 	 * Remove the key and any child keys from the cache, this is an expensive
@@ -82,6 +82,11 @@ public interface Cache<V> {
 	/**
 	 * @return
 	 */
-	List<V> list();
+	Collection<V> values();
+
+	/**
+	 * @return
+	 */
+	Collection<String> keys();
 
 }

Modified: sling/whiteboard/ieb/cache/ehcache/src/main/java/org/apache/sling/commons/cache/ehcache/CacheImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/cache/ehcache/src/main/java/org/apache/sling/commons/cache/ehcache/CacheImpl.java?rev=1403995&r1=1403994&r2=1403995&view=diff
==============================================================================
--- sling/whiteboard/ieb/cache/ehcache/src/main/java/org/apache/sling/commons/cache/ehcache/CacheImpl.java (original)
+++ sling/whiteboard/ieb/cache/ehcache/src/main/java/org/apache/sling/commons/cache/ehcache/CacheImpl.java Wed Oct 31 05:57:03 2012
@@ -20,6 +20,7 @@
 package org.apache.sling.commons.cache.ehcache;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 
 import net.sf.ehcache.CacheManager;
@@ -144,8 +145,8 @@ public class CacheImpl<V> implements Cac
 	 * 
 	 * @see org.apache.sling.commons.cache.api.Cache#remove(java.lang.String)
 	 */
-	public void remove(String key) {
-		cache.remove(key);
+	public boolean remove(String key) {
+		return cache.remove(key);
 	}
 
 	/**
@@ -172,7 +173,7 @@ public class CacheImpl<V> implements Cac
 	 * @see org.apache.sling.commons.cache.api.Cache#list()
 	 */
 	@SuppressWarnings("unchecked")
-	public List<V> list() {
+	public Collection<V> values() {
 		List<String> keys = cache.getKeys();
 		List<V> values = new ArrayList<V>();
 		for (String k : keys) {
@@ -184,4 +185,10 @@ public class CacheImpl<V> implements Cac
 		return values;
 	}
 
+
+	@SuppressWarnings("unchecked")
+	public Collection<String> keys() {
+		return cache.getKeys();
+	}
+
 }

Modified: sling/whiteboard/ieb/cache/impl/src/main/java/org/apache/sling/commons/cache/impl/MapCacheImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/cache/impl/src/main/java/org/apache/sling/commons/cache/impl/MapCacheImpl.java?rev=1403995&r1=1403994&r2=1403995&view=diff
==============================================================================
--- sling/whiteboard/ieb/cache/impl/src/main/java/org/apache/sling/commons/cache/impl/MapCacheImpl.java (original)
+++ sling/whiteboard/ieb/cache/impl/src/main/java/org/apache/sling/commons/cache/impl/MapCacheImpl.java Wed Oct 31 05:57:03 2012
@@ -20,6 +20,7 @@
 package org.apache.sling.commons.cache.impl;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Set;
@@ -60,11 +61,12 @@ public class MapCacheImpl<V> extends Has
 	 * 
 	 * @see org.apache.sling.commons.cache.api.Cache#remove(java.lang.String)
 	 */
-	public void remove(String key) {
+	public boolean remove(String key) {
 		V o = super.remove(key);
 		if (o instanceof ThreadBound) {
 			((ThreadBound) o).unbind();
 		}
+		return ( o != null );
 	}
 
 	/**
@@ -106,8 +108,12 @@ public class MapCacheImpl<V> extends Has
 	 * 
 	 * @see org.apache.sling.commons.cache.api.Cache#list()
 	 */
-	public List<V> list() {
-		return new ArrayList<V>(super.values());
+	public Collection<V> values() {
+		return super.values();
+	}
+
+	public Collection<String> keys() {
+		return super.keySet();
 	}
 
 }

Modified: sling/whiteboard/ieb/cache/infinispan/src/main/java/org/apache/sling/commons/cache/infinispan/CacheImpl.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/cache/infinispan/src/main/java/org/apache/sling/commons/cache/infinispan/CacheImpl.java?rev=1403995&r1=1403994&r2=1403995&view=diff
==============================================================================
--- sling/whiteboard/ieb/cache/infinispan/src/main/java/org/apache/sling/commons/cache/infinispan/CacheImpl.java (original)
+++ sling/whiteboard/ieb/cache/infinispan/src/main/java/org/apache/sling/commons/cache/infinispan/CacheImpl.java Wed Oct 31 05:57:03 2012
@@ -19,9 +19,7 @@
 
 package org.apache.sling.commons.cache.infinispan;
 
-import java.util.ArrayList;
-import java.util.List;
-
+import java.util.Collection;
 
 import org.apache.sling.commons.cache.api.Cache;
 import org.infinispan.manager.EmbeddedCacheManager;
@@ -134,8 +132,8 @@ public class CacheImpl<V> implements Cac
 	 * 
 	 * @see org.apache.sling.commons.cache.api.Cache#remove(java.lang.String)
 	 */
-	public void remove(String key) {
-		cache.remove(key);
+	public boolean remove(String key) {
+		return (cache.remove(key) != null);
 	}
 
 	/**
@@ -160,15 +158,13 @@ public class CacheImpl<V> implements Cac
 	 * 
 	 * @see org.apache.sling.commons.cache.api.Cache#list()
 	 */
-	public List<V> list() {
-		List<V> values = new ArrayList<V>();
-		for (String k : cache.keySet()) {
-			V v = cache.get(k);
-			if (v != null) {
-				values.add(v);
-			}
-		}
-		return values;
+	public Collection<V> values() {
+		return cache.values();
+	}
+
+	public Collection<String> keys() {
+		return cache.keySet();
 	}
 
+
 }

Added: sling/whiteboard/ieb/cache/portal/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/cache/portal/pom.xml?rev=1403995&view=auto
==============================================================================
--- sling/whiteboard/ieb/cache/portal/pom.xml (added)
+++ sling/whiteboard/ieb/cache/portal/pom.xml Wed Oct 31 05:57:03 2012
@@ -0,0 +1,119 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+    
+    http://www.apache.org/licenses/LICENSE-2.0
+    
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>13</version>
+        <relativePath>../../../parent/pom.xml</relativePath>
+    </parent>
+
+    <artifactId>org.apache.sling.commons.cache.portal</artifactId>
+    <version>0.1-SNAPSHOT</version>
+    <packaging>bundle</packaging>
+
+    <name>Apache Sling Cache Portal Cache</name>
+    <description>
+        This bundle provides an implementation of the portal Cache using the Cache API .
+    </description>
+
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/whiteboard/ieb/cache/portal</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/whiteboard/ieb/cache/portal</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/whiteboard/ieb/cache/portal</url>
+    </scm>
+    
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <version>2.3.6</version>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+		            <Import-Package>
+		               *
+		            </Import-Package>
+		            <Private-Package>
+		            	org.apache.sling.portal.cache.*,
+		            </Private-Package>
+		            <Embed-Transitive>true</Embed-Transitive>
+		            <Embed-Dependency>
+		            </Embed-Dependency>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <dependencies>
+        <dependency>
+	       	<groupId>org.apache.sling</groupId>
+	       	<artifactId>org.apache.sling.commons.cache.api</artifactId>
+	       	<version>0.1-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+	       	<groupId>org.apache.sling.portal</groupId>
+	       	<artifactId>org.apache.sling.portal.container</artifactId>
+	       	<version>0.6.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+        	<groupId>javax.servlet</groupId>
+        	<artifactId>servlet-api</artifactId>
+        	<version>2.5</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.portals.pluto</groupId>
+            <artifactId>pluto-container-api</artifactId>
+            <version>2.0.3</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+        	<groupId>org.slf4j</groupId>
+        	<artifactId>slf4j-simple</artifactId>
+       		<scope>test</scope>
+        </dependency>
+         <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.8.2</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+        
+    </dependencies>
+</project>

Propchange: sling/whiteboard/ieb/cache/portal/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/ieb/cache/portal/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/Payload.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/Payload.java?rev=1403995&view=auto
==============================================================================
--- sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/Payload.java (added)
+++ sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/Payload.java Wed Oct 31 05:57:03 2012
@@ -0,0 +1,46 @@
+package org.apache.sling.portal.cache;
+
+
+/**
+ * A simple payload wrapper for the cache to record when it expires.
+ *
+ * @param <V>
+ */
+public class Payload<V> {
+
+	private long ttiEnd;
+	private long ttlEnd;
+	private long tti;
+	private V payload;
+
+	/**
+	 * Create the payload
+	 * @param payload the payload itself
+	 * @param timeToIdleSeconds how many seconds the payload can be idle for before it becomes invalid.
+	 * @param timeToLiveSeconds how many seconds the payload can be live for.
+	 */
+	public Payload(V payload, long timeToIdleSeconds,
+			long timeToLiveSeconds) {
+		this.payload = payload;
+		long t = System.currentTimeMillis();
+		tti = timeToIdleSeconds+1000;
+		ttiEnd = t+tti;
+		ttlEnd = t+timeToLiveSeconds*1000;
+	}
+
+	/**
+	 * @return the payload if its still valid.
+	 */
+	public V get() {
+		long t = System.currentTimeMillis();
+		if ( t > ttiEnd ) {
+			return null;
+		}
+		if ( t > ttlEnd ) {
+			return null;
+		}
+		ttiEnd = t + tti;
+		return payload;
+	}
+
+}

Propchange: sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/Payload.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/PortalCache.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/PortalCache.java?rev=1403995&view=auto
==============================================================================
--- sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/PortalCache.java (added)
+++ sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/PortalCache.java Wed Oct 31 05:57:03 2012
@@ -0,0 +1,110 @@
+package org.apache.sling.portal.cache;
+
+import java.util.Collection;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.pluto.container.PortletWindow;
+import org.apache.sling.commons.cache.api.CacheManagerService;
+import org.apache.sling.commons.cache.api.CacheScope;
+import org.apache.sling.portal.container.cache.Cache;
+import org.apache.sling.portal.container.cache.CacheValue;
+
+/**
+ * Implementation of the Portals Cache service using the Cache API.
+ *
+ */
+@Component(immediate=true, metatype=true)
+@Service
+public class PortalCache implements Cache {
+	
+	private static final long DEFAULT_TTL = 120;
+	private static final long DEFAULT_TTI = 30;
+	private static final String DEFAULT_CACHE_NAME = "portal";
+	
+	
+	@Property(longValue=DEFAULT_TTL)
+	private static String CONF_TIME_TO_LIVE = "time-to-live";
+	@Property(longValue=DEFAULT_TTI)
+	private static String CONF_TIME_TO_IDLE = "time-to-idle";
+	@Property(value=DEFAULT_CACHE_NAME)
+	private static String CONF_CACHE_NAME = "cache-name";
+	
+
+	@Reference
+	private CacheManagerService cacheManagerService;
+	private org.apache.sling.commons.cache.api.Cache<Payload<CacheValue>> cache;
+	private long timeToLive = DEFAULT_TTL;
+	private long timeToIdle = DEFAULT_TTI;
+	private String cacheName = DEFAULT_CACHE_NAME;
+	
+	@Activate
+	public void activate(Map<String, Object> properties) {
+		cacheName = get(CONF_CACHE_NAME, properties, DEFAULT_CACHE_NAME);
+		timeToLive = get(CONF_TIME_TO_LIVE, properties, DEFAULT_TTL);
+		timeToIdle = get(CONF_TIME_TO_IDLE, properties, DEFAULT_TTI);
+		cache = cacheManagerService.getCache(cacheName, CacheScope.CLUSTERINVALIDATED);
+	}
+	
+	private <V> V get(String key, Map<String, Object> properties,
+			V defaultValue) {
+		@SuppressWarnings("unchecked")
+		V v = (V) properties.get(key);
+		if ( v == null ) {
+			return defaultValue;
+		}
+		return v;
+	}
+
+	@Deactivate 
+	public void deactivate(Map<String, Object> properties) {
+	}
+
+	public CacheValue getCacheEntry(String key) {
+		Payload<CacheValue> h = cache.get(key);
+		if ( h == null ) {
+			return null;
+		}
+		return h.get();
+	}
+
+	public void putCacheEntry(String key, CacheValue cachedResponse) {
+		cache.put(key, new Payload<CacheValue>(cachedResponse, getTimeToIdleSeconds(), getTimeToLiveSeconds()));
+	}
+
+	public boolean removeCacheEntry(String key) {
+		return cache.remove(key);
+	}
+
+	public Collection<String> getKeys() {
+		return cache.keys();
+	}
+
+	public void clearCache() {
+		cache.clear();
+	}
+
+	public String createCacheKey(PortletWindow portletWindow,
+			HttpServletRequest request) {
+		// FIXME: Check that this gives the correct isolation to items in the cache.
+		// Keys should be the same regardless of the app server instance.
+		// ignoring the user.
+		return request.getRequestURI()+":"+portletWindow.getId();
+	}
+
+	public long getTimeToIdleSeconds() {
+		return timeToIdle;
+	}
+
+	public long getTimeToLiveSeconds() {
+		return timeToLive;
+	}
+
+}

Propchange: sling/whiteboard/ieb/cache/portal/src/main/java/org/apache/sling/portal/cache/PortalCache.java
------------------------------------------------------------------------------
    svn:eol-style = native