Add guava-based object cache 33/5733/4
authorRobert Varga <rovarga@cisco.com>
Mon, 24 Mar 2014 19:41:01 +0000 (20:41 +0100)
committerRobert Varga <rovarga@cisco.com>
Tue, 25 Mar 2014 05:28:02 +0000 (06:28 +0100)
This object cache is bound by the garbage collector, as it uses soft
references internally. It can be grown later to implement a caching
policies based on object type.

Change-Id: I16a5683bdc587dd5fe7a3c9c8e1e5a14cb85d09b
Signed-off-by: Robert Varga <rovarga@cisco.com>
common/object-cache-guava/pom.xml [new file with mode: 0644]
common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/guava/GuavaObjectCache.java [new file with mode: 0644]
common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/guava/GuavaObjectCacheFactory.java [new file with mode: 0644]
common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/guava/package-info.java [new file with mode: 0644]
common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/impl/StaticObjectCacheBinder.java [new file with mode: 0644]
common/pom.xml

diff --git a/common/object-cache-guava/pom.xml b/common/object-cache-guava/pom.xml
new file mode 100644 (file)
index 0000000..ee65f85
--- /dev/null
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- vi: set et smarttab sw=4 tabstop=4: -->
+<!--
+ Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+
+ This program and the accompanying materials are made available under the
+ terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ and is available at http://www.eclipse.org/legal/epl-v10.html
+-->
+<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/xsd/maven-4.0.0.xsd">
+
+    <parent>
+        <groupId>org.opendaylight.yangtools</groupId>
+        <artifactId>common-parent</artifactId>
+        <version>0.6.2-SNAPSHOT</version>
+    </parent>
+    <packaging>bundle</packaging>
+    <modelVersion>4.0.0</modelVersion>
+    <artifactId>object-cache-guava</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>object-cache-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+        </dependency>
+    </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/guava/GuavaObjectCache.java b/common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/guava/GuavaObjectCache.java
new file mode 100644 (file)
index 0000000..7d90e39
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.objcache.guava;
+
+import org.opendaylight.yangtools.objcache.spi.AbstractObjectCache;
+
+import com.google.common.base.FinalizableReferenceQueue;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheBuilderSpec;
+
+final class GuavaObjectCache extends AbstractObjectCache {
+       public GuavaObjectCache(final FinalizableReferenceQueue queue, final CacheBuilderSpec spec) {
+               super(CacheBuilder.from(spec).softValues().build(), queue);
+       }
+}
diff --git a/common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/guava/GuavaObjectCacheFactory.java b/common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/guava/GuavaObjectCacheFactory.java
new file mode 100644 (file)
index 0000000..067b626
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.objcache.guava;
+
+import org.opendaylight.yangtools.objcache.ObjectCache;
+import org.opendaylight.yangtools.objcache.spi.IObjectCacheFactory;
+
+import com.google.common.base.FinalizableReferenceQueue;
+
+public final class GuavaObjectCacheFactory implements IObjectCacheFactory {
+       private static final GuavaObjectCacheFactory INSTANCE = new GuavaObjectCacheFactory();
+       private final FinalizableReferenceQueue queue = new FinalizableReferenceQueue();
+       private final ObjectCache cache;
+
+       private GuavaObjectCacheFactory() {
+               // FIXME: make this more dynamic
+               this.cache = new GuavaObjectCache(queue, null);
+       }
+
+       @Override
+       public void finalize() throws Throwable {
+               try {
+                       queue.close();
+               } finally {
+                       super.finalize();
+               }
+       }
+
+       @Override
+       public ObjectCache getObjectCache(final Class<?> objClass) {
+               return cache;
+       }
+
+       public static GuavaObjectCacheFactory getInstance() {
+               return INSTANCE;
+       }
+}
diff --git a/common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/guava/package-info.java b/common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/guava/package-info.java
new file mode 100644 (file)
index 0000000..46b6029
--- /dev/null
@@ -0,0 +1,11 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+/**
+ *
+ */
+package org.opendaylight.yangtools.objcache.guava;
\ No newline at end of file
diff --git a/common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/impl/StaticObjectCacheBinder.java b/common/object-cache-guava/src/main/java/org/opendaylight/yangtools/objcache/impl/StaticObjectCacheBinder.java
new file mode 100644 (file)
index 0000000..5867d55
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.objcache.impl;
+
+import org.opendaylight.yangtools.objcache.guava.GuavaObjectCacheFactory;
+import org.opendaylight.yangtools.objcache.spi.AbstractObjectCacheBinder;
+
+public final class StaticObjectCacheBinder extends AbstractObjectCacheBinder {
+       private static final StaticObjectCacheBinder INSTANCE = new StaticObjectCacheBinder();
+
+       private StaticObjectCacheBinder() {
+               super(GuavaObjectCacheFactory.getInstance());
+       }
+
+       public static StaticObjectCacheBinder getInstance() {
+               return INSTANCE;
+       }
+}
index ccf50baebdf3fd12c20adccf6d748adebfab6ce1..6f1d440faaa507c89fcc86fb6b1affb8136f34bc 100644 (file)
@@ -23,6 +23,7 @@
         <module>concepts</module>
         <module>mockito-configuration</module>
         <module>object-cache-api</module>
+        <module>object-cache-guava</module>
         <module>object-cache-noop</module>
     </modules>
 
                 <artifactId>object-cache-api</artifactId>
                 <version>${project.version}</version>
             </dependency>
+            <dependency>
+                <groupId>${project.groupId}</groupId>
+                <artifactId>object-cache-guava</artifactId>
+                <version>${project.version}</version>
+            </dependency>
             <dependency>
                 <groupId>${project.groupId}</groupId>
                 <artifactId>object-cache-noop</artifactId>