summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-06-15 20:07:10 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2011-06-15 20:07:10 +0900
commite3e7a420deacdf7e0d88d21d1a7b7d5431b33bf1 (patch)
tree4115e575973eb4be3f5463bea758d767763eb5d9 /src
parentd1885a58743f150fd1ec6979bbb4dfd05ec4900f (diff)
Cache and reuse RpcMethod objects.
Diffstat (limited to 'src')
-rw-r--r--src/RpcMethodFactory.cc34
-rw-r--r--src/RpcMethodFactory.h3
2 files changed, 35 insertions, 2 deletions
diff --git a/src/RpcMethodFactory.cc b/src/RpcMethodFactory.cc
index f2e48862..088f2ba9 100644
--- a/src/RpcMethodFactory.cc
+++ b/src/RpcMethodFactory.cc
@@ -41,8 +41,17 @@ namespace aria2 {
namespace rpc {
+namespace {
+SharedHandle<RpcMethod> getNoSuchMethod()
+{
+ static SharedHandle<RpcMethod> m(new NoSuchMethodRpcMethod());
+ return m;
+}
+} // namespace
+
+namespace {
SharedHandle<RpcMethod>
-RpcMethodFactory::create(const std::string& methodName)
+createMethod(const std::string& methodName)
{
if(methodName == AddUriRpcMethod::getMethodName()) {
return SharedHandle<RpcMethod>(new AddUriRpcMethod());
@@ -118,7 +127,28 @@ RpcMethodFactory::create(const std::string& methodName)
} else if(methodName == SystemMulticallRpcMethod::getMethodName()) {
return SharedHandle<RpcMethod>(new SystemMulticallRpcMethod());
} else {
- return SharedHandle<RpcMethod>(new NoSuchMethodRpcMethod());
+ return SharedHandle<RpcMethod>();
+ }
+}
+} // namespace
+
+std::map<std::string, SharedHandle<RpcMethod> > RpcMethodFactory::cache_;
+
+SharedHandle<RpcMethod>
+RpcMethodFactory::create(const std::string& methodName)
+{
+ std::map<std::string, SharedHandle<RpcMethod> >::const_iterator itr =
+ cache_.find(methodName);
+ if(itr == cache_.end()) {
+ SharedHandle<RpcMethod> m = createMethod(methodName);
+ if(m) {
+ cache_.insert(std::make_pair(methodName, m));
+ return m;
+ } else {
+ return getNoSuchMethod();
+ }
+ } else {
+ return (*itr).second;
}
}
diff --git a/src/RpcMethodFactory.h b/src/RpcMethodFactory.h
index 896c5caf..063502fc 100644
--- a/src/RpcMethodFactory.h
+++ b/src/RpcMethodFactory.h
@@ -38,6 +38,7 @@
#include "common.h"
#include <string>
+#include <map>
#include "SharedHandle.h"
@@ -50,6 +51,8 @@ class RpcMethod;
class RpcMethodFactory {
public:
static SharedHandle<RpcMethod> create(const std::string& methodName);
+private:
+ static std::map<std::string, SharedHandle<RpcMethod> > cache_;
};
} // namespace rpc