Permalink
Browse files

Add methods to cache full content tree

  • Loading branch information...
1 parent 27a5b3f commit 0a37683d8a134f9421901f6d88d8d8c6ef0603d2 @kolber committed Apr 4, 2013
Showing with 52 additions and 0 deletions.
  1. +52 −0 app/cache.inc.php
View
@@ -36,6 +36,58 @@ function delete_old_caches() {
foreach($old_caches as $file) unlink($file);
}
+ function get_full_cache() {
+ $htaccess = file_exists('./.htaccess') ? '.htaccess:'.filemtime('./.htaccess') : '';
+ $file_cache = serialize(Helpers::file_cache());
+ $content_hash = self::generate_hash($htaccess.$file_cache);
+
+ if (file_exists('./app/_cache/pages/all-content-'.$content_hash)) {
+ return file_get_contents('./app/_cache/pages/all-content-'.$content_hash);
+ } else {
+ # delete old full cache files
+ self::delete_old_full_caches();
+ # save a new full cache file
+ self::save_full_cache(self::create_full_cache(), $content_hash);
+ # return full cache file
+ return file_get_contents('./app/_cache/pages/all-content-'.$content_hash);
+ }
+ }
+
+ function delete_old_full_caches() {
+ $old_caches = glob('./app/_cache/pages/all-content-*');
+ foreach($old_caches as $file) unlink($file);
+ }
+
+ function save_full_cache($full_cache, $hash) {
+ $json = json_encode($full_cache);
+ if(is_writable('./app/_cache/pages')) {
+ $fp = fopen('./app/_cache/pages/all-content-'.$hash, 'w');
+ fwrite($fp, $json);
+ fclose($fp);
+ }
+ }
+
+ function create_full_cache($pages = null) {
+ $search_fields = array('url', 'file_path', 'title', 'author', 'content');
+ $store = array();
+ if (!isset($pages)) $pages = Helpers::file_cache('./content');
+ foreach ($pages as $page) {
+ if ($page['is_folder']) {
+ $current_page = AssetFactory::get($page['path']);
+ # Skip for password protected pages
+ if ($current_page['password_protect'] || $current_page['hide_from_search']) continue;
+ # Only save search field data
+ foreach ($current_page as $key => $value) {
+ if (!in_array($key, $search_fields)) unset($current_page[$key]);
+ }
+ $store[] = $current_page;
+ $children = self::create_full_cache(Helpers::file_cache($page['path']));
+ if (is_array($children)) $store = array_merge($store, $children);
+ }
+ }
+ return $store;
+ }
+
function create($route) {
# remove any unused caches for this route
$this->delete_old_caches();

0 comments on commit 0a37683

Please sign in to comment.