Initial Commit.
This commit is contained in:
116
src/main/java/com/couchbase/spring/cache/CouchbaseCache.java
vendored
Normal file
116
src/main/java/com/couchbase/spring/cache/CouchbaseCache.java
vendored
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
76
src/main/java/com/couchbase/spring/cache/CouchbaseCacheManager.java
vendored
Normal file
76
src/main/java/com/couchbase/spring/cache/CouchbaseCacheManager.java
vendored
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user