Initial Commit.

This commit is contained in:
Michael Nitschinger
2012-12-14 09:33:00 +01:00
commit 2e8e00a2d5
7 changed files with 609 additions and 0 deletions

View File

@@ -0,0 +1,116 @@
/**
* Copyright (C) 2009-2012 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/
package com.couchbase.spring.cache;
import com.couchbase.client.CouchbaseClient;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
/**
* The CouchbaseCache class implements the common cache operations on top of
* a CouchbaseClient instance.
*/
public class CouchbaseCache implements Cache {
/**
* The actual CouchbaseClient instance.
*/
private final CouchbaseClient client;
/**
* The name of the cache.
*/
private final String name;
/**
* Construct the cache and pass in the CouchbaseClient instance.
*
* @param client the CouchbaseClient instance.
*/
public CouchbaseCache(String name, CouchbaseClient client) {
this.name = name;
this.client = client;
}
/**
* Returns the name of the cache.
*
* @return the name of the cache.
*/
public String getName() {
return name;
}
/**
* Returns the actual CouchbaseClient instance.
*
* @return the actual CouchbaseClient instance.
*/
public CouchbaseClient getNativeCache() {
return this.client;
}
/**
* Get an element from the cache.
*
* @param key the key to lookup against.
* @return the fetched value from Couchbase.
*/
public ValueWrapper get(Object key) {
String documentId = key.toString();
Object result = this.client.get(documentId);
return (result != null ? new SimpleValueWrapper(result) : null);
}
/**
* Store a object in Couchbase.
*
* @param key the Key of the storable object.
* @param value the Object to store.
*/
public void put(Object key, Object value) {
String documentId = key.toString();
this.client.set(documentId, 0, value);
}
/**
* Remove an object from Couchbase.
*
* @param key the Key of the object to delete.
*/
public void evict(Object key) {
String documentId = key.toString();
this.client.delete(documentId);
}
/**
* Clear the complete cache.
*
* Note that this action is very destructive, so only use it with care.
* Also note that "flush" may not be enabled on the bucket.
*/
public void clear() {
this.client.flush();
}
}

View File

@@ -0,0 +1,76 @@
/**
* Copyright (C) 2009-2012 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/
package com.couchbase.spring.cache;
import com.couchbase.client.CouchbaseClient;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import org.springframework.cache.Cache;
import org.springframework.cache.support.AbstractCacheManager;
/**
* The CouchbaseCacheManager handles CouchbaseClient connections and
* orchestrates them as needed.
*/
public class CouchbaseCacheManager extends AbstractCacheManager {
/**
* Holds the reference to all stored CouchbaseClient cache connections.
*/
private final HashMap<String, CouchbaseClient> clients;
/**
* Construct a new CouchabseCacheManager.
*/
public CouchbaseCacheManager(HashMap<String, CouchbaseClient> clients) {
this.clients = clients;
}
/**
* Returns a Map of all CouchbaseClients with name.
*
* @return the actual CouchbaseClient instances.
*/
public HashMap<String, CouchbaseClient> getClients() {
return this.clients;
}
/**
* Populates all caches.
*
* @return a collection of loaded caches.
*/
@Override
protected Collection<? extends Cache> loadCaches() {
Collection<Cache> caches = new LinkedHashSet<Cache>();
for(Map.Entry<String, CouchbaseClient> cache : this.clients.entrySet()) {
caches.add(new CouchbaseCache(cache.getKey(), cache.getValue()));
}
return caches;
}
}

View File

@@ -0,0 +1,73 @@
/**
* Copyright (C) 2009-2012 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/
package com.couchbase.spring.cache;
import com.couchbase.client.CouchbaseClient;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.cache.Cache;
/**
* Verifies the correct functionality of the CouchbaseCacheManager.
*/
public class CouchbaseCacheManagerTest {
/**
* Contains a reference to the actual CouchbaseClient.
*/
private static CouchbaseClient client;
/**
* Setup a CouchbaseClient before the tests run.
*
* @throws Exception
*/
@BeforeClass
public static void setupCouchbase() throws Exception {
ArrayList<URI> baseList = new ArrayList<URI>();
baseList.add(URI.create("http://127.0.0.1:8091/pools"));
String bucketName = "default";
String pwd = "";
client = new CouchbaseClient(baseList, bucketName, pwd);
}
/**
* Tests the main functionality of the manager: loading the caches.
*/
@Test
public void testCacheInit() {
HashMap<String, CouchbaseClient> instances =
new HashMap<String, CouchbaseClient>();
instances.put("test", client);
CouchbaseCacheManager manager = new CouchbaseCacheManager(instances);
assertEquals(instances, manager.getClients());
}
}

View File

@@ -0,0 +1,113 @@
/**
* Copyright (C) 2009-2012 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/
package com.couchbase.spring.cache;
import com.couchbase.client.CouchbaseClient;
import java.net.URI;
import java.util.ArrayList;
import static org.junit.Assert.*;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.cache.Cache.ValueWrapper;
/**
* Tests the CouchbaseCache class and verifies its functionality.
*/
public class CouchbaseCacheTest {
/**
* Contains a reference to the actual CouchbaseClient.
*/
private static CouchbaseClient client;
/**
* Simple name of the cache bucket to create.
*/
private String cacheName = "test";
/**
* Setup a CouchbaseClient before the tests run.
*
* @throws Exception
*/
@BeforeClass
public static void setupCouchbase() throws Exception {
ArrayList<URI> baseList = new ArrayList<URI>();
baseList.add(URI.create("http://127.0.0.1:8091/pools"));
String bucketName = "default";
String pwd = "";
client = new CouchbaseClient(baseList, bucketName, pwd);
}
/**
* Tests the basic Cache construction functionality.
*/
@Test
public void testConstruction() {
CouchbaseCache cache = new CouchbaseCache(cacheName, client);
assertEquals(cacheName, cache.getName());
assertEquals(client, cache.getNativeCache());
}
/**
* Verifies set() and get() of cache objects.
*/
@Test
public void testGetSet() {
CouchbaseCache cache = new CouchbaseCache(cacheName, client);
String key = "couchbase-cache-test";
String value = "Hello World!";
cache.put(key, value);
String stored = (String) client.get(key);
assertNotNull(stored);
assertEquals(value, stored);
ValueWrapper loaded = cache.get(key);
assertEquals(value, loaded.get());
}
/**
* Verifies the deletion of cache objects.
*
* @throws Exception
*/
@Test
public void testEvict() throws Exception {
CouchbaseCache cache = new CouchbaseCache(cacheName, client);
String key = "couchbase-cache-test";
String value = "Hello World!";
Boolean success = client.set(key, 0, value).get();
assertTrue(success);
cache.evict(key);
Object result = client.get(key);
assertNull(result);
}
}