From b46f0545b1fe836fe105f60c0e555f02d92d96c0 Mon Sep 17 00:00:00 2001
From: David Beniamine <david.beniamine@tetras-libre.fr>
Date: Tue, 15 Feb 2022 17:50:39 +0100
Subject: [PATCH] Add cache

---
 .gitmodules                |  3 +++
 Readme.md                  |  9 +++++++++
 config/config.yml          | 12 +++++++++++-
 config/config.yml.template | 12 +++++++++++-
 content/cache/.gitignore   |  1 +
 docker-compose.yml         |  1 +
 pico                       | 28 ++++++++++++++++++++++++++++
 plugins/pico-cache         |  1 +
 8 files changed, 65 insertions(+), 2 deletions(-)
 create mode 100644 .gitmodules
 create mode 100644 content/cache/.gitignore
 create mode 100755 pico
 create mode 160000 plugins/pico-cache

diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 0000000..d882cf2
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "plugins/pico-cache"]
+	path = plugins/pico-cache
+    url = https://github.com/Tetras-Libre/pico_cache.git
diff --git a/Readme.md b/Readme.md
index 0af94e5..b98613e 100644
--- a/Readme.md
+++ b/Readme.md
@@ -11,6 +11,15 @@
 1. Put your contents in `content` directory
 2. Put images etc in `assets` directory
 
+### Cache
+
+By default an HTML cache is enable with a lifetime of 7 days. This means that **your changes will not be visible until you clear the cache**
+
+To clear the cache, simply run `./pico cache`.
+
+You can change this behavior in the config file.
+
+
 ### Customization
 
 #### Plugins / themes
diff --git a/config/config.yml b/config/config.yml
index ebc92de..897e687 100644
--- a/config/config.yml
+++ b/config/config.yml
@@ -22,7 +22,7 @@ twig_config:                        # Twig template engine config
     strict_variables: false         # If set to true, Twig will bail out when unset variables are being used
     charset: utf-8                  # The charset used by Twig templates
     debug: ~                        # Enable Twig's debug mode
-    cache: false                    # Enable Twig template caching by specifying a path to a writable directory
+    cache: content/cache/twig                    # Enable Twig template caching by specifying a path to a writable directory
     auto_reload: ~                  # Recompile Twig templates whenever the source code changes
 
 ##
@@ -58,3 +58,13 @@ DummyPlugin.enabled: false          # Force the plugin "DummyPlugin" to be disab
 # Custom
 #
 my_custom_setting: Hello World!     # You can access custom settings in themes using {{ config.my_custom_setting }}
+
+##
+# Cache
+##
+PicoZCache:
+  enabled: true          # True/False if cache is enabled
+  dir: content/cache/html/    # Directory where cache should be saved
+  time: 2592000           # Interval between caching (period from one to second cache) in seconds, here is 7 days = 60 * 60 * 24 * 30.
+  xhtml_output: false    # If true, XHTML Content-Type header will be sent when loading cache page
+  ignore_url_regex: "/blog/"
diff --git a/config/config.yml.template b/config/config.yml.template
index 181c17d..837df63 100644
--- a/config/config.yml.template
+++ b/config/config.yml.template
@@ -22,7 +22,7 @@ twig_config:                        # Twig template engine config
     strict_variables: false         # If set to true, Twig will bail out when unset variables are being used
     charset: utf-8                  # The charset used by Twig templates
     debug: ~                        # Enable Twig's debug mode
-    cache: false                    # Enable Twig template caching by specifying a path to a writable directory
+    cache: content/cache/twig                    # Enable Twig template caching by specifying a path to a writable directory
     auto_reload: ~                  # Recompile Twig templates whenever the source code changes
 
 ##
@@ -58,3 +58,13 @@ DummyPlugin.enabled: false          # Force the plugin "DummyPlugin" to be disab
 # Custom
 #
 my_custom_setting: Hello World!     # You can access custom settings in themes using {{ config.my_custom_setting }}
+
+##
+# Cache
+##
+PicoZCache:
+  enabled: true          # True/False if cache is enabled
+  dir: content/cache/html/    # Directory where cache should be saved
+  time: 2592000           # Interval between caching (period from one to second cache) in seconds, here is 7 days = 60 * 60 * 24 * 30.
+  xhtml_output: false    # If true, XHTML Content-Type header will be sent when loading cache page
+  ignore_url_regex: "/blog/"
diff --git a/content/cache/.gitignore b/content/cache/.gitignore
new file mode 100644
index 0000000..72e8ffc
--- /dev/null
+++ b/content/cache/.gitignore
@@ -0,0 +1 @@
+*
diff --git a/docker-compose.yml b/docker-compose.yml
index 2e32794..9149885 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -7,3 +7,4 @@ services:
       - "./assets:/var/www/html/assets"
       - "./content:/var/www/html/content"
       - "./config:/var/www/html/config"
+      - "./plugins/pico-cache/PicoZCache.php:/var/www/html/plugins/PicoZCache.php"
diff --git a/pico b/pico
new file mode 100755
index 0000000..b8b7b4b
--- /dev/null
+++ b/pico
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+usage() {
+    echo "Usage $0 [command]"
+    echo "Commands"
+    echo -e "\tcache clear the HTML cache"
+    echo -e "\tperms fix permissions"
+    echo -e "\thelp display this message and exit"
+}
+docker="docker-compose exec pico"
+
+case $1 in
+    "cache")
+        $docker rm -rf content/cache/html/*
+        $docker rm -rf content/cache/twig/*
+        ;;
+    "perms")
+        $docker chown -R $(id -u):33 content/
+        $docker chmod -R g+w content/cache
+        ;;
+    "help")
+        usage
+        ;;
+    *)
+        echo "Unknown command '$1'"
+        usage
+        exit 1
+esac
diff --git a/plugins/pico-cache b/plugins/pico-cache
new file mode 160000
index 0000000..e22f9ec
--- /dev/null
+++ b/plugins/pico-cache
@@ -0,0 +1 @@
+Subproject commit e22f9ece98272cd63b6ffa753c45483be85f380b
-- 
GitLab