Branch: Tag:

2006-12-07

2006-12-07 14:39:11 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Backported DataCache cache-cleaning optimizations from Roxen 4.6.

Rev: server/base_server/configuration.pike:1.625

5:   // @appears Configuration   //! A site's main configuration    - constant cvs_version = "$Id: configuration.pike,v 1.624 2006/11/07 08:37:14 wellhard Exp $"; + constant cvs_version = "$Id: configuration.pike,v 1.625 2006/12/07 14:39:11 grubba Exp $";   #include <module.h>   #include <module_constants.h>   #include <roxen.h>
219:   // the cache entry are done...)   class DataCache   { -  mapping(string: -  array(string|mapping(string:mixed))|string| -  function(string, RequestID:string)) cache = ([]); +  static typedef array(string|mapping(string:mixed))|string| +  function(string, RequestID:string) EntryType;    -  +  mapping(string:EntryType) cache = ([]); +     int current_size;    int max_size;    int max_file_size;
237:   #endif    }    -  static void low_expire_entry(string key_prefix, array(string) keys) +  // Expire a single entry. +  static void really_low_expire_entry(string key)    { -  if (!key_prefix) return; -  foreach(keys; int ind; string key) { +  EntryType e = m_delete(cache, key); +  if (arrayp(e)) { +  current_size -= sizeof(e[0]); +  } +  } +  +  // NOTE: Avoid using this function if possible! O(n) +  static int low_expire_entry(string key_prefix) +  { +  if (!key_prefix) return 0; +  if (arrayp(cache[key_prefix])) { +  // Leaf node. No need to loop. +  really_low_expire_entry(key_prefix); +  return 1; +  } +  // Inner node. Find all its children. +  int res = 0; +  foreach(indices(cache); int ind; string key) {    if (!key) continue;    if (has_prefix(key, key_prefix)) { -  if (arrayp(cache[key])) { -  current_size -= sizeof(cache[key][0]); +  really_low_expire_entry(key); +  res++;    } -  m_delete(cache, key); -  keys[ind] = 0; +     } -  +  return res;    } -  } +        void expire_entry(string key_prefix, RequestID|void id)    {    if (!id) { -  low_expire_entry(key_prefix, indices(cache)); +  low_expire_entry(key_prefix);    return;    }    string url = key_prefix;    sscanf(url, "%[^\0]", url);    while(1) { -  array(string|mapping(string:mixed))|string| -  function(string, RequestID:string) val; +  EntryType val;    if (arrayp(val = cache[key_prefix])) {    current_size -= sizeof(val[0]);    m_delete(cache, key_prefix);
286:    }    }    +  //! Clear ~1/10th of the cache.    static void clear_some_cache()    { -  // FIXME: Use random(cache) instead to avoid the indices() call. +     array(string) q = indices(cache);    if(!sizeof(q))    {    current_size=0;    return;    } -  for(int i = 0; i < sizeof(q)/10; i++) -  low_expire_entry(q[random(sizeof(q))], q); +  +  // The following code should be ~O(n * log(n)). +  sort(q); +  for(int i = 0; i < sizeof(q)/10; i++) { +  int r = random(sizeof(q)); +  string key_prefix = q[r = random(sizeof(q))]; +  if (!key_prefix) continue; +  for(;r < sizeof(q); r++,i++) { +  if (!q[r]) continue; +  if (!has_prefix(q[r], key_prefix)) break; +  really_low_expire_entry(q[r]); +  q[r] = 0;    } -  +  } +  }       void set(string url, string data, mapping meta, int expire, RequestID id)    {
311:    function(string, RequestID:string) old = cache[key];    if (old && (old != vary_cb)) {    // FIXME: Warn? -  expire_entry(key); +  low_expire_entry(key);    }    cache[key] = vary_cb;   
332:    function(string, RequestID:string) old = cache[key];    if (old) {    // FIXME: Warn? -  expire_entry(key); +  low_expire_entry(key);    }       current_size += strlen( data );
340:       // Only the actual cache entry is expired.    // FIXME: This could lead to lots and lots of call outs.. :P -  call_out(low_expire_entry, expire, key, ({ key })); +  call_out(really_low_expire_entry, expire, key);    int n;    while( (current_size > max_size) && (n++<10))    clear_some_cache();