0

I was allways using same cache class to cache array of element into file and it worked fine every time. But when I'm trying to use it with Flight PHP micro framework, I cant get it work. Here is my index.php part which directs to pages witch cache:

Flight::route('/results/@uzklausa', function($uzklausa){
    //include 'funcs/functions.new.php';
    include 'funcs/simple_html_dom.php';
    Flight::register('db2', 'PDO', array('mysql:host=localhost;port=3306;dbname=rasti_failai', 'root', 'pw'), function($db) {
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    });
    //Flight::etag($uzklausa);
    Flight::set('uzklausa', $uzklausa);
    Flight::render('search_main', array('uzklausa' => $uzklausa));
    Flight::render('footer');
});

And here is my search_main.php file witch cache class:

$uzkla = str_replace('-', ' ', Flight::get('uzklausa'));

class FileCache {

  // Funkcija issaugoti informacijai
  function store($key,$data,$ttl) {

    // Atidarom faila
    $h = fopen($this->getFileName($key),'w');
    if (!$h) throw new Exception('Could not write to cache');
    // Serializinam su  TTL
    $data = serialize(array(time()+$ttl,$data));
    if (fwrite($h,$data)===false) {
      throw new Exception('Could not write to cache');
    }
    fclose($h);

  }

  // paprasta funkcija kad surasti faila pagal key
  private function getFileName($key) {

      return 'newcache/s_cache' . md5($key);

  }

  //grazina false on failure
  function fetch($key) {

      $filename = $this->getFileName($key);
      if (!file_exists($filename) || !is_readable($filename)) return false;

      $data = file_get_contents($filename);

      $data = @unserialize($data);
      if (!$data) {

         // Unlinkinam file kai unserializing failed
         unlink($filename);
         return false;

      }

      // tikrinam ar expired
      if (time() > $data[0]) {

         // Unlinkinam
         unlink($filename);
         return false;

      }
      return $data[1];
    }

}

then goes my function which returns array that needs to be cached:

function pirmi_rezultatai($uzklausa)

  { some php code wich works fine and is displayed if no cache is on }

and then function which uses cacheClass to cache array:

$cache = new FileCache();
function rezultataiVienas($uzklausa) {

    global $cache;

    // Unikalus key ID
    $key = 'pirmas-'.$uzklausa.''; //ALso tried changing this to $key = 'pirmas'; for debug, nothing changed

    // tikrinam ar jau cachintas
    if (!$data = $cache->fetch($key)) {
       //Jei nera atliekam nauja funkcija ir paimam duomenis
       $data = pirmi_rezultatai($uzklausa);

       //ir juos uzcachinam
       $cache->store($key,$data,1440000); // 1440000 2592000 12960000
    }
    return $data;
}

$pirmiRezai = rezultataiVienas($uzkla);
print_r($pirmiRezai);

and with this code when im accessing mydomain.com/results/anything im getting: Fatal error: Call to a member function fetch() on a non-object in /var/www/ctifiles/views/search_main.php on line 136

when Im changing

$pirmiRezai = rezultataiVienas($uzkla);

to

$pirmiRezai = pirmi_rezultatai($uzkla);

at the end of the code, It works perfectly fine so problem is with cacheClass. Also this class works good elswhere, even on onther domains on same webserver. So i think problem is with using flight framework?

DadaB
  • 762
  • 4
  • 12
  • 29
  • In `search_main.php` I guess you use `$objsct->fetch($var)` on line 136, could you please post that file on http://pastebin.com/ or the like? – Marcus Olsson Feb 24 '15 at 19:06
  • it's if (!$data = $cache->fetch($key)) in last chunk of code I posted in question – DadaB Mar 03 '15 at 10:13

0 Answers0