Add memcached_clone support.

This commit is contained in:
lericson 2009-09-19 22:45:42 +02:00
parent e8817b6617
commit bc40569572
3 changed files with 30 additions and 0 deletions

View File

@ -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,

View File

@ -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}
};
/* }}} */

View File

@ -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})