diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-10-20 04:59:43 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-10-20 04:59:43 +0000 |
commit | 9558dae78cdbcf1909ab7649d2a02ab010c0d35c (patch) | |
tree | 0a4a36d89946011e1a993fc48819154a091ac2b9 /sca-cpp | |
parent | 073fcab1cf3045ff6f31f2b42cd33cb99c069e51 (diff) |
Renamed frontcache to datacache and add a memo cache that memoizes function applications.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1024519 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp')
-rw-r--r-- | sca-cpp/trunk/components/cache/Makefile.am | 16 | ||||
-rw-r--r-- | sca-cpp/trunk/components/cache/adder-test.scm | 20 | ||||
-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.cpp | 24 | ||||
-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.cpp | 77 | ||||
-rwxr-xr-x | sca-cpp/trunk/components/cache/server-test | 2 | ||||
-rw-r--r-- | sca-cpp/trunk/samples/store-cluster/shared/shared.composite | 2 | ||||
-rw-r--r-- | sca-cpp/trunk/samples/store-sql/store.composite | 2 |
9 files changed, 157 insertions, 24 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 diff --git a/sca-cpp/trunk/samples/store-cluster/shared/shared.composite b/sca-cpp/trunk/samples/store-cluster/shared/shared.composite index 714668af3a..b7eae5f78a 100644 --- a/sca-cpp/trunk/samples/store-cluster/shared/shared.composite +++ b/sca-cpp/trunk/samples/store-cluster/shared/shared.composite @@ -23,7 +23,7 @@ name="shared"> <component name="Cache"> - <implementation.cpp path="../../../components/cache" library="libfrontcache"/> + <implementation.cpp path="../../../components/cache" library="libdatacache"/> <service name="Cache"> <t:binding.atom uri="cache"/> </service> diff --git a/sca-cpp/trunk/samples/store-sql/store.composite b/sca-cpp/trunk/samples/store-sql/store.composite index 18a77c2802..a10740b5f9 100644 --- a/sca-cpp/trunk/samples/store-sql/store.composite +++ b/sca-cpp/trunk/samples/store-sql/store.composite @@ -60,7 +60,7 @@ </component> <component name="Cache"> - <implementation.cpp path="../../components/cache" library="libfrontcache"/> + <implementation.cpp path="../../components/cache" library="libdatacache"/> <service name="Cache"> <t:binding.atom uri="cache"/> </service> |