summaryrefslogtreecommitdiff
path: root/israfil-foundation-cache
diff options
context:
space:
mode:
authorBenjamin Culkin <scorpress@gmail.com>2024-05-27 11:38:33 -0400
committerBenjamin Culkin <scorpress@gmail.com>2024-05-27 11:38:33 -0400
commit7c279747beb43c7e88633a6228a155a30e6834f7 (patch)
tree511176048944fa7332dc1a163a6148c46e7c61b3 /israfil-foundation-cache
Initial import
Diffstat (limited to 'israfil-foundation-cache')
-rw-r--r--israfil-foundation-cache/pom.xml29
-rw-r--r--israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/Cache.java54
-rw-r--r--israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/Fetcher.java46
-rw-r--r--israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/FetcherCache.java48
-rw-r--r--israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/GenericCache.java96
-rw-r--r--israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/GenericFetcherCache.java124
-rw-r--r--israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/ObjectFactory.java46
-rw-r--r--israfil-foundation-cache/src/test/java/net/israfil/foundation/caching/GenericCacheTest.java124
8 files changed, 567 insertions, 0 deletions
diff --git a/israfil-foundation-cache/pom.xml b/israfil-foundation-cache/pom.xml
new file mode 100644
index 0000000..4167e2e
--- /dev/null
+++ b/israfil-foundation-cache/pom.xml
@@ -0,0 +1,29 @@
+<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>net.israfil.foundation</groupId>
+ <artifactId>israfil-foundation-all</artifactId>
+ <version>5-SNAPSHOT</version>
+ </parent>
+ <artifactId>israfil-foundation-cache</artifactId>
+ <name>Israfil Foundation Lightweight Caching</name>
+ <version>1.0.1-SNAPSHOT</version>
+ <url>http://www.israfil.net/projects/foundation/${artifactId}</url>
+ <inceptionYear>2003</inceptionYear>
+ <licenses>
+ <license>
+ <name>BSD</name>
+ <distribution>repo</distribution>
+ <url>http://www.israfil.net/israfil-license-bsd.txt</url>
+ <comments>A simple open-source license with minimal restrictions</comments>
+ </license>
+ </licenses>
+ <dependencies>
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>israfil-foundation-core</artifactId>
+ <version>[1.0.0,1.1)</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project> \ No newline at end of file
diff --git a/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/Cache.java b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/Cache.java
new file mode 100644
index 0000000..2ad96cf
--- /dev/null
+++ b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/Cache.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: Cache.java 499 2007-05-15 19:16:17Z christianedwardgruber $
+ */
+package net.israfil.foundation.caching;
+
+
+/**
+ * A cache object
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber</a>
+ */
+public interface Cache<K,T> {
+
+ public String getId();
+
+ public ObjectFactory<T> getFactory();
+
+ public void setFactory(ObjectFactory<T> factory);
+
+ public T get(K ... path) ;
+
+ public void purge(K ... path);
+
+}
diff --git a/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/Fetcher.java b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/Fetcher.java
new file mode 100644
index 0000000..06b9a29
--- /dev/null
+++ b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/Fetcher.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: Fetcher.java 129 2006-12-31 23:20:02Z cgruber $
+ */
+package net.israfil.foundation.caching;
+
+
+/**
+ * The main application class for the Quote Refresh Service.
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber</a>
+ */
+public interface Fetcher {
+
+ public void fetch();
+
+}
diff --git a/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/FetcherCache.java b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/FetcherCache.java
new file mode 100644
index 0000000..0bdccc1
--- /dev/null
+++ b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/FetcherCache.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: Cache.java 129 2006-12-31 23:20:02Z cgruber $
+ */
+package net.israfil.foundation.caching;
+
+
+/**
+ * A cache object
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber</a>
+ */
+public interface FetcherCache {
+
+ public Object get(Fetcher fetcher, String cacheName, String itemKey, String ... path) ;
+
+ void set(String cacheName, String itemKey, Object value, String ... path);
+
+}
diff --git a/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/GenericCache.java b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/GenericCache.java
new file mode 100644
index 0000000..37530a0
--- /dev/null
+++ b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/GenericCache.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: GenericCache.java 499 2007-05-15 19:16:17Z christianedwardgruber $
+ */
+package net.israfil.foundation.caching;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+
+/**
+ * A non-thread-safe cache object
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber</a>
+ */
+public class GenericCache<K,T> implements Cache <K,T>{
+
+ private final ReadWriteLock lock = new ReentrantReadWriteLock();
+
+ private String id;
+
+ private ObjectFactory<T> factory;
+
+ private Map<K[],T> _caches = new HashMap<K[],T>();
+
+ public GenericCache(String id) {
+ this.id = id;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ /**
+ * Retrieves an object from a path in the cache
+ * @param An array of path items identifying the path through the map keys.
+ */
+ public T get(K ... path) {
+
+ // Get the item from the map
+
+ return null;
+ }
+
+ public ObjectFactory<T> getFactory() {
+ return factory;
+ }
+
+ public void purge(K... path) {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setFactory(ObjectFactory<T> factory) {
+ try {
+ lock.writeLock().lock();
+ this.factory = factory;
+
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+
+}
diff --git a/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/GenericFetcherCache.java b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/GenericFetcherCache.java
new file mode 100644
index 0000000..ce42bc2
--- /dev/null
+++ b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/GenericFetcherCache.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: GenericCache.java 129 2006-12-31 23:20:02Z cgruber $
+ */
+package net.israfil.foundation.caching;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * A non-thread-safe cache object
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber</a>
+ */
+public class GenericFetcherCache implements FetcherCache {
+
+ private Map<String,Object> _caches = new HashMap<String,Object>();
+
+ /**
+ * Retrieves an object from a path in the cache
+ * @param cacheName A string identifying from which cache the object will be retrieved
+ * @param An array of path items identifying the path through the map keys.
+ */
+ public Object get(Fetcher fetcher, String cacheName, String itemKey, String ... path) {
+
+ // Fetch the map containing the item.
+ Map<String,Object> cache = _getLocationInMap(_caches,new ArrayList<String>(_prepPathList(cacheName,path)));
+
+ // Get the item from the map
+ Object o = cache.get((itemKey==null) ? "__NULL__" : itemKey);
+ if (o == null) {
+ // Item doesn't exist, so try to fetch, then retrieve again.
+ fetcher.fetch();
+ o = cache.get((itemKey==null) ? "__NULL__" : itemKey);
+ }
+ return o;
+ }
+
+
+ /**
+ * A method to set values within the cache, to be used by Fetcher implementations
+ * in setting up the cache when they fetch.
+ */
+ public void set(String cacheName, String itemKey, Object value, String ... path) {
+ List<String> pathList = _prepPathList(cacheName,path);
+ Map<String,Object> cache = _getLocationInMap(_caches,new ArrayList<String>(pathList));
+ cache.put(itemKey,value);
+ }
+
+ /**
+ * Creates a List that contains the pathelements, starting from the
+ * cache name down to the bottom, but excluding the actual final path
+ * item.
+ */
+ static List<String> _prepPathList(String cacheName,String[] pathElements) {
+ List<String> pathList;
+ if (pathElements == null || pathElements.length < 1) {
+ pathList = new ArrayList<String>();
+ } else {
+ // Need to make this explicitly an arrayList, so that
+ // add(index,value) will work.
+ pathList = new ArrayList<String>(Arrays.asList(pathElements));
+ }
+ pathList.add(0,cacheName);
+ return pathList;
+ }
+
+ /**
+ * Recursively traverses the cache map, creating maps at path locations
+ * where they do not already exist.
+ */
+ @SuppressWarnings("unchecked")
+ static Map<String, Object> _getLocationInMap(
+ Map<String, Object> currentMap, List<String> cachePath) {
+ if (cachePath == null || cachePath.size() < 1) return currentMap;
+ String cacheName = (String)cachePath.remove(0); // pull the first one
+
+ Object cacheObject = (Map<String,Object>)currentMap.get(cacheName);
+ if (cacheObject == null) {
+ cacheObject = new HashMap<String,Object>();
+ currentMap.put(cacheName,cacheObject);
+ }
+ if (! (cacheObject instanceof Map))
+ throw new IllegalArgumentException("Attempted to find a map at location ("+cachePath+") in cache ("+cacheName+"), but found a " + cacheObject.getClass().getName() + ".");
+ Map<String,Object> cache = (Map<String,Object>)cacheObject;
+
+ return _getLocationInMap(cache,cachePath);
+ }
+
+
+}
diff --git a/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/ObjectFactory.java b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/ObjectFactory.java
new file mode 100644
index 0000000..b16ee1c
--- /dev/null
+++ b/israfil-foundation-cache/src/main/java/net/israfil/foundation/caching/ObjectFactory.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2006 Israfil Consulting Services Corporation
+ * Copyright (c) 2006 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: Fetcher.java 129 2006-12-31 23:20:02Z cgruber $
+ */
+package net.israfil.foundation.caching;
+
+
+/**
+ * The main application class for the Quote Refresh Service.
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber</a>
+ */
+public interface ObjectFactory <T> {
+
+ public T produce(Object ... parameters);
+
+}
diff --git a/israfil-foundation-cache/src/test/java/net/israfil/foundation/caching/GenericCacheTest.java b/israfil-foundation-cache/src/test/java/net/israfil/foundation/caching/GenericCacheTest.java
new file mode 100644
index 0000000..2ea505c
--- /dev/null
+++ b/israfil-foundation-cache/src/test/java/net/israfil/foundation/caching/GenericCacheTest.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright © 2003-2009 Israfil Consulting Services Corporation
+ * Copyright © 2003-2009 Christian Edward Gruber
+ * All Rights Reserved
+ *
+ * This software is licensed under the Berkeley Standard Distribution license,
+ * (BSD license), as defined below:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. Neither the name of Israfil Consulting Services nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * $Id: GenericCacheTest.java 499 2007-05-15 19:16:17Z christianedwardgruber $
+ */
+package net.israfil.foundation.caching;
+
+import net.israfil.foundation.core.Counter;
+import net.israfil.foundation.core.SimpleCounter;
+
+import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author <a href="mailto:cgruber@israfil.net">Christian Edward Gruber </a>
+ * @author Latest: $Author: christianedwardgruber $
+ * @version $Revision: 499 $
+ */
+@Test
+public class GenericCacheTest {
+
+ Object o;
+
+ @BeforeMethod
+ protected void setUp() throws Exception {
+ o = "AnObject";
+ }
+
+ @AfterMethod
+ protected void tearDown() throws Exception {
+ o = null;
+ }
+
+ public void testGenericCache() {
+ final Counter fetchCount = new SimpleCounter();
+ final FetcherCache c = new GenericFetcherCache();
+ Assert.assertEquals(0,fetchCount.getCount());
+ Assert.assertEquals(o,c.get(new Fetcher(){
+ public void fetch() {
+ c.set("testCache","testItem",o);
+ fetchCount.increment();
+ }
+ },"testCache","testItem"));
+ Assert.assertEquals(1,fetchCount.getCount());
+ Assert.assertEquals(o,c.get(new Fetcher(){
+ public void fetch() {
+ c.set("testCache","testItem",o);
+ fetchCount.increment();
+ }
+ },"testCache","testItem"));
+ Assert.assertEquals(1,fetchCount.getCount());
+ }
+
+ public void testGenericCacheWithSubcache() {
+ final Counter fetchCount = new SimpleCounter();
+ final FetcherCache c = new GenericFetcherCache();
+ Assert.assertEquals(0,fetchCount.getCount());
+ Assert.assertEquals(o,c.get(new Fetcher(){
+ public void fetch() {
+ c.set("testCache","testItem",o,"subCache");
+ fetchCount.increment();
+ }
+ },"testCache","testItem","subCache"));
+ Assert.assertEquals(1,fetchCount.getCount());
+ Assert.assertEquals(o,c.get(new Fetcher(){
+ public void fetch() {
+ c.set("testCache","testItem",o,"subCache");
+ fetchCount.increment();
+ }
+ },"testCache","testItem","subCache"));
+ Assert.assertEquals(1,fetchCount.getCount());
+ }
+
+ public void testGenericCacheWithDeepSubcache() {
+ final Counter fetchCount = new SimpleCounter();
+ final FetcherCache c = new GenericFetcherCache();
+ Assert.assertEquals(0,fetchCount.getCount());
+ Assert.assertEquals(o,c.get(new Fetcher(){
+ public void fetch() {
+ c.set("testCache","testItem",o,"subCache","subSubCache","subSubSubCache");
+ fetchCount.increment();
+ }
+ },"testCache","testItem","subCache","subSubCache","subSubSubCache"));
+ Assert.assertEquals(1,fetchCount.getCount());
+ Assert.assertEquals(o,c.get(new Fetcher(){
+ public void fetch() {
+ c.set("testCache","testItem",o,"subCache","subSubCache","subSubSubCache");
+ fetchCount.increment();
+ }
+ },"testCache","testItem","subCache","subSubCache","subSubSubCache"));
+ Assert.assertEquals(1,fetchCount.getCount());
+ }
+}