TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
Forums are in read-only mode while we transition to new software.
Does Kohana ORM and Cache work together?
  • I'm currently working on improving the caching of our website. And in the process I noticed that when I updated the database content for a "node" through a php function, the cache for it automatically got updated with the latest changes. As a result, the class that used this cache displayed the latest information even though I never told the program to update the cache.

    It's 4:30 AM here so I might be just tired and unsure of what's happening. But did ORM and Cache module just work together? It looks like the ORM module updated the cache for this node without even knowing what the cache variable should be called.

  • If ORM::$_reload_on_wakeup (TRUE by default) is enabled, a single row will be retrieved again from the database once the object is "woken up" (__wakeup()).

    These queries are per PK, meaning they're really fast but still cause many additional db hits. This basically means that, by default, you only gain caching objects when the initial query before caching is slow. To cache objects without wakeup reload, set $_reload_on_wakeup to FALSE (on model level?)

  • Thank you but turning that false didn't generate the expected outcome.

    Here's my code below, this is not a serious problem but I am unsure why the cache is getting updated on its own magically.

    PAGE CLASS

    THIS CLASS LOADS THE NODE AND DISPLAYS IT TO THE PUBLIC

    ======

        // The following will return false if cache for this node is NOT set
        if (!($this->node = $cache->get('node-'.$id, false))) {
    
                  // If Cache is not set, find the node and set it
          $this->node = ORM::factory('node', $id);
    
          if (isset($this->node->id)) {
            $cache->set('node-'.$this->node->id, $this->node);
          }
        }
    

    ====== EDIT CLASS

    THIS CLASS EDITS THE NODE ON THE MYSQL DATABASE

    ======

    ..... FUNCTIONS FOR SETTING THE POSTED VALUES TO ORM THEN UPDATE DATABASE ...... $update->update();

    ======

    At the end of the Edit class, it will update the database. However I did not set the cache for 'node-'.$id yet. So when I go to the actual page, shouldn't it load outdated data from the old cache? Why is it that when I go to the actual page, I see the new data that I just updated on the database?

  • Random note. I have no idea why my fonts are in bold/large size in my reply above. But it wasn't intentional.

  • You put ==== under your text. That's a heading in markdown.

  • @saeho85 one more time: ORM doesn't use Cache in any way (by default). When your object is read from cache, it's data is reloaded from the database anyways.

  • So, does this mean that it's not possible to store an entire database query as a cache?

    i.e.

    $variable = MYSQL SELECT STATMENT $cache->set('VARIABLE', $variable);

    So the above code is not possible?

  • Why would you think it's impossible?

    $posts = ORM::factory('post')->find_all()->cached(); 
    Cache::instance()->set('posts', $posts);
    

    But when calling 'posts' later from cache, every record will be 'refreshed' from the database if $_reload_on_wakeup isn't FALSE.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion