Browse Source

Allow the selection of cache lenght

Jaap Marcus 5 years ago
parent
commit
71820ddb0a

+ 19 - 8
bin/v-add-fastcgi-cache

@@ -1,14 +1,14 @@
 #!/bin/bash
 # info: Add FastCGI nginx support
-# options: USER DOMAIN [DEBUG]
+# options: USER DOMAIN [LENTGH] [DEBUG] [RESTART]
 # labels: hestia web
 #
-# example: v-add-fastcgi-cache user domain.tld
+# example: v-add-fastcgi-cache user domain.tld 30m
 #
 # The function enables FastCGI cache for nginx
+# For Length only use time in seconds (10s) minutes (10m) or days (10d)
 # Add "yes" as last parameter to append debug information to response headers
 
-
 #----------------------------------------------------------#
 #                    Variable&Function                     #
 #----------------------------------------------------------#
@@ -16,7 +16,9 @@
 # Argument definition
 user=$1
 domain=$2
-debug=$3
+length=${3-30m}
+debug=${4-no}
+restart=${5-no}
 
 # Includes
 source $HESTIA/func/main.sh
@@ -34,6 +36,10 @@ is_object_unsuspended 'user' 'USER' "$user"
 is_object_valid 'web' 'DOMAIN' "$domain"
 is_object_unsuspended 'web' 'DOMAIN' "$domain"
 
+if ! [[ "$length" =~ ^[0-9].*[s|m|d]$ ]]; then
+   echo "Invalid length";
+   exit 2;
+fi
 # Perform verification if read-only mode is enabled
 check_hestia_demo_mode
 
@@ -94,16 +100,21 @@ mkdir -p /var/cache/nginx/micro/$domain
 #                       Hestia                             #
 #----------------------------------------------------------#
 
-if [ -z "$FASTCGI" ]; then
+if [ -z "$FASTCGI_CACHE" ]; then
     add_object_key "web" 'DOMAIN' "$domain" 'FASTCGI_CACHE' 'ALIAS'
 fi
+if [ -z "$FASTCGI_LENGTH" ]; then
+add_object_key "web" 'DOMAIN' "$domain" '$FASTCGI_LENGTH' 'ALIAS'
+fi
 
 # Set FastCGI cache flag to enabled
 update_object_value 'web' 'DOMAIN' "$domain" '$FASTCGI_CACHE' 'yes'
 
-# Restart web server
-$BIN/v-restart-web
-check_result $? "Web server restart failed" > /dev/null
+if [ "$restart" = "yes"]; then
+    # Restart web server
+    $BIN/v-restart-web
+    check_result $? "Web server restart failed" > /dev/null
+fi
 
 # Logging
 log_history "Enabled FastCGI cache for $domain"

+ 11 - 4
web/edit/web/index.php

@@ -68,6 +68,10 @@ if (empty($v_letsencrypt)) $v_letsencrypt = 'no';
 $v_ssl_home = $data[$v_domain]['SSL_HOME'];
 $v_backend_template = $data[$v_domain]['BACKEND'];
 $v_nginx_cache = $data[$v_domain]['FASTCGI_CACHE'];
+$v_nginx_cache_length = $data[$v_domain]['FASTCGI_CACHE_LENGTH'];
+if(empty($v_nginx_cache_length)){
+    $v_nginx_cache_length = '30m';
+}
 $v_proxy = $data[$v_domain]['PROXY'];
 $v_proxy_template = $data[$v_domain]['PROXY'];
 $v_proxy_ext = str_replace(',', ', ', $data[$v_domain]['PROXY_EXT']);
@@ -329,11 +333,14 @@ if (!empty($_POST['save'])) {
     }
 
     // Enable/Disable nginx cache
-    if (($_SESSION['WEB_SYSTEM'] == 'nginx') && ($v_nginx_cache != $_POST['v_nginx_cache'] ) && (empty($_SESSION['error_msg']))) {
+    if (($_SESSION['WEB_SYSTEM'] == 'nginx') && ($v_nginx_cache != $_POST['v_nginx_cache'] ) && ($v_nginx_cache_length != $_POST['v_nginx_cache_length'] && $_POST['v_nginx_cache'] = "yes" ) && (empty($_SESSION['error_msg']))) {
         if ( $_POST['v_nginx_cache'] == 'yes' ) {
-           exec (HESTIA_CMD."v-add-fastcgi-cache ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
-           check_return_code($return_var,$output);
-           unset($output); 
+            if (!empty ($_POST['v_nginx_cache_length'])){
+                echo $_POST['v_nginx_cache_length'] = "30m";
+            }
+            exec (HESTIA_CMD."v-add-fastcgi-cache ".$v_username." ".escapeshellarg($v_domain).' '. escapeshellarg($_POST['v_nginx_cache_length']) , $output, $return_var);
+            check_return_code($return_var,$output);
+            unset($output); 
         } else {
             exec (HESTIA_CMD."v-delete-fastcgi-cache ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
             check_return_code($return_var,$output);

+ 2 - 0
web/js/pages/edit_web.js

@@ -241,8 +241,10 @@ $(function() {
     
         if(select.val() != 'yes'){
             $('#v-clear-cache').hide();
+            $('#v_nginx_cache_length').hide();
         } else {
             $('#v-clear-cache').show();
+            $('#v_nginx_cache_length').show();
         }
     });  
     

+ 12 - 0
web/templates/admin/edit_web.html

@@ -397,6 +397,18 @@
                                         <option value="yes" <?php if ( $v_nginx_cache == "yes") { echo 'selected'; }?>><?php echo _('Yes');?></option>
                                     </select>
                                 </td>
+                            </tr>
+                            <tr>
+                                <td class="step-left">
+                                    <table id="v_nginx_cache_length" style="display:<?php if ($v_nginx_cache != 'yes' ) { echo 'none';} else {echo 'block';}?> ;" >
+                                        <td class="vst-text input-label">
+                                            <?php print _('Cache length'); ?> <span class="optional"><?=_('For example: 30s, 10m or 1d');?>
+                                        </td>
+                                        <tr>
+                                            <td><input type="text" size="20" class="vst-input" name="v_nginx_cache_length" value="<?=htmlentities(trim($v_nginx_cache_length, "'"))?>"></td>
+                                        </tr>
+                                    </table>
+                                </td>
                             </tr>
                              <?php } ?>                          
                             <?php if (!empty($_SESSION['WEB_BACKEND'])) { ?>

+ 12 - 0
web/templates/user/edit_web.html

@@ -397,6 +397,18 @@
                                         <option value="yes" <?php if ( $v_nginx_cache == "yes") { echo 'selected'; }?>><?php echo _('Yes');?></option>
                                     </select>
                                 </td>
+                            </tr>
+                            <tr>
+                                <td class="step-left">
+                                    <table id="v_nginx_cache_length" style="display:<?php if ($v_nginx_cache == 'no' ) { echo 'none';} else {echo 'block';}?> ;" >
+                                        <td class="vst-text input-label">
+                                            <?php print _('Cache length'); ?> <span class="optional"><?=_('For example: 30s, 10m or 1d');?>
+                                        </td>
+                                        <tr>
+                                            <td><input type="text" size="20" class="vst-input" name="v_nginx_cache_length" value="<?=htmlentities(trim($v_nginx_cache_length, "'"))?>"></td>
+                                        </tr>
+                                    </table>
+                                </td>
                             </tr>
                              <?php } ?>                          
                             <?php if (!empty($_SESSION['WEB_BACKEND'])) { ?>