Search code examples
phpzend-frameworkzend-cache

Setting a flag in cache memory using zend_registry


Let's suppose I have a Php function like this:

private function verification($isXLSFile = false) {

    if ($isXLSFile) {
        $this->parseXLSFile();
    }else {
        $parsedCSVArr = $this->getCSVarr();
        Zend_Registry::get('VMMS_ZEND_CACHE')->save($parsedCSVArr, $this->getXLSCacheName());
        Zend_Registry::get('VMMS_ZEND_CACHE')->save($isXLSFile, $this->getXLSCacheName());
    }
}

And then I call another function checking the CACHE like this:

private function process(){
    Logger::info("EnterDataMassUpload - Start Process...");
    if (($parsedCSVArr = Zend_Registry::get('VMMS_ZEND_CACHE')->load($this->getXLSCacheName())) === false) {
        $this->verification();
        return;
    }
    //if XLS file it reads the lines for each sheet.
    if ($isXLSFile == true) {
        $i=0;
        //make sure there's no duplicated row
        $parsedCSVArr = array_unique($parsedCSVArr, SORT_REGULAR);
        foreach($parsedCSVArr as $sheetIndex=>$arr){
            foreach ($arr as $k=>$line) {
                $this->processLine($line, $k);
                $i++;
            }
        }
    } else { //for a CSV file
        foreach($parsedCSVArr as $k=>$line){
            $this->processLine($line, $k);
        }
    }
    // ...

As you can see, I'm trying to save 2 variables in the same place to read it in the function process(). Do you know why my flag is not working? I mean.. can I save two variables in the same place:

 Zend_Registry::get('VMMS_ZEND_CACHE')->save($parsedCSVArr, $this->getXLSCacheName());
 Zend_Registry::get('VMMS_ZEND_CACHE')->save($isXLSFile, $this->getXLSCacheName());

Solution

  • Why don't you save it under two different names ?

    Else, you can save under the same name the two variables using serialization.

    Zend_Registry::get('VMMS_ZEND_CACHE')->save(serialize(array('parsedCSVArr' => $parsedCSVArr, 'isXLSFile' => $isXLSFile)), $this->getXLSCacheName());
    

    Then, in the process function get the cache result and :

    $cacheResult['parsedCSVArray'];
    $cacheResult['isXLSFile'];