Branch: Tag:

2000-08-13

2000-08-13 13:54:22 by Per Hedbor <ph@opera.com>

Optimizations

Rev: server/base_server/configuration.pike:1.335
Rev: server/base_server/fastpipe.pike:1.6
Rev: server/config_interface/standard/sites/site_content.pike:1.75
Rev: server/protocols/http.pike:1.241

1:   // A vitual server's main configuration   // Copyright © 1996 - 2000, Roxen IS.    - constant cvs_version = "$Id: configuration.pike,v 1.334 2000/08/13 00:23:29 per Exp $"; + constant cvs_version = "$Id: configuration.pike,v 1.335 2000/08/13 13:54:21 per Exp $";   constant is_configuration = 1;   #include <module.h>   #include <module_constants.h>
2173:    registered_urls -= ({ url });    roxenp()->unregister_url( url );    } +  if( !datacache ) +  datacache = DataCache( ); +  else +  datacache->init_from_variables();   }      void save_me()
2942:    "\n\n", query_name(), (gethrtime()-start_time)/1000000.0);   }    +  + // Trivial cache (actually, it's more or less identical to the 200+ + // lines of C in HTTPLoop. But it does not have to bother with the + // fact that more than one thread can be active in it at once. Also, + // it does not have to delay free until all current connections using + // the cache entry is done...) + class DataCache + { +  mapping(string:array(string|mapping(string:mixed))) cache = ([]); +  +  int current_size; +  int max_size; +  int max_file_size; +  +  int hits, misses; +  +  static void clear_some_cache() +  { +  array q = indices( cache ); +  for( int i = 0; i<sizeof( cache)/10; i++ ) +  { +  string ent = q[random(sizeof(q))]; +  current_size -= strlen( cache[ent][0] ); +  m_delete( cache, q[random(sizeof(q))] ); +  } +  } +  +  void expire_entry( string url ) +  { +  if( cache[ url ] ) +  { +  current_size -= strlen(cache[url][0]); +  m_delete( cache, url ); +  } +  } +  +  void set( string url, string data, mapping meta, int expire ) +  { + // if( strlen( data ) > max_file_size ) // checked in conf + // return 0; +  call_out( expire_entry, expire, url ); +  while( (strlen( data ) + current_size) > max_size ) +  clear_some_cache(); +  current_size += strlen( data ); +  cache[url] = ({ data, meta }); +  } +  +  array(string|mapping(string:mixed)) get( string url ) +  { +  mixed res; +  if( res = cache[ url ] ) +  hits++; +  else +  misses++; +  return res; +  } +  +  void init_from_variables( ) +  { +  max_size = query( "data_cache_size" ) * 1024; +  max_file_size = query( "data_cache_file_max_size" ) * 1024; +  while( current_size > max_size ) +  clear_some_cache(); +  } +  +  static void create() +  { +  init_from_variables(); +  } + }; +  + DataCache datacache; +    void create(string config)   {    name=config;    -  +  // for now only theese two. In the future there might be more variables. +  defvar( "data_cache_size", 2048, DLOCALE("", "Data Cache:Cache size"), +  TYPE_INT, +  DLOCALE("", "The size of the data cache used to speed up requests " +  "for commonly requested files, in KBytes")); +  +  defvar( "data_cache_file_max_size", 50, DLOCALE("", "Data Cache:Max file size"), +  TYPE_INT, +  DLOCALE("", "The maximum size of a file that is to be considered for " +  "the cache")); +  +     defvar("default_server", 0, DLOCALE(20, "Default site"),    TYPE_FLAG,    DLOCALE(21, "If true, this site will be selected in preference of "