web/webcache.hh

00001 /* -*- mode: c++; c-basic-offset: 4; -*- */
00002 #ifndef _LIGO_WEBCACHE_H
00003 #define _LIGO_WEBCACHE_H
00004 /*----------------------------------------------------------------------*/
00005 /*                                                                      */
00006 /* Module Name: webcache                                                */
00007 /*                                                                      */
00008 /* Module Description: a cache for web servers                          */
00009 /*                                                                      */
00010 /* Revision History:                                                    */
00011 /* Rel   Date     Programmer    Comments                                */
00012 /* 0.1   4Jan02   D. Sigg       First release                           */
00013 /*                                                                      */
00014 /* Documentation References:                                            */
00015 /*      Man Pages: webcache.html                                        */
00016 /*      References: none                                                */
00017 /*                                                                      */
00018 /* Author Information:                                                  */
00019 /* Name          Telephone       Fax             e-mail                 */
00020 /* Daniel Sigg   (509) 372-8132  (509) 372-8137  sigg_d@ligo.mit.edu    */
00021 /*                                                                      */
00022 /*                                                                      */
00023 /*                      -------------------                             */
00024 /*                                                                      */
00025 /*                             LIGO                                     */
00026 /*                                                                      */
00027 /*        THE LASER INTERFEROMETER GRAVITATIONAL WAVE OBSERVATORY.      */
00028 /*                                                                      */
00029 /*                     (C) The LIGO Project, 1999.                      */
00030 /*                                                                      */
00031 /*                                                                      */
00032 /* Caltech                              MIT                             */
00033 /* LIGO Project MS 51-33                LIGO Project NW-17 161          */
00034 /* Pasadena CA 91125                    Cambridge MA 01239              */
00035 /*                                                                      */
00036 /* LIGO Hanford Observatory             LIGO Livingston Observatory     */
00037 /* P.O. Box 1970 S9-02                  19100 LIGO Lane Rd.             */
00038 /* Richland WA 99352                    Livingston, LA 70754            */
00039 /*                                                                      */
00040 /*----------------------------------------------------------------------*/
00041 
00042 #include "gmutex.hh"
00043 #include "Time.hh"
00044 #include <sys/types.h>
00045 #include <string>
00046 #include <map>
00047 #include <vector>
00048 #include <utility>
00049 
00050 namespace web {
00051 
00052 
00059    class webcache {
00060    public:
00062       class cachepage {
00063       public:
00065          typedef std::pair <std::string, std::string> header_entry;
00067          typedef std::vector <header_entry> header_type;
00068       
00070          cachepage();
00078          cachepage (const Time& exp, const char* d, int len,
00079                    const header_type& header);
00086          cachepage (const Time& exp, const std::string& s,
00087                    const header_type& header);
00089          cachepage (const cachepage& page);
00091          virtual ~cachepage();
00093          cachepage& operator= (const cachepage& page);
00099          void load (const char* d, int len);
00104          void load (const std::string& s);
00106          const Time& getTime() const {
00107             return fTime; }
00109          void setTime (Time& t) {
00110             fTime = t; }
00112          char* get() {
00113             return fData; }
00115          const char* get() const {
00116             return fData; }
00118          int size() const {
00119             return fLen; }
00121          header_type& header() {
00122             return fHeader; }
00124          const header_type& header() const {
00125             return fHeader; }
00127          void setHeader (const header_type& header) {
00128             fHeader = header; }
00130          bool allowCompress() const {
00131             return fAllowCompress; }
00133          void setAllowCompress (bool allow = true) {
00134             fAllowCompress = allow; }
00135       
00136       protected:
00138          Time           fTime;
00140          char*          fData;
00142          int            fLen;
00144          header_type    fHeader;
00146          bool           fAllowCompress;
00147       };
00148    
00150       struct urlcompare {
00152          bool operator() (const std::string& s1, const std::string& s2) const;
00153       };
00155       typedef std::map<std::string, cachepage, urlcompare> cachelist; 
00156    
00162       webcache (int maxsize = 1024*1024, bool autocleanup = false);
00164       virtual ~webcache();
00171       bool add (const char* url, const cachepage& page);
00178       bool lookup (const char* url, cachepage& page);
00184       void cleanup (const Time& now);
00190       bool enableAutoCleanup ();
00197       void setMax (int size);   
00202       int getMax() const {
00203          return fMaxSize; }     
00208       int size() const; 
00215       bool reduceCache (int size);
00216    
00217    protected:
00219       mutable thread::recursivemutex    fMux;
00221       int               fMaxSize;
00223       cachelist         fCache;
00225       pthread_t         fCleanupID;
00226    };
00227 
00228 
00229 }
00230 
00231 #endif // _LIGO_WEBCACHE_H

Generated on Sun Mar 8 19:20:52 2009 for dmt by  doxygen 1.5.4