summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/components/cache
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sca-cpp/trunk/components/cache/client-test.cpp27
-rw-r--r--sca-cpp/trunk/components/cache/mcache-test.cpp27
-rw-r--r--sca-cpp/trunk/components/cache/mcache.cpp62
-rw-r--r--sca-cpp/trunk/components/cache/mcache.hpp16
-rwxr-xr-xsca-cpp/trunk/components/cache/memcached-start21
-rwxr-xr-xsca-cpp/trunk/components/cache/memcached-stop23
-rwxr-xr-xsca-cpp/trunk/components/cache/memcached-test7
-rwxr-xr-xsca-cpp/trunk/components/cache/server-test8
8 files changed, 135 insertions, 56 deletions
diff --git a/sca-cpp/trunk/components/cache/client-test.cpp b/sca-cpp/trunk/components/cache/client-test.cpp
index c8b23b391e..ddf093a6dc 100644
--- a/sca-cpp/trunk/components/cache/client-test.cpp
+++ b/sca-cpp/trunk/components/cache/client-test.cpp
@@ -36,7 +36,7 @@
namespace tuscany {
namespace cache {
-const string url("http://localhost:8090/mcache");
+const string uri("http://localhost:8090/mcache");
bool testCache() {
http::CURLSession cs;
@@ -46,10 +46,12 @@ bool testCache() {
+ (list<value>() + "price" + string("$2.99"));
const list<value> a = mklist<value>(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i);
- const failable<value> id = http::post(a, url, cs);
+ const failable<value> id = http::post(a, uri, cs);
assert(hasContent(id));
+
+ const string p = path(content(id));
{
- const failable<value> val = http::get(url + "/" + content(id), cs);
+ const failable<value> val = http::get(uri + p, cs);
assert(hasContent(val));
assert(content(val) == a);
}
@@ -60,22 +62,22 @@ bool testCache() {
const list<value> b = mklist<value>(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), j);
{
- const failable<value> r = http::put(b, url + "/" + content(id), cs);
+ const failable<value> r = http::put(b, uri + p, cs);
assert(hasContent(r));
assert(content(r) == value(true));
}
{
- const failable<value> val = http::get(url + "/" + content(id), cs);
+ const failable<value> val = http::get(uri + p, cs);
assert(hasContent(val));
assert(content(val) == b);
}
{
- const failable<value> r = http::del(url + "/" + content(id), cs);
+ const failable<value> r = http::del(uri + p, cs);
assert(hasContent(r));
assert(content(r) == value(true));
}
{
- const failable<value> val = http::get(url + "/" + content(id), cs);
+ const failable<value> val = http::get(uri + p, cs);
assert(!hasContent(val));
}
@@ -83,13 +85,13 @@ bool testCache() {
}
struct getLoop {
- const value id;
+ const string path;
const value entry;
http::CURLSession cs;
- getLoop(const value& id, const value& entry, http::CURLSession cs) : id(id), entry(entry), cs(cs) {
+ getLoop(const string& path, const value& entry, http::CURLSession cs) : path(path), entry(entry), cs(cs) {
}
const bool operator()() const {
- const failable<value> val = http::get(url + "/" + id, cs);
+ const failable<value> val = http::get(uri + path, cs);
assert(hasContent(val));
assert(content(val) == entry);
return true;
@@ -103,10 +105,11 @@ bool testGetPerf() {
const value a = mklist<value>(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i);
http::CURLSession cs;
- const failable<value> id = http::post(a, url, cs);
+ const failable<value> id = http::post(a, uri, cs);
assert(hasContent(id));
+ const string p = path(content(id));
- const lambda<bool()> gl = getLoop(content(id), a, cs);
+ const lambda<bool()> gl = getLoop(p, a, cs);
cout << "Cache get test " << time(gl, 5, 200) << " ms" << endl;
return true;
diff --git a/sca-cpp/trunk/components/cache/mcache-test.cpp b/sca-cpp/trunk/components/cache/mcache-test.cpp
index 90f17ffd48..316372c5be 100644
--- a/sca-cpp/trunk/components/cache/mcache-test.cpp
+++ b/sca-cpp/trunk/components/cache/mcache-test.cpp
@@ -33,33 +33,36 @@ namespace tuscany {
namespace cache {
bool testMemCached() {
- MemCached ch;
+ MemCached ch("127.0.0.1", 11211);
+ const value k = mklist<value>("a");
- assert(hasContent(post("a", string("AAA"), ch)));
- assert((get("a", ch)) == value(string("AAA")));
- assert(hasContent(put("a", string("aaa"), ch)));
- assert((get("a", ch)) == value(string("aaa")));
- assert(hasContent(del("a", ch)));
- assert(!hasContent(get("a", ch)));
+ assert(hasContent(post(k, string("AAA"), ch)));
+ assert((get(k, ch)) == value(string("AAA")));
+ assert(hasContent(put(k, string("aaa"), ch)));
+ assert((get(k, ch)) == value(string("aaa")));
+ assert(hasContent(del(k, ch)));
+ assert(!hasContent(get(k, ch)));
return true;
}
struct getLoop {
+ const value k;
MemCached& ch;
- getLoop(MemCached& ch) : ch(ch) {
+ getLoop(const value& k, MemCached& ch) : k(k), ch(ch) {
}
const bool operator()() const {
- assert((get("c", ch)) == value(string("CCC")));
+ assert((get(k, ch)) == value(string("CCC")));
return true;
}
};
bool testGetPerf() {
- MemCached ch;
- assert(hasContent(post("c", string("CCC"), ch)));
+ const value k = mklist<value>("c");
+ MemCached ch("127.0.0.1", 11211);
+ assert(hasContent(post(k, string("CCC"), ch)));
- const lambda<bool()> gl = getLoop(ch);
+ const lambda<bool()> gl = getLoop(k, ch);
cout << "Memcached get test " << time(gl, 5, 200) << " ms" << endl;
return true;
}
diff --git a/sca-cpp/trunk/components/cache/mcache.cpp b/sca-cpp/trunk/components/cache/mcache.cpp
index b1dc750ccb..50b642106f 100644
--- a/sca-cpp/trunk/components/cache/mcache.cpp
+++ b/sca-cpp/trunk/components/cache/mcache.cpp
@@ -34,14 +34,12 @@
#include "mcache.hpp"
namespace tuscany {
-namespace cache {
-
-cache::MemCached ch;
+namespace mcache {
/**
* Get an item from the cache.
*/
-const failable<value> get(const list<value>& params) {
+const failable<value> get(const list<value>& params, cache::MemCached& ch) {
return cache::get(car(params), ch);
}
@@ -56,9 +54,9 @@ const value uuidValue() {
return value(string(buf, APR_UUID_FORMATTED_LENGTH));
}
-const failable<value> post(const list<value>& params) {
- const value id = uuidValue();
- const failable<bool> val = cache::post(id, car(params), ch);
+const failable<value> post(const list<value>& params, cache::MemCached& ch) {
+ const value id = append<value>(car(params), mklist(uuidValue()));
+ const failable<bool> val = cache::post(id, cadr(params), ch);
if (!hasContent(val))
return mkfailure<value>(reason(val));
return id;
@@ -67,7 +65,7 @@ const failable<value> post(const list<value>& params) {
/**
* Put an item into the cache.
*/
-const failable<value> put(const list<value>& params) {
+const failable<value> put(const list<value>& params, cache::MemCached& ch) {
const failable<bool> val = cache::put(car(params), cadr(params), ch);
if (!hasContent(val))
return mkfailure<value>(reason(val));
@@ -77,13 +75,49 @@ const failable<value> put(const list<value>& params) {
/**
* Delete an item from the cache.
*/
-const failable<value> del(const list<value>& params) {
+const failable<value> del(const list<value>& params, cache::MemCached& ch) {
const failable<bool> val = cache::del(car(params), ch);
if (!hasContent(val))
return mkfailure<value>(reason(val));
return value(content(val));
}
+/**
+ * Component implementation lambda function.
+ */
+class applyCache {
+public:
+ applyCache(cache::MemCached& ch) : ch(ch) {
+ }
+
+ const value operator()(const list<value>& params) const {
+ const value func(car(params));
+ if (func == "get")
+ return get(cdr(params), ch);
+ if (func == "post")
+ return post(cdr(params), ch);
+ if (func == "put")
+ return put(cdr(params), ch);
+ if (func == "delete")
+ return del(cdr(params), ch);
+ return tuscany::mkfailure<tuscany::value>();
+ }
+
+private:
+ cache::MemCached& ch;
+};
+
+/**
+ * Start the component.
+ */
+const failable<value> start(unused const list<value>& params) {
+ // Connect to memcached
+ cache::MemCached& ch = *(new (gc_new<cache::MemCached>()) cache::MemCached("127.0.0.1", 11211));
+
+ // Return the component implementation lambda function
+ return value(lambda<value(const list<value>&)>(applyCache(ch)));
+}
+
}
}
@@ -91,14 +125,8 @@ extern "C" {
const tuscany::value apply(const tuscany::list<tuscany::value>& params) {
const tuscany::value func(car(params));
- if (func == "get")
- return tuscany::cache::get(cdr(params));
- if (func == "post")
- return tuscany::cache::post(cdr(params));
- if (func == "put")
- return tuscany::cache::put(cdr(params));
- if (func == "delete")
- return tuscany::cache::del(cdr(params));
+ if (func == "start" || func == "restart")
+ return tuscany::mcache::start(cdr(params));
return tuscany::mkfailure<tuscany::value>();
}
diff --git a/sca-cpp/trunk/components/cache/mcache.hpp b/sca-cpp/trunk/components/cache/mcache.hpp
index 9659c11788..4751975099 100644
--- a/sca-cpp/trunk/components/cache/mcache.hpp
+++ b/sca-cpp/trunk/components/cache/mcache.hpp
@@ -48,23 +48,28 @@ namespace cache {
*/
class MemCached {
public:
- MemCached() {
- apr_pool_create(&pool, NULL);
- apr_memcache_create(pool, 1, 0, &mc);
- init("localhost", 11211);
+ MemCached() : owner(false) {
}
- MemCached(const string host, const int port) {
+ MemCached(const string host, const int port) : owner(true) {
apr_pool_create(&pool, NULL);
apr_memcache_create(pool, 1, 0, &mc);
init(host, port);
}
+ MemCached(const MemCached& c) : owner(false) {
+ pool = c.pool;
+ mc = c.mc;
+ }
+
~MemCached() {
+ if (!owner)
+ return;
apr_pool_destroy(pool);
}
private:
+ bool owner;
apr_pool_t* pool;
apr_memcache_t* mc;
@@ -86,7 +91,6 @@ private:
return mkfailure<bool>("Could not add server");
return true;
}
-
};
/**
diff --git a/sca-cpp/trunk/components/cache/memcached-start b/sca-cpp/trunk/components/cache/memcached-start
new file mode 100755
index 0000000000..cd27faf046
--- /dev/null
+++ b/sca-cpp/trunk/components/cache/memcached-start
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+# 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.
+
+# Start memcached
+memcached -l 127.0.0.1 -m 4 -p 11211 &
diff --git a/sca-cpp/trunk/components/cache/memcached-stop b/sca-cpp/trunk/components/cache/memcached-stop
new file mode 100755
index 0000000000..b999228b46
--- /dev/null
+++ b/sca-cpp/trunk/components/cache/memcached-stop
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+# 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.
+
+# Stop memcached
+mc="memcached -l 127.0.0.1 -m 4 -p 11211"
+
+kill `ps -f | grep -v grep | grep "${mc}" | awk '{ print $2 }'`
diff --git a/sca-cpp/trunk/components/cache/memcached-test b/sca-cpp/trunk/components/cache/memcached-test
index df21d32a57..d4b9c04eda 100755
--- a/sca-cpp/trunk/components/cache/memcached-test
+++ b/sca-cpp/trunk/components/cache/memcached-test
@@ -18,14 +18,13 @@
# under the License.
# Setup
-mc="memcached -l 127.0.0.1 -m 4 -p 11211"
-$mc &
+./memcached-start
sleep 1
# Test
-./mcache-test
+./mcache-test 2>/dev/null
rc=$?
# Cleanup
-kill `ps -f | grep -v grep | grep "$mc" | awk '{ print $2 }'`
+./memcached-stop
return $rc
diff --git a/sca-cpp/trunk/components/cache/server-test b/sca-cpp/trunk/components/cache/server-test
index 821724295d..4942f547bc 100755
--- a/sca-cpp/trunk/components/cache/server-test
+++ b/sca-cpp/trunk/components/cache/server-test
@@ -26,18 +26,16 @@ SCAContribution `pwd`/
SCAComposite mcache.composite
EOF
+./memcached-start
../../modules/http/httpd-start tmp
-
-mc="memcached -l 127.0.0.1 -m 4 -p 11211"
-$mc &
sleep 2
# Test
-./client-test
+./client-test 2>/dev/null
rc=$?
# Cleanup
-kill `ps -f | grep -v grep | grep "$mc" | awk '{ print $2 }'`
../../modules/http/httpd-stop tmp
+./memcached-stop
sleep 2
return $rc