00001
00002 #ifndef _LIGO_WEBCACHE_H
00003 #define _LIGO_WEBCACHE_H
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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