xoops forums

wishcraft

Module Developer
Posted on: 2015/6/25 2:28
wishcraft
wishcraft (Show more)
Module Developer
Posts: 3711
Since: 2007/5/18
#1

@request - cache files need to be reduces for cpanel machines

The Cache Files need to be reduced for the xoops_data folder, this is quite easy to do some example basic work around routines are included here to be worked into XoopsCache::read(), XoopsCache::write() & XoopsCache::delete().

XoopsCache::read() in class/cache/file.php
/**
     * Read a key from the cache
     *
     * @param  string $key Identifier for the data
     * @return mixed  The cached data, or false if the data doesn't exist, has expired, or if there was an error fetching it
     * @access public
     */
    
function read($key)
    {
        
$fingerprint md5(implode('',array_reverse(explode("_",$key)));
        if (
$this->setKey(sha1(substr($fingerprint,0,2))) === false || ! $this->init) {
            return 
false;
        }
        if (
$this->settings['lock']) {
            
$this->file->lock true;
        }
        
$data $this->file->read(true);
        if (!empty(
$data) && !empty($this->settings['serialize'])) {
            
$data stripslashes($data);
            
$data preg_replace('!s:(d+):"(.*?)";!se'"'s:'.strlen('$2').':"$2";'"$data);
            
$data unserialize($data);
            if (
is_array($data)) {
                
XoopsLoad::load('XoopsUtility');
                
$data XoopsUtility::recursive('stripslashes'$data);
            }
        } else if (
$data && empty($this->settings['serialize'])) {
            
$data = eval($data);
        }
        
$unset false;
        foreach(
$data['keys'] as $kiy => $cachetime)
        {
            if (
$cachetime !== false && intval($cachetime) < time()) {
                unset(
$data['values'][$kiy]);
                unset(
$data['keys'][$kiy]);
                
$unset true;
            }
        }
        
$this->file->close();
        return !empty(
$data['values'][$key])?$data['values'][$key]:false;
    }


XoopsCache::write() in class/cache/file.php
/**
     * Write data for key into cache
     *
     * @param  string  $key      Identifier for the data
     * @param  mixed   $data     Data to be cached
     * @param  mixed   $duration How long to cache the data, in seconds
     * @return boolean True if the data was successfully cached, false on failure
     * @access public
     */
    
function write($key$values null$duration null)
    {
        if (!isset(
$data) || ! $this->init) {
            return 
false;
        }

        
$fingerprint md5(implode('',array_reverse(explode("_",$key)));
        if (
$this->setKey(sha1(substr($fingerprint,0,2))) === false || ! $this->init) {
            return 
false;
        }

        if (
$duration == null) {
            
$duration $this->settings['duration'];
        }
        
$data $this->file->read(true);
        if (!empty(
$data) && !empty($this->settings['serialize'])) {
            
$data stripslashes($data);
            
$data preg_replace('!s:(d+):"(.*?)";!se'"'s:'.strlen('$2').':"$2";'"$data);
            
$data unserialize($data);
            if (
is_array($data)) {
                
XoopsLoad::load('XoopsUtility');
                
$data XoopsUtility::recursive('stripslashes'$data);
            }
        } else if (
$data && empty($this->settings['serialize'])) {
            
$data = eval($data);
        }
        foreach(
$data['keys'] as $kiy => $cachetime)
        {
            if (
$cachetime !== false && intval($cachetime) < time()) {
                unset(
$data['values'][$kiy]);
                unset(
$data['keys'][$kiy]);
            }
        }
        
$data['keys'][$key] = $expires time() + $duration;
        
$data['values'][$key] = $values;
        
        
$windows false;
        
$lineBreak "n";

        if (
substr(PHP_OS03) == "WIN") {
            
$lineBreak "rn";
            
$windows true;
        }
        ;
        if (!empty(
$this->settings['serialize'])) {
            if (
$windows) {
                
$data str_replace('\''\\\\'serialize($data));
            } else {
                
$data serialize($data);
            }
            
$contents $data $lineBreak;
        } else {
            
$contents "return " var_export($datatrue) . ";" $lineBreak;
        }

        if (
$this->settings['lock']) {
            
$this->file->lock true;
        }
        
$success $this->file->write($contents);
        
$this->file->close();

        return 
$success;
    }


XoopsCache::delete() in class/cache/file.php
/**
     * Delete a key from the cache
     *
     * @param  string  $key Identifier for the data
     * @return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
     * @access public
     */
    
function delete($key)
    {
        
$fingerprint md5(implode('',array_reverse(explode("_",$key)));
        if (
$this->setKey(sha1(substr($fingerprint,0,2))) === false || ! $this->init) {
            return 
false;
        }
        if (
$this->settings['lock']) {
            
$this->file->lock true;
        }
        
$data $this->file->read(true);
        if (!empty(
$data) && !empty($this->settings['serialize'])) {
            
$data stripslashes($data);
            
$data preg_replace('!s:(d+):"(.*?)";!se'"'s:'.strlen('$2').':"$2";'"$data);
            
$data unserialize($data);
            if (
is_array($data)) {
                
XoopsLoad::load('XoopsUtility');
                
$data XoopsUtility::recursive('stripslashes'$data);
            }
        } else if (
$data && empty($this->settings['serialize'])) {
            
$data = eval($data);
        }
        
$unset false;
        foreach(
$data['keys'] as $kiy => $cachetime)
        {
            if (
$cachetime !== false && intval($cachetime) < time()) {
                unset(
$data['values'][$kiy]);
                unset(
$data['keys'][$kiy]);
                
$unset true;
            }
        }
        unset(
$data['values'][$key]);
        unset(
$data['keys'][$key]);
        
        
$windows false;
        
$lineBreak "n";

        if (
substr(PHP_OS03) == "WIN") {
            
$lineBreak "rn";
            
$windows true;
        }
        ;
        if (!empty(
$this->settings['serialize'])) {
            if (
$windows) {
                
$data str_replace('\''\\\\'serialize($data));
            } else {
                
$data serialize($data);
            }
            
$contents $data $lineBreak;
        } else {
            
$contents "return " var_export($datatrue) . ";" $lineBreak;
        }

        if (
$this->settings['lock']) {
            
$this->file->lock true;
        }
        
$success $this->file->write($contents);
        
$this->file->close();

        return 
true;
    }