From 0d60b001fbab5674807f2e0d752d96969b28d137 Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Thu, 28 Jul 2011 10:27:37 +0200 Subject: [PATCH] uwsgi.cache_update --- cache.c | 10 +++++++++ plugins/python/uwsgi_pymodule.c | 38 +++++++++++++++++++++++++++++++++ uwsgi.h | 1 + 3 files changed, 49 insertions(+) diff --git a/cache.c b/cache.c index 9ed94029..ee31aaa4 100644 --- a/cache.c +++ b/cache.c @@ -512,6 +512,16 @@ int uwsgi_cache_set(char *key, uint16_t keylen, char *val, uint64_t vallen, uint uci->prev = last_index; } } + else if (flags && UWSGI_CACHE_FLAG_UPDATE) { + uci = &uwsgi.cache_items[index] ; + if (expires) { + expires += time(NULL); + uci->expires = expires; + } + memcpy(uwsgi.cache+(index*uwsgi.cache_blocksize), val, vallen); + uci->valsize = vallen; + ret = 0; + } end: return ret; diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index fb7b5eab..d2a169f7 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -2671,6 +2671,43 @@ PyObject *py_uwsgi_cache_set(PyObject * self, PyObject * args) { } +PyObject *py_uwsgi_cache_update(PyObject * self, PyObject * args) { + + char *key; + char *value; + Py_ssize_t vallen = 0; + Py_ssize_t keylen = 0; + char *remote = NULL; + + uint64_t expires = 0; + + if (!PyArg_ParseTuple(args, "s#s#|is:cache_update", &key, &keylen, &value, &vallen, &expires, &remote)) { + return NULL; + } + + if ((uint64_t)vallen > uwsgi.cache_blocksize) { + return PyErr_Format(PyExc_ValueError, "uWSGI cache items size must be < %llu, requested %d bytes", (unsigned long long)uwsgi.cache_blocksize, (int) vallen); + } + + if (remote && strlen(remote) > 0) { + uwsgi_simple_send_string2(remote, 111, 1, key, keylen, value, vallen, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]); + } + else if (uwsgi.cache_max_items) { + uwsgi_wlock(uwsgi.cache_lock); + if (uwsgi_cache_set(key, keylen, value, vallen, expires, UWSGI_CACHE_FLAG_UPDATE)) { + uwsgi_rwunlock(uwsgi.cache_lock); + Py_INCREF(Py_None); + return Py_None; + } + uwsgi_rwunlock(uwsgi.cache_lock); + } + + Py_INCREF(Py_True); + return Py_True; + +} + + PyObject *py_uwsgi_cache_exists(PyObject * self, PyObject * args) { @@ -2904,6 +2941,7 @@ PyObject *py_uwsgi_cache_get(PyObject * self, PyObject * args) { static PyMethodDef uwsgi_cache_methods[] = { {"cache_get", py_uwsgi_cache_get, METH_VARARGS, ""}, {"cache_set", py_uwsgi_cache_set, METH_VARARGS, ""}, + {"cache_update", py_uwsgi_cache_update, METH_VARARGS, ""}, {"cache_del", py_uwsgi_cache_del, METH_VARARGS, ""}, {"cache_exists", py_uwsgi_cache_exists, METH_VARARGS, ""}, {NULL, NULL}, diff --git a/uwsgi.h b/uwsgi.h index 1570da20..be77099a 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -235,6 +235,7 @@ extern int pivot_root(const char *new_root, const char *put_old); #define UWSGI_CACHE_MAX_KEY_SIZE 2048 #define UWSGI_CACHE_FLAG_UNGETTABLE 0x0001 +#define UWSGI_CACHE_FLAG_UPDATE 0x0002 #define uwsgi_cache_update_start(x, y, z) uwsgi_cache_set(x, y, "", 0, CACHE_FLAG_UNGETTABLE)