You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2007/07/13 04:37:44 UTC
svn commit: r555842 - in
/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache:
InMemoryCache.java LRUMap.java lru/LRUCache.java
Author: jmsnell
Date: Thu Jul 12 19:37:43 2007
New Revision: 555842
URL: http://svn.apache.org/viewvc?view=rev&rev=555842
Log:
Cache the most recently used cache keys so we do not have to create brand new ones on every request to the same url.
It's a minor thing, but since we calculate an md5 digest every time we create a simple cache key, creating a new key
has a definite performance impact
Added:
incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/LRUMap.java
Modified:
incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java
incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCache.java
Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java?view=diff&rev=555842&r1=555841&r2=555842
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java (original)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/InMemoryCache.java Thu Jul 12 19:37:43 2007
@@ -29,6 +29,8 @@
extends CacheBase {
protected transient Map<CacheKey,CachedResponse> cache;
+ protected transient final Map<String,SimpleCacheKey> keycache =
+ new LRUMap<String,SimpleCacheKey>(20,0.75f,true);
protected InMemoryCache(Abdera abdera) {
super(abdera);
@@ -58,8 +60,7 @@
public CacheKey getCacheKey(
String uri,
RequestOptions options) {
- //TODO: We need a complete solution that takes the Vary header into account
- return new SimpleCacheKey(uri);
+ return getCacheKey(uri,options,null);
}
public CacheKey getCacheKey(
@@ -67,7 +68,12 @@
RequestOptions options,
ClientResponse response) {
//TODO: We need a complete solution that takes the Vary header into account
- return new SimpleCacheKey(uri);
+ SimpleCacheKey key = keycache.get(uri);
+ if (key == null) {
+ key = new SimpleCacheKey(uri);
+ keycache.put(uri, key);
+ }
+ return key;
}
public void remove(
Added: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/LRUMap.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/LRUMap.java?view=auto&rev=555842
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/LRUMap.java (added)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/LRUMap.java Thu Jul 12 19:37:43 2007
@@ -0,0 +1,38 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. 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. For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.client.cache;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+public final class LRUMap<A,B> extends LinkedHashMap<A,B> {
+
+ private static final long serialVersionUID = -8243948270889739367L;
+ private final int size;
+
+ public LRUMap(int initialSize, float loadFactor, boolean accessOrder) {
+ super(initialSize, loadFactor, accessOrder);
+ this.size = initialSize;
+ }
+
+ protected boolean removeEldestEntry(
+ Map.Entry<A,B> eldest) {
+ return size() > size;
+ }
+
+}
Modified: incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCache.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCache.java?view=diff&rev=555842&r1=555841&r2=555842
==============================================================================
--- incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCache.java (original)
+++ incubator/abdera/java/trunk/client/src/main/java/org/apache/abdera/protocol/client/cache/lru/LRUCache.java Thu Jul 12 19:37:43 2007
@@ -17,14 +17,12 @@
*/
package org.apache.abdera.protocol.client.cache.lru;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
import org.apache.abdera.Abdera;
import org.apache.abdera.protocol.client.cache.Cache;
import org.apache.abdera.protocol.client.cache.CacheKey;
import org.apache.abdera.protocol.client.cache.CachedResponse;
import org.apache.abdera.protocol.client.cache.InMemoryCache;
+import org.apache.abdera.protocol.client.cache.LRUMap;
@SuppressWarnings("serial")
public class LRUCache
@@ -39,15 +37,7 @@
public LRUCache(Abdera abdera, final int size) {
super(abdera);
- setMap(
- new LinkedHashMap<CacheKey,CachedResponse>(size,0.75f,true) {
- @Override
- protected boolean removeEldestEntry(
- Map.Entry<CacheKey,CachedResponse> eldest) {
- return size() > size;
- }
- }
- );
+ setMap(new LRUMap<CacheKey,CachedResponse>(size,0.75f,true));
}
}