2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2003 Net Integration Technologies, Inc.
5 * WvDiskCache uses GDBM hash to help keep track of the least recently used
6 * files in an on-disk cache of file with an enforced maximum size.
9 #ifndef __WVDISKCACHE_H
10 #define __WVDISKCACHE_H
12 #include <wvautoconf.h>
16 #include <wvcallback.h>
18 #include <wvgdbmhash.h>
24 DCFileInfo(size_t _size
, WvStringParm _next
, WvStringParm _prev
)
25 : size(_size
), next(_next
), prev(_prev
) {};
26 DCFileInfo(const DCFileInfo
&other
)
39 class DatumAdapter
<DCFileInfo
> : public DatumAdapterBase
42 typedef DCFileInfo ReturnType
;
43 operator DCFileInfo ()
44 { return convert(mydatum
); }
45 static ReturnType
convert(datum convertme
)
47 WvInPlaceBuf
buf(convertme
.dptr
, convertme
.dsize
, convertme
.dsize
);
49 fileinfo
.size
= atoi(buf
.getstr(buf
.strchr('\0')));
50 fileinfo
.next
= buf
.getstr(buf
.strchr('\0'));
51 fileinfo
.prev
= buf
.getstr(buf
.strchr('\0'));
54 DatumAdapter(DCFileInfo fileinfo
)
57 dynbuf
.putstr(fileinfo
.size
); dynbuf
.putch('\0');
58 dynbuf
.putstr(fileinfo
.next
); dynbuf
.putch('\0');
59 dynbuf
.putstr(fileinfo
.prev
); dynbuf
.putch('\0');
60 mydatum
.dsize
= dynbuf
.used();
61 mydatum
.dptr
= (char *)dynbuf
.get(dynbuf
.used());
63 DatumAdapter(const DatumAdapter
<DCFileInfo
> &other
)
65 // I don't know what's going on here....
66 // My theory is that gcc 3.x is crazy.
67 // Where is my implicit conversion to a base class?
68 dynbuf
.merge(*((WvDynBufBase
<unsigned char> *)&other
.dynbuf
),
76 typedef WvCallback
<void, WvStringParm
> DiskCacheCallback
;
81 WvDiskCache(WvStringParm hashpath
, int megabytes
);
82 void add(WvStringParm path
, size_t size
);
83 void remove(WvString path
);
84 void set_remove_callback(const DiskCacheCallback
&_callback
)
85 { callback
= _callback
; }
86 void set_max(int megabytes
);
87 int current_size() { return cur_size
/ 1024; };
88 int count() { return xcount
; };
91 size_t max_size
, cur_size
;
94 DiskCacheCallback callback
;
95 WvGdbmHash
<WvString
, DCFileInfo
> backend
;