You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/10/18 23:23:35 UTC

[sling-org-apache-sling-commons-cache-portal] 01/07: SLING-2555 moving into the right place this time, sorry.

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-cache-portal.git

commit d58baed4e313a5e276c9b3797e654686b6af16f1
Author: Ian Boston <ie...@apache.org>
AuthorDate: Fri Nov 9 05:22:15 2012 +0000

    SLING-2555 moving into the right place this time, sorry.
    
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1407364 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            | 119 +++++++++++++++++++
 .../org/apache/sling/portal/cache/Payload.java     |  65 +++++++++++
 .../org/apache/sling/portal/cache/PortalCache.java | 129 +++++++++++++++++++++
 3 files changed, 313 insertions(+)

diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..b827797
--- /dev/null
+++ b/pom.xml
@@ -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>
diff --git a/src/main/java/org/apache/sling/portal/cache/Payload.java b/src/main/java/org/apache/sling/portal/cache/Payload.java
new file mode 100644
index 0000000..1eb74b2
--- /dev/null
+++ b/src/main/java/org/apache/sling/portal/cache/Payload.java
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+
+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;
+	}
+
+}
diff --git a/src/main/java/org/apache/sling/portal/cache/PortalCache.java b/src/main/java/org/apache/sling/portal/cache/PortalCache.java
new file mode 100644
index 0000000..049f408
--- /dev/null
+++ b/src/main/java/org/apache/sling/portal/cache/PortalCache.java
@@ -0,0 +1,129 @@
+/*
+ * 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.
+ */
+
+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;
+	}
+
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.