From bc40569572bfad9045810e69062263119785849d Mon Sep 17 00:00:00 2001 From: lericson Date: Sat, 19 Sep 2009 22:45:42 +0200 Subject: [PATCH] Add memcached_clone support. --- _pylibmcmodule.c | 15 +++++++++++++++ _pylibmcmodule.h | 4 ++++ tests.py | 11 +++++++++++ 3 files changed, 30 insertions(+) diff --git a/_pylibmcmodule.c b/_pylibmcmodule.c index 142c33f..778c7e4 100644 --- a/_pylibmcmodule.c +++ b/_pylibmcmodule.c @@ -745,6 +745,21 @@ static PyObject *PylibMC_Client_disconnect_all(PylibMC_Client *self) { memcached_quit(self->mc); Py_RETURN_NONE; } + +static PyObject *PylibMC_Client_clone(PylibMC_Client *self) { + PylibMC_Client *new_self; + + /* XXX Is it really wise to short-circuit like this? I don't see a problem + * with it. */ + new_self = (PylibMC_Client *)self->ob_type->tp_new(self->ob_type, NULL, NULL); + if (new_self == NULL) { + return NULL; + } + memset(new_self->mc, 0, sizeof(memcached_st)); + new_self->mc = memcached_clone(new_self->mc, self->mc); + + return (PyObject *)new_self; +} /* }}} */ static PyObject *PylibMC_ErrFromMemcached(PylibMC_Client *self, const char *what, diff --git a/_pylibmcmodule.h b/_pylibmcmodule.h index 3b78eac..c79495a 100644 --- a/_pylibmcmodule.h +++ b/_pylibmcmodule.h @@ -149,6 +149,7 @@ static PyObject *PylibMC_Client_get_behaviors(PylibMC_Client *); static PyObject *PylibMC_Client_set_behaviors(PylibMC_Client *, PyObject *); static PyObject *PylibMC_Client_flush_all(PylibMC_Client *, PyObject *, PyObject *); static PyObject *PylibMC_Client_disconnect_all(PylibMC_Client *); +static PyObject *PylibMC_Client_clone(PylibMC_Client *); static PyObject *PylibMC_ErrFromMemcached(PylibMC_Client *, const char *, memcached_return); static PyObject *_PylibMC_Unpickle(const char *, size_t); @@ -191,6 +192,9 @@ static PyMethodDef PylibMC_ClientType_methods[] = { METH_VARARGS|METH_KEYWORDS, "Flush all data on all servers."}, {"disconnect_all", (PyCFunction)PylibMC_Client_disconnect_all, METH_NOARGS, "Disconnect from all servers and reset own state."}, + {"clone", (PyCFunction)PylibMC_Client_clone, METH_NOARGS, + "Clone this client entirely such that it is safe to access from " + "another thread. This creates a new connection."}, {NULL, NULL, 0, NULL} }; /* }}} */ diff --git a/tests.py b/tests.py index ec58432..8e92fa9 100644 --- a/tests.py +++ b/tests.py @@ -116,6 +116,17 @@ True >>> c.get("tengil").bar == bla.bar True +Cloning (ethically, I don't know about it.) +>>> c is not c.clone() +True +>>> c2 = c.clone() +>>> c.set("test", "hello") +True +>>> c2.get("test") +'hello' +>>> c2.delete("test") +True +>>> del c2 Behaviors. >>> c.set_behaviors({"tcp_nodelay": True, "hash": 6})