Skip to content

Commit

Permalink
Write the cache key to the file.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralt committed Jul 29, 2016
1 parent 0d63d47 commit 9fbfff0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
4 changes: 4 additions & 0 deletions nginx.conf
Expand Up @@ -11,11 +11,15 @@ http {
server localhost:9999;
}

proxy_cache_path /home/ralt/src/c/ngx_tagpurge/build/nginx/cache levels=1:2 keys_zone=STATIC:10m;

server {
listen 8888;

location / {
proxy_pass http://app;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
}
}
}
24 changes: 13 additions & 11 deletions src/ngx_http_tagpurge_module.c
Expand Up @@ -3,6 +3,8 @@
#include <ngx_core.h>
#include <ngx_http.h>

#define log(...) ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, __VA_ARGS__);

ngx_module_t ngx_http_tagpurge_module;

typedef struct
Expand Down Expand Up @@ -92,14 +94,19 @@ ngx_http_tagpurge_filter(ngx_http_request_t *r)
/* For now, assume only one tag. */
ngx_str_t tag = upstream_header->value;

if (r->cache == NULL) {
/* Cache disabled. */
return ngx_http_next_header_filter(r);
}

ngx_file_t *file;
file = ngx_palloc(r->pool, sizeof(ngx_file_t));
if (file == NULL) {
return NGX_ERROR;
}

file->name.len = hmcf->cache_path->name.len + 1 +
hmcf->cache_path->len + 10;
hmcf->cache_path->len + tag.len;

file->name.data = ngx_palloc(r->pool, file->name.len + 1);
if (file->name.data == NULL) {
Expand All @@ -111,13 +118,8 @@ ngx_http_tagpurge_filter(ngx_http_request_t *r)
hmcf->cache_path->name.len);

(void) ngx_sprintf(file->name.data +
hmcf->cache_path->name.len + 1 +
hmcf->cache_path->len,
"%s", tag.data);

ngx_create_hashed_filename(hmcf->cache_path,
file->name.data,
file->name.len);
hmcf->cache_path->name.len,
"/%s", tag.data);

if (ngx_create_path(file, hmcf->cache_path) != NGX_OK) {
return NGX_ERROR;
Expand All @@ -132,9 +134,9 @@ ngx_http_tagpurge_filter(ngx_http_request_t *r)
goto end;
}

ssize_t n;
ssize_t n = 0;
for (;;) {
n = ngx_write_fd(file->fd, tag.data, tag.len);
n += ngx_write_fd(file->fd, r->cache->key, ngx_strlen(r->cache->key));
if (n == -1 ){
ngx_log_error(NGX_LOG_ALERT, r->connection->log,
ngx_errno,
Expand All @@ -143,7 +145,7 @@ ngx_http_tagpurge_filter(ngx_http_request_t *r)
goto close;
}

if ((size_t) n == tag.len) {
if ((size_t) n == ngx_strlen(r->cache->key)) {
break;
}
}
Expand Down
4 changes: 1 addition & 3 deletions tests/basic.lisp
Expand Up @@ -39,9 +39,7 @@
(format t "~A~%" headers)
;; Make sure we're getting our custom header
(is (drakma:header-value :cache-tag headers) "bar")
;; Make sure we're getting nginx custom tagpurge header
(is (drakma:header-value :X-foo headers)
"bar")))
(is-true (probe-file "build/tagpurge/bar"))))

(run! :basic)
(uiop:run-program "build/nginx/sbin/nginx -s stop")
Expand Down

0 comments on commit 9fbfff0

Please sign in to comment.