views:

219

answers:

2

I'm working on setting up caching for my site, but am having difficulty testing whether the caching is working properly or not. I've got the following in my controller:

public function read($id, $slug = null)
{
  $this->cache = Cache::instance();
  $story = $this->cache->get("story".$id);

  if (!$story) {
    $story_model = new Story_Model;
    $story = $story_model->get_story($id);
    if (!$story) throw new Kohana_404_Exception();
    $this->cache->set("story".$id, $story);
  }

  $this->template->content = new View('story');
  $this->template->title = htmlspecialchars($story->title);
  $this->template->content->story = $story;
}

This works just fine, I can even verify that the cache is being found, and the if() check is not being entered after the cache is set. My confusion is this, why is it when I edit my cache-file the changes aren't reflected in the view? For instance, my cache looks like this:

O:8:"stdClass":11:{
  s:2:"id";s:3:"636";
  s:5:"title";s:45:"Some Article Title";
  s:4:"link";s:50:"http://www.somesite.com";
  s:8:"category";s:2:"12";
  s:4:"user";s:1:"5";
  s:4:"slug";s:45:"some-article-title";
  s:7:"pubdate";s:19:"2009-08-05 03:57:50";
  s:6:"sticky";s:1:"0";
  s:7:"summary";N;
  s:13:"categorytitle";s:13:"International";
  s:8:"username";s:7:"usernameHere";
}

If I changed the title value to "Some Article Title Part 2," and refresh my view, I still see the old Title name, and the changes I made to the cache file vanish.

Am I doing it wrong? How can I test whether my cache files are being accessed instead of my database? My config file contents follow:

$config['default'] = array
(
  'driver'   => 'file',
  'params'   => APPPATH.'cache',
  'lifetime' => 1800,
  'requests' => 1000
);
+1  A: 

The serialized PHP object places constraints on the values. Look at the following line:

s:5:"title";s:45:"Some Article Title";

The s:5 means the following value will have five characters "T-i-t-l-e". Five total. The next value s:45 should have forty-five characters. You changed the text from whatever it was to "Some Article Title" for posting here, but the original had 45 chars in it total.

Adding more chars, or subtracting chars is going to break the relationship between the string length of the value, and the int-val tied to it. If s:3, your string should be three chars long.

Rather than adding/subtracting to/from the value, just change a couple characters. Change "Title" to "Ninja" and then refresh your page.

Jonathan Sampson
Wouldn't be a problem in JSON.
Alix Axel
It's not a problem in PHP either, as long as you understand what is taking place.
Jonathan Sampson
A: 

You'll want to edit your data in the original data store and treat caching like a black box.

If you using a database, get a good database manager so you can easily edit values. I use DBVisualizer... don't ask me why.

When using the file cache driver, I will delete everything under application/cache in order to clear the cache and test the caching code.

The only time I would edit cache files, was if I was actually writing a caching system to replace the file or memcached drivers.

Ozten