src/Webiny/Hrc/Hrc.php
public function save($name, $content, $cacheRule = null, $cacheTagsAppend = [])
    {
        // initialize log
        $log = new DebugLog();
        $log->addMessage('State', 'Save');

        // check if get
        if (!isset($_SERVER['REQUEST_METHOD']) || strtolower($_SERVER['REQUEST_METHOD']) != 'get') {
            $log->addMessage('CacheRule-Match', 'Only GET requests can be cached.');

            return false;
        }

        // get rule
        if (!($rule = $this->getMatchedRule($cacheRule)) || $rule->getCacheRule()->getTtl() <= 0) {
            $log->addMessage('CacheRule-Match', 'No rule matched the request.');

            return false;
        }
        $log->addMessage('CacheRule-Match', sprintf('%s rule matched the request.', $rule->getCacheRule()->getName()));

        // append tags
        $rule->getCacheRule()->appendTags($cacheTagsAppend);

        // create key
        $key = $this->createJointKey($name, $rule->getCacheKey());

        // create save payload instance
        $savePayload = new SavePayload($key, $content, $rule);

        // callback: beforeSave
        foreach ($this->callbacks as $cb) {
            call_user_func_array([$cb, 'beforeSave'], [$savePayload]);
        }

        // check if a callback set the save flag to false
        if(!$savePayload->getSaveFlag()){
            return false;
        }

        $log->addMessage('CacheRule-CacheKey', $savePayload->getKey());
        $saved = $this->cacheStorage->save($savePayload->getKey(), $savePayload->getContent(),
            $savePayload->getRule()->getCacheRule()->getTtl());

        // update the index
        if ($saved) {
            $this->indexStorage->save($savePayload->getKey(), $savePayload->getRule()->getCacheRule()->getTags(),
                ($savePayload->getRule()->getCacheRule()->getTtl()));
            $log->addMessage('CacheStorage-Save', 'Cache saved.');
        } else {
            throw new HrcException('There has been an error while trying to save the cache.');
        }

        $this->log = $log;

        // when saved, return cache key
        // callback: afterSave
        foreach ($this->callbacks as $cb) {
            call_user_func_array([$cb, 'afterSave'], [$savePayload]);
        }

        return $savePayload->getKey();
    }
public function save($name, $content, $cacheRule = null, $cacheTagsAppend = [])
    {
        // initialize log
        $log = new DebugLog();
        $log->addMessage('State', 'Save');

        // check if get
        if (!isset($_SERVER['REQUEST_METHOD']) || strtolower($_SERVER['REQUEST_METHOD']) != 'get') {
            $log->addMessage('CacheRule-Match', 'Only GET requests can be cached.');

            return false;
        }

        // get rule
        if (!($rule = $this->getMatchedRule($cacheRule)) || $rule->getCacheRule()->getTtl() <= 0) {
            $log->addMessage('CacheRule-Match', 'No rule matched the request.');

            return false;
        }
        $log->addMessage('CacheRule-Match', sprintf('%s rule matched the request.', $rule->getCacheRule()->getName()));

        // append tags
        $rule->getCacheRule()->appendTags($cacheTagsAppend);

        // create key
        $key = $this->createJointKey($name, $rule->getCacheKey());

        // create save payload instance
        $savePayload = new SavePayload($key, $content, $rule);

        // callback: beforeSave
        foreach ($this->callbacks as $cb) {
            call_user_func_array([$cb, 'beforeSave'], [$savePayload]);
        }

        // check if a callback set the save flag to false
        if(!$savePayload->getSaveFlag()){
            return false;
        }

        $log->addMessage('CacheRule-CacheKey', $savePayload->getKey());
        $saved = $this->cacheStorage->save($savePayload->getKey(), $savePayload->getContent(),
            $savePayload->getRule()->getCacheRule()->getTtl());

        // update the index
        if ($saved) {
            $this->indexStorage->save($savePayload->getKey(), $savePayload->getRule()->getCacheRule()->getTags(),
                ($savePayload->getRule()->getCacheRule()->getTtl()));
            $log->addMessage('CacheStorage-Save', 'Cache saved.');
        } else {
            throw new HrcException('There has been an error while trying to save the cache.');
        }

        $this->log = $log;

        // when saved, return cache key
        // callback: afterSave
        foreach ($this->callbacks as $cb) {
            call_user_func_array([$cb, 'afterSave'], [$savePayload]);
        }

        return $savePayload->getKey();
    }
public function save ( $ name , $ content , $ cacheRule = null , $ cacheTagsAppend = [ ] ) { // initialize log $ log = new DebugLog ( ) ; $ log -> addMessage ( 'State' , 'Save' ) ; // check if get if ( ! isset ( $ _SERVER [ 'REQUEST_METHOD' ] ) || strtolower ( $ _SERVER [ 'REQUEST_METHOD' ] ) != 'get' ) { $ log -> addMessage ( 'CacheRule-Match' , 'Only GET requests can be cached.' ) ; return false ; } // get rule if ( ! ( $ rule = $ this -> getMatchedRule ( $ cacheRule ) ) || $ rule -> getCacheRule ( ) -> getTtl ( ) <= 0 ) { $ log -> addMessage ( 'CacheRule-Match' , 'No rule matched the request.' ) ; return false ; } $ log -> addMessage ( 'CacheRule-Match' , sprintf ( '%s rule matched the request.' , $ rule -> getCacheRule ( ) -> getName ( ) ) ) ; // append tags $ rule -> getCacheRule ( ) -> appendTags ( $ cacheTagsAppend ) ; // create key $ key = $ this -> createJointKey ( $ name , $ rule -> getCacheKey ( ) ) ; // create save payload instance $ savePayload = new SavePayload ( $ key , $ content , $ rule ) ; // callback: beforeSave foreach ( $ this -> callbacks as $ cb ) { call_user_func_array ( [ $ cb , 'beforeSave' ] , [ $ savePayload ] ) ; } // check if a callback set the save flag to false if ( ! $ savePayload -> getSaveFlag ( ) ) { return false ; } $ log -> addMessage ( 'CacheRule-CacheKey' , $ savePayload -> getKey ( ) ) ; $ saved = $ this -> cacheStorage -> save ( $ savePayload -> getKey ( ) , $ savePayload -> getContent ( ) , $ savePayload -> getRule ( ) -> getCacheRule ( ) -> getTtl ( ) ) ; // update the index if ( $ saved ) { $ this -> indexStorage -> save ( $ savePayload -> getKey ( ) , $ savePayload -> getRule ( ) -> getCacheRule ( ) -> getTags ( ) , ( $ savePayload -> getRule ( ) -> getCacheRule ( ) -> getTtl ( ) ) ) ; $ log -> addMessage ( 'CacheStorage-Save' , 'Cache saved.' ) ; } else { throw new HrcException ( 'There has been an error while trying to save the cache.' ) ; } $ this -> log = $ log ; // when saved, return cache key // callback: afterSave foreach ( $ this -> callbacks as $ cb ) { call_user_func_array ( [ $ cb , 'afterSave' ] , [ $ savePayload ] ) ; } return $ savePayload -> getKey ( ) ; }
Save a value into cache.
In case if no cache rule was matched, false is returned.


@param string $name Name that will be used to construct the cache key.
@param string $content Content that should be save, must be a string.
@param string $cacheRule The name of the cache rule that should be used, instead of the matched cache rule based on the request.
@param array  $cacheTagsAppend Optionally you can append additional tags to the matched rule for this request.
These tags can be used later to purge the cache.

@return bool|string A cache key is returned if save was successful, otherwise false.
@throws HrcException
Save a value into cache . In case if no cache rule was matched false is returned .