Welcome Guest, Not a member yet? Register   Sign In
Database Caching - turning on/off with if/else isn't working
#1

[eluser]tinawina[/eluser]
I posted this yesterday within a thread I started titled "Database Cache - automatically "ping" pages on database update to create new cache files?" (http://ellislab.com/forums/viewthread/224464). I'm reposting as a separate thread because this is really a different problem than the one I originally wanted help with. Here's the problem.

Our queries are generated dynamically according to the URL string. We only want to cache a particular query. I can determine that query using one variable in my script. I am trying to set up an if/else that says: if this query is for X pages turn on caching, otherwise do not cache. Here's what I've got but it doesn't work (I am using $this->CI because this is all happening in a library file using CI v.2.1.2):
Code:
function fetch_results($query, $field = '') // $field comes from the URL, eg., http://mysite.org/field/X
{
$this->CI->db->cache_off(); // caching should be off to begin with

if ($field == 'X') // here I'm determining which query I'm running - if X then turn caching on
{
  $this->CI->db->cache_on();
}
$query = $this->CI->db->query($query);
I've also set this up like so but this also doesn't work:
Code:
function fetch_results($query, $field = '')
{
if ($field == 'X')
{
  $this->CI->db->cache_on();
}
else
{
  $this->CI->db->cache_off();
}
$query = $this->CI->db->query($query);


Any help/insight is greatly appreciated!
#2

[eluser]yacman[/eluser]
Please tell us more about what isn't working?

What is your expected result? How do you know that caching isn't working? The end result should be the query results.

Also, you don't have the sql posted, there are some sql functions that cannot be cached.

Please add some more detail to help diagnose.
#3

[eluser]tinawina[/eluser]
Database cache is working -- I'm getting a ton of cache files. But what I want to get is database cache files only for pages viewed from a certain controller. For example, here are three URLs:
Code:
http://mysite.org/publications
http://mysite.org/authors
http://mysite.org/topics
What we want are cache files for topics only. So when someone clicks on http://mysite.org/topics/education, a cache file is created just for that topic. At this point our cache folder should have only one sub-folder, "topics+education". When someone clicks on http://mysite.org/topics/employment_and_labor a second file, "topics+employment_and_labor", is generated. But when they click on http://mysite.org/authors/terry_cloth there should be no database cache file created. Right now, "authors+terry_cloth" is generated.

The way that our site is set up, all queries funnel through one library that generates the query we need according to the controller and subsequent segments in the URL. That's why I'm able to discern in my code which query needs to run. According to the user guide you can turn database caching on or off as needed on a query by query basis. That's why I set up the if/else to work as described in my original post. Theoretically it seems it should work, it just doesn't.

Thanks for your help!




Theme © iAndrew 2016 - Forum software by © MyBB