This speeds up writing and reading the "metadata" by using a struct rather that all the calls to ap_cache_usec2hex

Note: I haven't actually tested this in mod_cache. Very similar code works very well elsewhere.

--
Brian Akins
Senior Systems Engineer
CNN Internet Technologies

--- httpd-2.0.50.old/modules/experimental/mod_disk_cache.c      2004-02-09 
15:53:16.000000000 -0500
+++ httpd-2.0.50/modules/experimental/mod_disk_cache.c  2004-08-04 08:47:28.000000000 
-0400
@@ -43,6 +43,14 @@
     apr_off_t file_size;     /*  File size of the cached data file  */    
 } disk_cache_object_t;
 
+typedef struct {
+    apr_time_t date;
+    apr_time_t expire;
+    apr_time_t version;
+    apr_time_t request_time;
+    apr_time_t response_time;
+} disk_cache_info_t;
+
 /*
  * mod_disk_cache configuration
  */
@@ -179,34 +187,21 @@
     int urllen = sizeof(urlbuff);
     int offset=0;
     char * temp;
+    disk_cache_info_t disk_info;
+    apr_size_t len;
 
-    /* read the data from the cache file */
-    /* format
-     * date SP expire SP count CRLF
-     * dates are stored as a hex representation of apr_time_t (number of
-     * microseconds since 00:00:00 january 1, 1970 UTC)
-     */
-    rv = apr_file_gets(&urlbuff[0], urllen, fd);
+   /* read the data from the cache file */
+    len = sizeof(disk_cache_info_t);
+    rv = apr_file_read(fd, &disk_info, &len);
     if (rv != APR_SUCCESS) {
         return rv;
     }
 
-    if ((temp = strchr(&urlbuff[0], '\n')) != NULL) /* trim off new line character */
-        *temp = '\0';      /* overlay it with the null terminator */
-
-    if (!apr_date_checkmask(urlbuff, "&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&& 
&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&")) {
-        return APR_EGENERAL;
-    }
-
-    info->date = ap_cache_hex2usec(urlbuff + offset);
-    offset += (sizeof(info->date)*2) + 1;
-    info->expire = ap_cache_hex2usec(urlbuff + offset);
-    offset += (sizeof(info->expire)*2) + 1;
-    dobj->version = ap_cache_hex2usec(urlbuff + offset);
-    offset += (sizeof(info->expire)*2) + 1;
-    info->request_time = ap_cache_hex2usec(urlbuff + offset);
-    offset += (sizeof(info->expire)*2) + 1;
-    info->response_time = ap_cache_hex2usec(urlbuff + offset);
+    info->date = disk_info.date;
+    info->expire = disk_info.expire;
+    dobj->version = disk_info.version;
+    info->request_time = disk_info.request_time;
+    info->response_time = disk_info.response_time;
     
     /* check that we have the same URL */
     rv = apr_file_gets(&urlbuff[0], urllen, fd);
@@ -217,11 +212,8 @@
     if ((temp = strchr(&urlbuff[0], '\n')) != NULL) { /* trim off new line character 
*/
         *temp = '\0';      /* overlay it with the null terminator */
     }
-
-    if (strncmp(urlbuff, "X-NAME: ", 7) != 0) {
-        return APR_EGENERAL;
-    }
-    if (strcmp(urlbuff + 8, dobj->name) != 0) {
+    
+    if (strcmp(urlbuff, dobj->name) != 0) {
         return APR_EGENERAL;
     }
     
@@ -232,41 +224,37 @@
 {
     apr_status_t rc;
     char *buf;
-    apr_size_t amt;
-
-    char       dateHexS[sizeof(apr_time_t) * 2 + 1];
-    char       expireHexS[sizeof(apr_time_t) * 2 + 1];
-    char       verHexS[sizeof(apr_time_t) * 2 + 1];
-    char       requestHexS[sizeof(apr_time_t) * 2 + 1];
-    char       responseHexS[sizeof(apr_time_t) * 2 + 1];
-    cache_info *info = &(h->cache_obj->info);
     disk_cache_object_t *dobj = (disk_cache_object_t *) h->cache_obj->vobj;
-    
+    disk_cache_info_t disk_info;
+    const char *newline = "\n";
+    struct iovec iov[3]; 
+ 
     if (!r->headers_out) {
         /* XXX log message */
         return 0;
     }
 
-    ap_cache_usec2hex(info->date, dateHexS);
-    ap_cache_usec2hex(info->expire, expireHexS);
-    ap_cache_usec2hex(dobj->version++, verHexS);
-    ap_cache_usec2hex(info->request_time, requestHexS);
-    ap_cache_usec2hex(info->response_time, responseHexS);
-    buf = apr_pstrcat(r->pool, dateHexS, " ", expireHexS, " ", verHexS, " ", 
requestHexS, " ", responseHexS, "\n", NULL);
-    amt = strlen(buf);
-    rc = apr_file_write(fd, buf, &amt);
-    if (rc != APR_SUCCESS) {
-        /* XXX log message */
+    disk_info.date = info->date;
+    disk_info.expire = info->expire;
+    disk_info.version = dobj->version++;
+    disk_info.request_time = info->request_time;
+    disk_info.response_time = info->response_time;
+
+
+    iov[0].iov_base = &disk_info;
+    iov[0].iov_len = sizeof(disk_cache_info_t);
+    iov[1].iov_base = dobj->name;
+    iov[1].iov_len = strlen(dobj->name);
+    iov[1].iov_base = newline;
+    iov[0].iov_len = sizeof(char); /*you never know*/
+
+    if ((rc =
+         apr_file_writev(fd, (const struct iovec *) &iov, 3,
+                         &len)) != APR_SUCCESS) {
         return 0;
+        
     }
 
-    buf = apr_pstrcat(r->pool, "X-NAME: ", dobj->name, "\n", NULL);
-    amt = strlen(buf);
-    rc = apr_file_write(fd, buf, &amt);
-    if (rc != APR_SUCCESS) {
-        /* XXX log message */
-        return 0;
-    }
     return 1;
 }
 

Reply via email to