4 This plugin can cache readily rendered pages either as files or in the
5 ewiki database, so they will show up much faster when accessed a second
8 Right now it only supports storing the fully rendered page (_CACHE_FULL).
9 Storing of page plugins output (_CACHE_ALL) should be kept disabled,
10 because "UpdatedPages" and so on cannot be verified to not have updated
11 since the cache entry was written.
12 Also you may want to disallow caching of the "links" action, because
13 there is no change tracking with it.
16 #-- how and which pages to store
17 define("EWIKI_CACHE_FULL", 1); # including control links line?
18 define("EWIKI_CACHE_ALL", 0); # also virtual pages?
19 define("EWIKI_CACHE_VTIME", 1000); # time to keep page plugins` output, NYI
21 #-- where to store the pre-rendered pages (DB or files) - unset one of them:
22 define("EWIKI_CACHE_DIR", "./var/cache"); # preferred over db storage
23 define("EWIKI_CACHE_DB", "system/cache/"); # only has effect, if _DIR undefined
25 #-- when to store rendered pages?
26 $ewiki_config["cache.actions"] = array(
33 if (EWIKI_CACHE_FULL) {
34 $ewiki_plugins["handler"][] = "ewiki_handler_cache_full";
35 $ewiki_plugins["page_final"][] = "ewiki_store_cache_full";
38 die("unsupported ewiki caching guideline setting");
43 if (defined("EWIKI_CACHE_DIR") && !file_exists(EWIKI_CACHE_DIR)) {
44 @mkdir(dirname(EWIKI_CACHE_DIR));
45 mkdir(EWIKI_CACHE_DIR);
49 #-- fetch cache entry for page
50 function ewiki_get_cache($action, $id) {
52 if (defined("EWIKI_CACHE_DIR") && EWIKI_CACHE_DIR) {
53 $file = EWIKI_CACHE_DIR . "/" . $action . "," . urlencode($id);
54 if (file_exists($file)) {
55 $f = gzopen($file, "r");
57 $content = gzread($f, 1<<17);
63 "flags" => EWIKI_DB_F_BINARY¦EWIKI_DB_F_TEXT|EWIKI_DB_F_HTML,
64 "created" => filectime($file),
65 "lastmodified" => filemtime($file),
66 "content" => $content,
67 "meta" => array("class"=>"cache"),
73 elseif (defined("EWIKI_CACHE_DB") && (EWIKI_CACHE_DB)) {
74 $id = EWIKI_CACHE_DB."$action/$id";
75 $row = ewiki_db::GET($id);
81 #-- return rendered page
82 function ewiki_handler_cache_full($id, &$data, $action) {
84 if (in_array($action, $ewiki_config["cache.actions"]) && ($cache = ewiki_get_cache($action,$id))) {
85 if ($cache["lastmodified"] >= $data["lastmodified"]) {
87 ewiki_http_headers($data["content"], $id, $cache, $action);
88 return($data["content"]);
94 #-- if we get here, we should store the rendered page
95 function ewiki_store_cache_full(&$o, $id, &$data, $action) {
97 if (in_array($action, $ewiki_config["cache.actions"]) && ($data["version"] || EWIKI_CACHE_ALL) && ($_SERVER["REQUEST_METHOD"]=="GET")) {
99 #-- only store, if we got just a few QueryString parameters
100 if (count($_GET) <= 2) {
101 ewiki_put_cache($action, $id, $o);
107 #-- real save function
108 function ewiki_put_cache($action, $id, &$o) {
109 #-- save into cache dir
110 if (defined("EWIKI_CACHE_DIR") && EWIKI_CACHE_DIR) {
111 $file = EWIKI_CACHE_DIR . "/" . $action . "," . urlencode($id);
112 $f = gzopen($file, "w9");
118 #-- store in ewiki database
119 elseif (defined("EWIKI_CACHE_DB") && (EWIKI_CACHE_DB)) {
120 $id = EWIKI_CACHE_DB."$action/$id";
124 "flags" => EWIKI_DB_F_BINARY¦EWIKI_DB_F_TEXT|EWIKI_DB_F_HTML,
125 "created" => $data["lastmodified"],
126 "lastmodified" => time(),
128 "meta" => array("class"=>"cache"),
129 "author" => ewiki_author("ewiki_cache"),
131 ewiki_db::WRITE($save, true);