yf_output_cache class

Class to handle output caching.

Fileplugins/cache/classes/yf_output_cache.class.php
Use_class('output_cache')
Extendsyf_cache

Public properties

A site sets them in its config with $PROJECT_CONF['output_cache']['NAME'] = ...;

PropertyDefaultDescription
$APPEND_STRING_CODE
string
'return PHP_EOL."<!-- cache generated at ".date("Y-m-d H:i'... PHP code that will be eval'ed and added to the cache file @conf_skip
$CACHE_APPEND_STRING
bool
true Append string to the end of the cached file
$CACHE_CONTROL_FROM_URL
bool
true Allow to refresh cache from url
$CACHE_NS
string
'oc_' Namespace for drivers other than 'file'
$CUSTOM_CACHE_TTLS
array
[] Use this if you need to have some page cached different from the global setting 'OUTPUT_CACHE_TTL' NOTE: NOT WORKING ON WINDOWS (php < 5.3.0) ! (because we are using touch function that is broken under windows)
$NO_NEED_TO_CACHE
bool
false @conf_skip Tells that current page will not be cached (default)
$OUTPUT_CACHE_DIR
string
'pages_cache/' Output cache dir (relative to the SITE_PATH constant)
$OUTPUT_CACHE_TTL
int
604800 Output cache TTL, in seconds (0 - for unlimited)
$OUTPUT_CACHING
bool
false Output caching on/off
$WHITE_LIST_PATTERN
string
'' Use instead of '_OC_STOP_LIST', include _ONLY_ that is matched this pattern, will be checked if non-empty
$_OC_STOP_LIST
array
['object=(account|advert|aff|email|forum|manage_escorts|que'..., 'object=search&+', 'task=(login|logout).*', 'debug_mode']
array (
  0 => 'object=(account|advert|aff|email|forum|manage_escorts|que|reviews|reviews_search|stats|task_loader|user_info|user_profile).*',
  1 => 'object=search&+',
  2 => 'task=(login|logout).*',
  3 => 'debug_mode',
)
Stop-list for output caching (REGEXPs allowed here) @conf_skip
$_cur_cache_name null

Inherited properties

from yf_cache: $AUTO_CACHE_NS, $DRIVER, $FILES_TTL, $FORCE_REBUILD_CACHE, $NO_CACHE, $RANDOM_TTL_ADD, $REMOVED_DRIVERS, $TTL, $_NO_CACHE_WHY, $_driver, $_driver_ok, $_extend, $_init_complete, $_no_cache, $_tried_to_connect

Public methods 15

refresh

public function refresh($event = '', $params = [])

Refreshing cache files.

    public function refresh($event = '', $params = [])
    {
    }

Methods starting with _ hooks and helpers; not routable as page actions

__call

public function __call($name, $args)

Catch missing method call.

    public function __call($name, $args)
    {
        return main()->extend_call($this, $name, $args);
    }
public function _cache_refresh_all()

Cleanup all cache files.

    public function _cache_refresh_all()
    {
    }
public function _check_if_need_to_cache()

Check if current page need to be cached.

    public function _check_if_need_to_cache()
    {
        // Fast implementation of disabling output caching for the current page
        if (conf('no_output_cache')) {
            $this->NO_NEED_TO_CACHE = true;
            return false;
        }
        if (main()->NO_GRAPHICS && ! conf('output_cache_force')) {
            $this->NO_NEED_TO_CACHE = true;
            return false;
        }
        if (($_SESSION['user_group'] ?? null) > 1 || ($_COOKIE['member_id'] ?? null)) {
            $this->NO_NEED_TO_CACHE = true;
            return false;
        }
        // Special for the 'share on facebook' feature
        if (false !== strpos(($_SERVER['HTTP_USER_AGENT'] ?? ''), 'facebookexternalhit')) {
            $this->NO_NEED_TO_CACHE = true;
            return false;
        }
        // Check 'white' list first
        $w = $this->WHITE_LIST_PATTERN;
        if ( ! empty($w)) {
            // Array like: 'search' => array(), 'static_pages' => array('show')
            if (is_array($w)) {
                if ( ! isset($w[$_GET['object']])) {
                    $this->NO_NEED_TO_CACHE = true;
                    return $this->NO_NEED_TO_CACHE;
                } elseif ( ! empty($w[$_GET['object']]) && ! in_array(($_GET['action'] ?? null), (array) $w[$_GET['object']])) {
                    $this->NO_NEED_TO_CACHE = true;
                    return $this->NO_NEED_TO_CACHE;
                }
            } else {
                if ( ! preg_match('/' . $w . '/i', $_SERVER['QUERY_STRING'])) {
                    $this->NO_NEED_TO_CACHE = true;
                    return $this->NO_NEED_TO_CACHE;
                }
            }
            return false;
        }
        // Try to search current query string in the stop list
        foreach ((array) $this->_OC_STOP_LIST as $pattern) {
            if (preg_match('/' . $pattern . '/i', $_SERVER['QUERY_STRING'])) {
                $this->NO_NEED_TO_CACHE = true;
                return $this->NO_NEED_TO_CACHE;
            }
        }
        return false; // Default return value
    }
public function _clean_by_hashes($hashes = [])

Refreshing cache files (by given array of hashes).

    public function _clean_by_hashes($hashes = [])
    {
    }
public function _clean_by_params($params = [])

Refreshing cache files (by given array of params).

    public function _clean_by_params($params = [])
    {
    }

_clean_by_sql

public function _clean_by_sql($sql = '')

Refreshing cache files (by given direct sql).

    public function _clean_by_sql($sql = '')
    {
    }

_exec_trigger

public function _exec_trigger($params = [])

Output cache file and stop.

    public function _exec_trigger($params = [])
    {
    }
public function _get_page_cache_name()

Generate cahe name.

    public function _get_page_cache_name()
    {
        if ( ! isset($this->_cur_cache_name)) {
            $this->_cur_cache_name = md5(
                ($_SERVER['HTTP_HOST'] ?? null)
                . '/' . ($_SERVER['SCRIPT_NAME'] ?? null)
                . '?' . ($_SERVER['QUERY_STRING'] ?? null)
                . '---' . conf('language')
                . '---' . (int) conf('SITE_ID')
                . '---' . (($_SESSION['user_group'] ?? null) <= 1 ? 'guest' : 'member')
            );
        }
        return $this->_cur_cache_name;
    }

_init

public function _init($params = [])

Module constructor.

    public function _init($params = [])
    {
        // Assign group_id = 1 for guests
        if (empty($_SESSION['user_group'])) {
            $_SESSION['user_group'] = 1;
        }
        $this->OUTPUT_CACHING = main()->OUTPUT_CACHING;

        if (($_SESSION['user_group'] ?? null) > 1 || ($_COOKIE['member_id'] ?? null)) {
            main()->OUTPUT_CACHING = false;
            $this->OUTPUT_CACHING = false;
        }
        // Ability to handle output cache through http query
        if ($this->OUTPUT_CACHING && $this->CACHE_CONTROL_FROM_URL) {
            // Display current page without as it is without cache
            if (isset($_GET['no_cache']) || false !== strpos(($_SERVER['REQUEST_URI'] ?? ''), '?no_cache')) {
                conf('no_output_cache', true);
            }
            if (isset($_GET['refresh_cache']) || false !== strpos(($_SERVER['REQUEST_URI'] ?? ''), '?refresh_cache')) {
                conf('refresh_output_cache', true);
            }
        }
        parent::_init($params);
    }

_post_filter

public function _post_filter()

Do some actions before throw output.

    public function _post_filter()
    {
    }

_pre_filter

public function _pre_filter($body = '')

Do special actions before put to output cache.

    public function _pre_filter($body = '')
    {
        return $body;
    }
public function _process_output_cache()

Output cache file and stop.

    public function _process_output_cache()
    {
        $this->_check_if_need_to_cache();
        if ( ! $this->OUTPUT_CACHING || ($_SERVER['REQUEST_METHOD'] ?? null) != 'GET' || MAIN_TYPE_ADMIN || $this->NO_NEED_TO_CACHE) {
            return false;
        }
        if (DEBUG_MODE) {
            $time_start = microtime(true);
        }
        $cache_key = $this->CACHE_NS . $this->_get_page_cache_name();
    }
public function _put_page_to_output_cache($body = [])

Try to put current page to the output cache.

    public function _put_page_to_output_cache($body = [])
    {
        $this->_check_if_need_to_cache();
    }
public function _send_http_headers($cache_last_modified_time = 0)

Send HTTP headers.

    public function _send_http_headers($cache_last_modified_time = 0)
    {
        // Send correct headers
        header('Content-Type: text/html; charset=' . conf('charset'), 1);
        header('Content-language: ' . conf('language'), 1);
        header('Expires: ' . gmdate('D, d M Y H:i:s', $cache_last_modified_time + 600/*$this->OUTPUT_CACHE_TTL*/) . ' GMT', 1);
        header('Cache-Control: ', 1);
        header('Pragma: ', 1);
        // Set default values:
        $date = gmdate('D, d M Y H:i:s', $cache_last_modified_time) . ' GMT';
        $etag = '' . md5($date) . '';
        // Check http headers:
        $modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? ($_SERVER['HTTP_IF_MODIFIED_SINCE'] ?? null) == $date : null;
        if ( ! empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && ($timestamp = strtotime(($_SERVER['HTTP_IF_MODIFIED_SINCE'] ?? ''))) > 0) {
            $modified_since = $cache_last_modified_time <= $timestamp;
        } else {
            $modified_since = null;
        }
        $none_match = ! empty($_SERVER['HTTP_IF_NONE_MATCH']) ? ($_SERVER['HTTP_IF_NONE_MATCH'] ?? null) == $etag : null;
        // The type checking here is very important, be careful when changing entries.
        if (($modified_since !== null || $none_match !== null) && $modified_since !== false && $none_match !== false) {
            header('HTTP/1.0 304 Not Modified');
            exit();
        }
        // Send appropriate response:
        header('Last-Modified: ' . $date, 1);
        header('ETag: ' . $etag, 1);
    }

Inherited methods

from yf_cache: __clone(), _clear_cache_files(), _connect(), _get_avail_drivers_list(), _init_settings(), _set_current_driver(), _url_action_allowed(), clean(), clean_all(), clear(), clear_all(), del(), del_by_prefix(), flush(), get(), getset(), list_keys(), multi_del(), multi_get(), multi_set(), put(), refresh_all(), set()