From 4383578795c1b7f5f2446bc02cc3d2c9db577956 Mon Sep 17 00:00:00 2001 From: Unbit Date: Fri, 8 Mar 2013 10:04:13 +0100 Subject: [PATCH] prepare for magic cache functions --- core/cache.c | 67 +++++++++++++++++++++++++++++++-- plugins/python/uwsgi_pymodule.c | 30 +++++---------- uwsgi.h | 2 + 3 files changed, 76 insertions(+), 23 deletions(-) diff --git a/core/cache.c b/core/cache.c index ab7e88dd..2010abca 100644 --- a/core/cache.c +++ b/core/cache.c @@ -176,15 +176,28 @@ static void uwsgi_cache_load_files(struct uwsgi_cache *uc) { struct uwsgi_string_list *usl = uwsgi.load_file_in_cache; while(usl) { size_t len = 0; - char *value = uwsgi_open_and_read(usl->value, &len, 0, NULL); + char *value = NULL; + char *key = usl->value; + uint16_t key_len = usl->len; + char *space = strchr(usl->value, ' '); + if (space) { + // need to skip ? + if (uwsgi_strncmp(uc->name, uc->name_len, usl->value, space-usl->value)) { + goto next; + } + key = space+1; + key_len = usl->len - ((space-usl->value)+1); + } + value = uwsgi_open_and_read(key, &len, 0, NULL); if (value) { uwsgi_wlock(uc->lock); - if (!uwsgi_cache_set2(uc,usl->value, usl->len, value, len, 0, 0)) { - uwsgi_log("[cache] stored %s\n", usl->value); + if (!uwsgi_cache_set2(uc, key, key_len, value, len, 0, 0)) { + uwsgi_log("[cache] stored \"%.*s\" in \"%s\"\n", key_len, key, uc->name); } uwsgi_rwunlock(uc->lock); free(value); } +next: usl = usl->next; } } @@ -1058,6 +1071,7 @@ struct uwsgi_cache *uwsgi_cache_create(char *arg) { // default (old-stye) cache ? if (!arg) { uc->name = "default"; + uc->name_len = strlen(uc->name); uc->blocksize = uwsgi.cache_blocksize; if (!uc->blocksize) uc->blocksize = UMAX16; uc->max_items = uwsgi.cache_max_items; @@ -1127,6 +1141,7 @@ struct uwsgi_cache *uwsgi_cache_create(char *arg) { } uc->name = c_name; + uc->name_len = strlen(c_name); uc->max_items = uwsgi_n64(c_max_items); if (!uc->max_items) { uwsgi_log("you have to specify the maximum number of cache items\n"); @@ -1247,3 +1262,49 @@ char *uwsgi_cache_safe_get2(struct uwsgi_cache *uc, char *key, uint16_t keylen, return NULL; } + +/* + * uWSGI cache magic functions. They can be used by plugin to easily access local and remote caches + * + * they generate (when needed) a new memory buffer. Locking is automatically managed + * + * You have to free the returned memory !!! + * + */ +char *uwsgi_cache_magic_get(char *key, uint16_t keylen, uint64_t *vallen, char *cachename) { + struct uwsgi_cache *uc = NULL; + char *cache_server = NULL; + if (cachename) { + char *at = strchr(cachename, '@'); + if (!at) { + uc = uwsgi_cache_by_name(cachename); + } + else { + } + } + // use default (local) cache + else { + uc = uwsgi.caches; + } + + // we have a local cache !!! + if (uc) { + uwsgi_rlock(uc->lock); + char *value = uwsgi_cache_get2(uc, key, keylen, vallen); + if (!value) { + uwsgi_rwunlock(uc->lock); + return NULL; + } + char *buf = uwsgi_malloc(*vallen); + memcpy(buf, value, *vallen); + uwsgi_rwunlock(uc->lock); + return buf; + } + + // we have a remote one + if (cache_server) { + } + + return NULL; +} + diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index c5c2a596..ae390aeb 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -2983,31 +2983,21 @@ PyObject *py_uwsgi_queue_last(PyObject * self, PyObject * args) { PyObject *py_uwsgi_cache_get(PyObject * self, PyObject * args) { char *key; - uint64_t valsize; Py_ssize_t keylen = 0; - char *value = NULL; - PyObject *ret; + char *cache = NULL; - if (!PyArg_ParseTuple(args, "s#:cache_get", &key, &keylen)) { + if (!PyArg_ParseTuple(args, "s#|s:cache_get", &key, &keylen, &cache)) { return NULL; } - if (uwsgi.caches) { - UWSGI_RELEASE_GIL - uwsgi_rlock(uwsgi.caches->lock); - value = uwsgi_cache_get(key, keylen, &valsize); - if (!value) { - uwsgi_rwunlock(uwsgi.caches->lock); - UWSGI_GET_GIL - Py_INCREF(Py_None); - return Py_None; - } - char *storage = uwsgi_malloc(valsize); - memcpy(storage, value, valsize); - uwsgi_rwunlock(uwsgi.caches->lock); - UWSGI_GET_GIL - ret = PyString_FromStringAndSize(storage, valsize); - free(storage); + uint64_t vallen = 0; + UWSGI_RELEASE_GIL + char *value = uwsgi_cache_magic_get(key, keylen, &vallen, cache); + UWSGI_GET_GIL + if (value) { + // in python 3.x we return bytes + PyObject *ret = PyString_FromStringAndSize(value, vallen); + free(value); return ret; } diff --git a/uwsgi.h b/uwsgi.h index 1b05edbc..32d2a08b 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -3893,6 +3893,8 @@ struct uwsgi_stats_pusher_instance *uwsgi_stats_pusher_add(struct uwsgi_stats_pu int plugin_already_loaded(const char *); +char *uwsgi_cache_magic_get(char *, uint16_t, uint64_t *, char *); + #ifdef UWSGI_ZLIB #include int uwsgi_deflate_init(z_stream *, char *, size_t);