From 86fcd13dd9a012fe22ef98b187dd3eab15198bf6 Mon Sep 17 00:00:00 2001 From: "roberto@goyle" Date: Fri, 18 Feb 2011 19:31:58 +0100 Subject: [PATCH] structure for future response caching --- cache.c | 4 +++- uwsgi.h | 5 ++++- writers.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 writers.c diff --git a/cache.c b/cache.c index ac8858ec..6b012bec 100644 --- a/cache.c +++ b/cache.c @@ -57,6 +57,7 @@ char *uwsgi_cache_get(char *key, uint16_t keylen, uint64_t *valsize) { uint64_t index = uwsgi_cache_get_index(key, keylen); if (index) { + if (uwsgi.cache_items[index].flags & UWSGI_CACHE_FLAG_UNGETTABLE) return NULL; *valsize = uwsgi.cache_items[index].valsize; uwsgi.cache_items[index].hits++; return uwsgi.cache+(index*uwsgi.cache_blocksize); @@ -104,7 +105,7 @@ int uwsgi_cache_del(char *key, uint16_t keylen) { return ret; } -int uwsgi_cache_set(char *key, uint16_t keylen, char *val, uint64_t vallen, uint64_t expires) { +int uwsgi_cache_set(char *key, uint16_t keylen, char *val, uint64_t vallen, uint64_t expires, uint16_t flags) { uint64_t index = 0, last_index = 0 ; @@ -141,6 +142,7 @@ int uwsgi_cache_set(char *key, uint16_t keylen, char *val, uint64_t vallen, uint uci->expires = expires; uci->djbhash = djb33x_hash(key, keylen); uci->hits = 0; + uci->flags = flags; memcpy(uci->key, key, keylen); memcpy(uwsgi.cache+(index*uwsgi.cache_blocksize), val, vallen); diff --git a/uwsgi.h b/uwsgi.h index e915f424..52b37122 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -193,6 +193,9 @@ extern int pivot_root(const char * new_root, const char * put_old); #endif #define UWSGI_CACHE_MAX_KEY_SIZE 2048 +#define UWSGI_CACHE_FLAG_UNGETTABLE 0x0001 + +#define uwsgi_cache_update_start(x, y, z) uwsgi_cache_set(x, y, "", 0, CACHE_FLAG_UNGETTABLE) union uwsgi_sockaddr { struct sockaddr sa; @@ -242,7 +245,7 @@ struct uwsgi_queue_item { // maintain alignment here !!! struct uwsgi_cache_item { // unused - uint16_t unused; + uint16_t flags; // size of the key uint16_t keysize; // djb hash of the key diff --git a/writers.c b/writers.c new file mode 100644 index 00000000..db97282f --- /dev/null +++ b/writers.c @@ -0,0 +1,51 @@ +// uWSGI writer + +void simple_writer_init(struct wsgi_request *wsgi_req) {} +void simple_writer_end(struct wsgi_request *wsgi_req) {} + +ssize_t simple_writer(struct wsgi_request *wsgi_req, char *buf, size_t len) { + + return write(wsgi_req->poll.fd, buf, len); + +} + +void cache_writer_init(struct wsgi_request *wsgi_req) { + if (wsgi_req->cache_it) { + // lock cache write + if (uwsgi_cache_update_start(wsgi_req->uri, wsgi_req->uri_len)) { + uwsgi_log("unable to write data to uwsgi cache\n"); + wsgi_req->cache_it = 0; + } + } +} + +ssize_t cache_writer(struct wsgi_request *wsgi_req, char *buf, size_t len) { + + if (wsgi_req->cache_it) { + // lock cache write + if (uwsgi_cache_update(wsgi_req->uri, wsgi_req->uri_len, buf, len, wsgi_req->cache_expires)) { + uwsgi_log("unable to write data to uwsgi cache\n"); + uwsgi_cache_del(sgi_req->uri, wsgi_req->uri_len); + } + // unlock cache + } + return write(wsgi_req->poll.fd, buf, len); +} + +void cache_writer_end(struct wsgi_request *wsgi_req) { + + if (wsgi_req->cache_it) { + // finish cache update (lock write it) + uwsgi_cache_update_finalize(wsgi_req->uri, wsgi_req->uri_len); + } +} + +void memcached_writer_init(struct wsgi_request *wsgi_req) { + //open connection to memcached and create entry +} + +void memcached_writer_end(struct wsgi_request *wsgi_req) { + // close connection with memcached +} +ssize_t memcached_writer(struct wsgi_request *wsgi_req, char *buf, size_t len) { +}