From a61f03cfbf62eb006e6bd845694f18d40101d4e8 Mon Sep 17 00:00:00 2001 From: alekseiplusplus Date: Sat, 29 Jun 2024 20:27:11 +1000 Subject: Added cache --- http-cache.tcl | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 http-cache.tcl (limited to 'http-cache.tcl') diff --git a/http-cache.tcl b/http-cache.tcl new file mode 100755 index 0000000..25e5f0d --- /dev/null +++ b/http-cache.tcl @@ -0,0 +1,35 @@ +namespace eval cache { + + # Dictionary containing filename and contents pairs. + variable cache {}; + + proc hit {filename} { + variable cache + return [dict exists $cache $filename] + } + + proc get {filename} { + variable cache + return [dict get $cache $filename] + } + + proc add {filename} { + variable cache + set file [open $filename] + fconfigure $file -translation binary + dict append cache $filename [read $file] + } + + proc remove {filename} { + variable cache + if [dict exists $cache $filename] { + set cache [dict remove $cache $filename] + } + } + + #TODO: Pre-cache all precache files. + foreach i $precache { + add [string cat $::http::root $i] + } + +} -- cgit v1.2.3