[PATCH] Cache: directio support when reading cache files

Maxim Dounin mdounin at mdounin.ru
Thu May 15 02:49:33 UTC 2025


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1747277255 -10800
#      Thu May 15 05:47:35 2025 +0300
# Node ID 579fd22909a485c48d0780e764c02af0c44c3666
# Parent  abc30f01c381567949fcb62c8e3ce04f47cf7198
Cache: directio support when reading cache files.

diff --git a/src/http/ngx_http_file_cache.c b/src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c
+++ b/src/http/ngx_http_file_cache.c
@@ -353,7 +353,7 @@ ngx_http_file_cache_open(ngx_http_reques
     of.valid = clcf->open_file_cache_valid;
     of.min_uses = clcf->open_file_cache_min_uses;
     of.events = clcf->open_file_cache_events;
-    of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
+    of.directio = clcf->directio;
     of.read_ahead = clcf->read_ahead;
 
     if (ngx_open_cached_file(clcf->open_file_cache, &c->file.name, &of, r->pool)
@@ -380,13 +380,36 @@ ngx_http_file_cache_open(ngx_http_reques
 
     c->file.fd = of.fd;
     c->file.log = r->connection->log;
+    c->file.directio = of.is_directio;
     c->uniq = of.uniq;
     c->length = of.size;
     c->fs_size = (of.fs_size + cache->bsize - 1) / cache->bsize;
 
-    c->buf = ngx_create_temp_buf(r->pool, c->body_start);
-    if (c->buf == NULL) {
-        return NGX_ERROR;
+    if (of.is_directio) {
+
+        c->body_start = ngx_align(c->body_start, clcf->directio_alignment);
+
+        c->buf = ngx_calloc_buf(r->pool);
+        if (c->buf == NULL) {
+            return NGX_ERROR;
+        }
+
+        c->buf->start = ngx_pmemalign(r->pool, c->body_start,
+                                      clcf->directio_alignment);
+        if (c->buf->start == NULL) {
+            return NGX_ERROR;
+        }
+
+        c->buf->pos = c->buf->start;
+        c->buf->last = c->buf->start;
+        c->buf->end = c->buf->start + c->body_start;
+        c->buf->temporary = 1;
+
+    } else {
+        c->buf = ngx_create_temp_buf(r->pool, c->body_start);
+        if (c->buf == NULL) {
+            return NGX_ERROR;
+        }
     }
 
     return ngx_http_file_cache_read(r, c);
@@ -1663,6 +1686,7 @@ ngx_http_cache_send(ngx_http_request_t *
     b->file->fd = c->file.fd;
     b->file->name = c->file.name;
     b->file->log = r->connection->log;
+    b->file->directio = c->file.directio;
 
     out.buf = b;
     out.next = NULL;



More information about the nginx-devel mailing list