From f3b1acc55e76c8a26bc3b16c64da1924ce6f5b24 Mon Sep 17 00:00:00 2001 From: Unbit Date: Fri, 8 Mar 2013 16:26:16 +0100 Subject: [PATCH] completed cache magic update --- core/cache.c | 8 +++++++- plugins/cache/cache.c | 4 ++-- plugins/python/uwsgi_pymodule.c | 28 +++++++--------------------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/core/cache.c b/core/cache.c index 1da3e074..5e876176 100644 --- a/core/cache.c +++ b/core/cache.c @@ -1439,7 +1439,13 @@ int uwsgi_cache_magic_set(char *key, uint16_t keylen, char *value, uint64_t vall return -1; } - struct uwsgi_buffer *ub = uwsgi_cache_prepare_magic_set(cache_name, cache_name_len, key, keylen, vallen, expires); + struct uwsgi_buffer *ub = NULL; + if (flags & UWSGI_CACHE_FLAG_UPDATE) { + ub = uwsgi_cache_prepare_magic_update(cache_name, cache_name_len, key, keylen, vallen, expires); + } + else { + ub = uwsgi_cache_prepare_magic_set(cache_name, cache_name_len, key, keylen, vallen, expires); + } if (!ub) { close(fd); return -1; diff --git a/plugins/cache/cache.c b/plugins/cache/cache.c index db8bd4c6..28d3feb0 100644 --- a/plugins/cache/cache.c +++ b/plugins/cache/cache.c @@ -98,7 +98,7 @@ static void manage_magic_context(struct wsgi_request *wsgi_req, struct uwsgi_cac } // cache set - if (!uwsgi_strncmp(ucmc->cmd, ucmc->cmd_len, "set", 3)) { + if (!uwsgi_strncmp(ucmc->cmd, ucmc->cmd_len, "set", 3) || !uwsgi_strncmp(ucmc->cmd, ucmc->cmd_len, "update", 6)) { if (ucmc->size == 0 || ucmc->size > uc->max_item_size) return; wsgi_req->post_cl = ucmc->size; // read the value @@ -107,7 +107,7 @@ static void manage_magic_context(struct wsgi_request *wsgi_req, struct uwsgi_cac if (rlen != (ssize_t) ucmc->size) return; // ok let's lock uwsgi_wlock(uc->lock); - if (uwsgi_cache_set2(uc, ucmc->key, ucmc->key_len, value, ucmc->size, ucmc->expires, 0)) { + if (uwsgi_cache_set2(uc, ucmc->key, ucmc->key_len, value, ucmc->size, ucmc->expires, ucmc->cmd_len > 3 ? UWSGI_CACHE_FLAG_UPDATE : 0)) { uwsgi_rwunlock(uc->lock); return; } diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index 1969518c..ad427d25 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -2604,7 +2604,7 @@ PyObject *py_uwsgi_cache_set(PyObject * self, PyObject * args) { PyObject *py_uwsgi_cache_update(PyObject * self, PyObject * args) { - char *key; + char *key; char *value; Py_ssize_t vallen = 0; Py_ssize_t keylen = 0; @@ -2616,27 +2616,13 @@ PyObject *py_uwsgi_cache_update(PyObject * self, PyObject * args) { return NULL; } - if ((uint64_t)vallen > uwsgi.caches->blocksize) { - return PyErr_Format(PyExc_ValueError, "uWSGI cache items size must be < %llu, requested %llu bytes", (unsigned long long)uwsgi.caches->blocksize, (unsigned long long) vallen); - } - - if (remote && strlen(remote) > 0) { - UWSGI_RELEASE_GIL - //uwsgi_simple_send_string2(remote, 111, 1, key, keylen, value, vallen, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]); - UWSGI_GET_GIL - } - else if (uwsgi.caches) { - UWSGI_RELEASE_GIL - uwsgi_wlock(uwsgi.caches->lock); - if (uwsgi_cache_set(key, keylen, value, vallen, expires, UWSGI_CACHE_FLAG_UPDATE)) { - uwsgi_rwunlock(uwsgi.caches->lock); - UWSGI_GET_GIL - Py_INCREF(Py_None); - return Py_None; - } - uwsgi_rwunlock(uwsgi.caches->lock); - UWSGI_GET_GIL + UWSGI_RELEASE_GIL + if (uwsgi_cache_magic_set(key, keylen, value, vallen, expires, UWSGI_CACHE_FLAG_UPDATE, remote)) { + UWSGI_GET_GIL + Py_INCREF(Py_None); + return Py_None; } + UWSGI_GET_GIL Py_INCREF(Py_True); return Py_True;