summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/components/cache
diff options
context:
space:
mode:
Diffstat (limited to 'sca-cpp/trunk/components/cache')
-rw-r--r--sca-cpp/trunk/components/cache/Makefile.am16
-rw-r--r--sca-cpp/trunk/components/cache/adder-test.scm20
-rw-r--r--sca-cpp/trunk/components/cache/cache.composite (renamed from sca-cpp/trunk/components/cache/memcache.composite)24
-rw-r--r--sca-cpp/trunk/components/cache/client-test.cpp24
-rw-r--r--sca-cpp/trunk/components/cache/datacache.cpp (renamed from sca-cpp/trunk/components/cache/frontcache.cpp)14
-rw-r--r--sca-cpp/trunk/components/cache/memocache.cpp77
-rwxr-xr-xsca-cpp/trunk/components/cache/server-test2
7 files changed, 155 insertions, 22 deletions
diff --git a/sca-cpp/trunk/components/cache/Makefile.am b/sca-cpp/trunk/components/cache/Makefile.am
index 03e63f12ed..e6181ace05 100644
--- a/sca-cpp/trunk/components/cache/Makefile.am
+++ b/sca-cpp/trunk/components/cache/Makefile.am
@@ -25,18 +25,22 @@ comp_DATA = memcached.prefix
memcached.prefix: $(top_builddir)/config.status
echo ${MEMCACHED_PREFIX} >memcached.prefix
-EXTRA_DIST = memcache.composite
+EXTRA_DIST = cache.composite
-comp_LTLIBRARIES = libmemcache.la libfrontcache.la
-noinst_DATA = libmemcache.so libfrontcache.so
+comp_LTLIBRARIES = libmemcache.la libdatacache.la libmemocache.la
+noinst_DATA = libmemcache.so libdatacache.so libmemocache.so
libmemcache_la_SOURCES = memcache.cpp
libmemcache.so:
ln -s .libs/libmemcache.so
-libfrontcache_la_SOURCES = frontcache.cpp
-libfrontcache.so:
- ln -s .libs/libfrontcache.so
+libdatacache_la_SOURCES = datacache.cpp
+libdatacache.so:
+ ln -s .libs/libdatacache.so
+
+libmemocache_la_SOURCES = memocache.cpp
+libmemocache.so:
+ ln -s .libs/libmemocache.so
memcache_test_SOURCES = memcache-test.cpp
memcache_test_LDFLAGS = -lxml2
diff --git a/sca-cpp/trunk/components/cache/adder-test.scm b/sca-cpp/trunk/components/cache/adder-test.scm
new file mode 100644
index 0000000000..ccd5bc555f
--- /dev/null
+++ b/sca-cpp/trunk/components/cache/adder-test.scm
@@ -0,0 +1,20 @@
+; 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.
+
+; Logger test case
+
+(define (add a b) (+ a b))
diff --git a/sca-cpp/trunk/components/cache/memcache.composite b/sca-cpp/trunk/components/cache/cache.composite
index 4d546db410..1f3302614d 100644
--- a/sca-cpp/trunk/components/cache/memcache.composite
+++ b/sca-cpp/trunk/components/cache/cache.composite
@@ -38,10 +38,10 @@
<property name="servers">localhost:11411,localhost:11412,localhost:11413</property>
</component>
- <component name="frontcache">
- <implementation.cpp path="." library="libfrontcache"/>
- <service name="frontcache">
- <t:binding.http uri="frontcache"/>
+ <component name="datacache">
+ <implementation.cpp path="." library="libdatacache"/>
+ <service name="datacache">
+ <t:binding.http uri="datacache"/>
</service>
<reference name="l1reader" target="memcache"/>
<reference name="l1writer" target="memcache"/>
@@ -49,4 +49,20 @@
<reference name="l2writer" target="l2cache"/>
</component>
+ <component name="memocache">
+ <implementation.cpp path="." library="libmemocache"/>
+ <service name="memocache">
+ <t:binding.http uri="memocache"/>
+ </service>
+ <reference name="relay" target="adder"/>
+ <reference name="cache" target="memcache"/>
+ </component>
+
+ <component name="adder">
+ <implementation.scheme script="adder-test.scm"/>
+ <service name="adder">
+ <t:binding.http uri="adder"/>
+ </service>
+ </component>
+
</composite>
diff --git a/sca-cpp/trunk/components/cache/client-test.cpp b/sca-cpp/trunk/components/cache/client-test.cpp
index 4fb4cfe4e1..fce911a302 100644
--- a/sca-cpp/trunk/components/cache/client-test.cpp
+++ b/sca-cpp/trunk/components/cache/client-test.cpp
@@ -37,7 +37,8 @@ namespace tuscany {
namespace cache {
const string memcacheuri("http://localhost:8090/memcache");
-const string frontcacheuri("http://localhost:8090/frontcache");
+const string datacacheuri("http://localhost:8090/datacache");
+const string memocacheuri("http://localhost:8090/memocache");
bool testCache(const string& uri) {
http::CURLSession cs("", "", "");
@@ -89,8 +90,22 @@ bool testMemcache() {
return testCache(memcacheuri);
}
-bool testFrontcache() {
- return testCache(frontcacheuri);
+bool testDatacache() {
+ return testCache(datacacheuri);
+}
+
+bool testMemocache() {
+ http::CURLSession cs("", "", "");
+
+ const failable<value> res = http::evalExpr(mklist<value>(string("add"), 33, 22), memocacheuri, cs);
+ assert(hasContent(res));
+ assert((int)content(res) == 55);
+
+ const failable<value> res2 = http::evalExpr(mklist<value>(string("add"), 33, 22), memocacheuri, cs);
+ assert(hasContent(res2));
+ assert((int)content(res2) == 55);
+
+ return true;
}
struct getLoop {
@@ -131,7 +146,8 @@ int main() {
tuscany::cout << "Testing..." << tuscany::endl;
tuscany::cache::testMemcache();
- tuscany::cache::testFrontcache();
+ tuscany::cache::testDatacache();
+ tuscany::cache::testMemocache();
tuscany::cache::testGetPerf();
tuscany::cout << "OK" << tuscany::endl;
diff --git a/sca-cpp/trunk/components/cache/frontcache.cpp b/sca-cpp/trunk/components/cache/datacache.cpp
index a9b18f0792..e1cfdbfa57 100644
--- a/sca-cpp/trunk/components/cache/frontcache.cpp
+++ b/sca-cpp/trunk/components/cache/datacache.cpp
@@ -20,14 +20,14 @@
/* $Rev$ $Date$ */
/**
- * A front cache component implementation which coordinates access to two
+ * A data cache component implementation which coordinates access to two
* levels of backend read/write caches or stores. Each cache or store is
* accessed through two references: a writer reference and a reader reference.
*
* This is useful if your level2 store is made of a master and slave
* replicated databases, you can then wire the writer reference to the master
* database and the reader reference to one your slave databases (assuming
- * that the updates eventually get replicated to the slave database,in the
+ * that the updates eventually get replicated to the slave database, in the
* meantime the updates will be retrieved from the level1 cache).
*/
@@ -38,7 +38,7 @@
#include "monad.hpp"
namespace tuscany {
-namespace frontcache {
+namespace datacache {
/**
* Get an item from the cache.
@@ -112,13 +112,13 @@ extern "C" {
const tuscany::value apply(const tuscany::list<tuscany::value>& params) {
const tuscany::value func(car(params));
if (func == "get")
- return tuscany::frontcache::get(cadr(params), caddr(params), cadddr(params), caddddr(params), cadddddr(params));
+ return tuscany::datacache::get(cadr(params), caddr(params), cadddr(params), caddddr(params), cadddddr(params));
if (func == "post")
- return tuscany::frontcache::post(cadr(params), caddr(params), cadddr(params), caddddr(params), cadddddr(params), caddddddr(params));
+ return tuscany::datacache::post(cadr(params), caddr(params), cadddr(params), caddddr(params), cadddddr(params), caddddddr(params));
if (func == "put")
- return tuscany::frontcache::put(cadr(params), caddr(params), cadddr(params), caddddr(params), cadddddr(params), caddddddr(params));
+ return tuscany::datacache::put(cadr(params), caddr(params), cadddr(params), caddddr(params), cadddddr(params), caddddddr(params));
if (func == "delete")
- return tuscany::frontcache::del(cadr(params), caddr(params), cadddr(params), caddddr(params), cadddddr(params));
+ return tuscany::datacache::del(cadr(params), caddr(params), cadddr(params), caddddr(params), cadddddr(params));
return tuscany::mkfailure<tuscany::value>();
}
diff --git a/sca-cpp/trunk/components/cache/memocache.cpp b/sca-cpp/trunk/components/cache/memocache.cpp
new file mode 100644
index 0000000000..2482c2202b
--- /dev/null
+++ b/sca-cpp/trunk/components/cache/memocache.cpp
@@ -0,0 +1,77 @@
+/*
+ * 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.
+ */
+
+/* $Rev$ $Date$ */
+
+/**
+ * A cache component implementation which memoizes the value of function
+ * applications, keyed by the function arguments, in a key/value cache passed
+ * as a reference.
+ *
+ * This is useful if your functions are idempotent and applied many times to
+ * the same arguments. The results can then be retrieved quickly from the
+ * cache without actually applying the function.
+ */
+
+#include "string.hpp"
+#include "function.hpp"
+#include "list.hpp"
+#include "value.hpp"
+#include "monad.hpp"
+
+namespace tuscany {
+namespace memocache {
+
+/**
+ * Memoize the value of a function application in a cache.
+ */
+const failable<value> memoize(const list<value>& params, const lambda<value(const list<value>&)> relay, const lambda<value(const list<value>&)> cache) {
+ debug(params, "memocache::memoize::params");
+
+ // Lookup memoized value from cache
+ const value val = cache(mklist<value>("get", params));
+ if (!isNil(val)) {
+ debug(val, "memocache::memoize::cached");
+ return val;
+ }
+
+ // Apply the given function
+ const value res = relay(params);
+ debug(res, "memocache::memoize::res");
+
+ // Store the result value in the cache
+ cache(mklist<value>("put", params, res));
+
+ return res;
+}
+
+}
+}
+
+extern "C" {
+
+const tuscany::value apply(const tuscany::list<tuscany::value>& params) {
+ const tuscany::value func(car(params));
+ if (func == "start" || func == "stop")
+ return tuscany::mkfailure<tuscany::value>();
+ const tuscany::list<tuscany::value> rev = tuscany::reverse(params);
+ return tuscany::memocache::memoize(tuscany::reverse(tuscany::cddr(rev)), tuscany::cadr(rev), tuscany::car(rev));
+}
+
+}
diff --git a/sca-cpp/trunk/components/cache/server-test b/sca-cpp/trunk/components/cache/server-test
index a93a7e73ac..6d14f789f5 100755
--- a/sca-cpp/trunk/components/cache/server-test
+++ b/sca-cpp/trunk/components/cache/server-test
@@ -23,7 +23,7 @@
../../modules/server/scheme-conf tmp
cat >>tmp/conf/httpd.conf <<EOF
SCAContribution `pwd`/
-SCAComposite memcache.composite
+SCAComposite cache.composite
EOF
./memcached-start 11211