Author: etnu
Date: Sat May 17 19:50:25 2008
New Revision: 657493
URL: http://svn.apache.org/viewvc?rev=657493&view=rev
Log:
Added a simple LRU cache implementation for use in numerous places where in
memory caching is necessary.
The test cases for this are using junit4 instead of junit3 as an experiment.
This will either be converted to junit3 or all other tests will be converted to
junit4 in the future.
Added:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/Cache.java
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/LruCache.java
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/cache/
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/cache/LruCacheTest.java
Added:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/Cache.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/Cache.java?rev=657493&view=auto
==============================================================================
---
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/Cache.java
(added)
+++
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/Cache.java
Sat May 17 19:50:25 2008
@@ -0,0 +1,46 @@
+/*
+ * 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.shindig.common.cache;
+
+/**
+ * A basic cache interface. If necessary, we can always move to the commons
+ * cache for the future.
+ */
+public interface Cache<K, V> {
+
+ /**
+ * Retrieves an entry for the cache.
+ *
+ * @return The entry stored under the given key, or null if it doesn't exist.
+ */
+ public V getElement(K key);
+
+ /**
+ * Stores an entry into the cache.
+ */
+ public void addElement(K key, V value);
+
+ /**
+ * Removes an entry from the cache.
+ *
+ * @param key The entry to return.
+ * @return The entry stored under the given key, or null if it doesn't exist.
+ */
+ public V removeElement(K key);
+}
Added:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/LruCache.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/LruCache.java?rev=657493&view=auto
==============================================================================
---
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/LruCache.java
(added)
+++
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/cache/LruCache.java
Sat May 17 19:50:25 2008
@@ -0,0 +1,60 @@
+/*
+ * 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.shindig.common.cache;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * A basic LRU cache.
+ */
+public class LruCache<K, V> extends LinkedHashMap<K, V> implements Cache<K, V>
{
+
+ private final int capacity;
+
+ public V getElement(K key) {
+ return super.get(key);
+ }
+
+ public void addElement(K key, V value) {
+ super.put(key, value);
+ }
+
+ public V removeElement(K key) {
+ return super.remove(key);
+ }
+
+ @Override
+ protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
+ return size() > capacity;
+ }
+
+ /**
+ * Convenience method for producing a new cache that takes advantage of
+ * type inference.
+ */
+ public static <K, V> LruCache<K, V> create(int capacity) {
+ return new LruCache<K, V>(capacity);
+ }
+
+ public LruCache(int capacity) {
+ super(capacity, 0.75f, true);
+ this.capacity = capacity;
+ }
+}
Added:
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/cache/LruCacheTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/cache/LruCacheTest.java?rev=657493&view=auto
==============================================================================
---
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/cache/LruCacheTest.java
(added)
+++
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/cache/LruCacheTest.java
Sat May 17 19:50:25 2008
@@ -0,0 +1,47 @@
+/*
+ * 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.shindig.common.cache;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class LruCacheTest {
+
+ private static final int TEST_CAPACITY = 2;
+ private LruCache<String, String> cache = LruCache.create(TEST_CAPACITY);
+
+ @Test
+ public void normalCapacityOk() {
+ for (int i = 0; i < TEST_CAPACITY; ++i) {
+ cache.addElement(Integer.toString(i), Integer.toString(i));
+ }
+ assertEquals(TEST_CAPACITY, cache.size());
+ assertEquals("0", cache.getElement("0"));
+ }
+
+ @Test
+ public void exceededCapacityRemoved() {
+ for (int i = 0; i < TEST_CAPACITY + 1; ++i) {
+ cache.addElement(Integer.toString(i), Integer.toString(i));
+ }
+ assertEquals(TEST_CAPACITY, cache.size());
+ assertEquals(null, cache.getElement("0"));
+ }
+}