Jump to content

Recommended Posts

I have a script with categories, it has thousands of categories and theres a page where it needs to build a list of all the children categories and it takes forever to list this. so i want to make a cache of each categories children.

 

I cant figure out how to traverse all the categories to make caches for them. anyone have any ideas of how to do this or know of any scripts that can do this?

If your script that generates the categories isn't 1000's of lines long can you post the relevant bits of it please?

 

It is better to optimise that first and if further optimisation / caching is required we can advise.

 

Also, how many lines of data in the relevant tables?

Well on the page where it gets the children the code is

 

function getsubtree($catsubtree, $i)
{
global $catlist, $DBPrefix, $system;
$query = "SELECT cat_id FROM " . $DBPrefix . "categories WHERE parent_id = " . $catsubtree[$i];
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
while ($row = mysql_fetch_assoc($res))
{
	$catlist[] = $row['cat_id'];
	$catsubtree[$i + 1] = $row['cat_id'];
	getsubtree($catsubtree, $i + 1);
}
}

$catsubtree[0] = $id;
$catlist = array();
$catlist[] = $catsubtree[0];
getsubtree($catsubtree, 0);
$catalist = join(',', $catlist);

 

and that gets the children for the one category your currently looking at. And there's about 12,000 lines in the table. Running on my localhost it takes about 50 seconds to make the list.

Categories are rarely updated.

This is wut Ive done.

I create a cache file with the array serialized

on the category editor, is when I build this cache file.

 

Yeah, it may take 50 secs to generate the list

but it shud take under a sec to read a file/unserialized the generated list.

 

 

 

 

Categories are rarely updated.

This is wut Ive done.

I create a cache file with the array serialized

on the category editor, is when I build this cache file.

 

Yeah, it may take 50 secs to generate the list

but it shud take under a sec to read a file/unserialized the generated list.

 

this is a good way of doing it. another way is to cache the subtree right in the mysql table. so, create a TEXT field in your categories table to store all the subtree info, and create a function that gets run whenever a category is changed. put a serialized version of the array in that field

this is a good way of doing it. another way is to cache the subtree right in the mysql table. so, create a TEXT field in your categories table to store all the subtree info, and create a function that gets run whenever a category is changed. put a serialized version of the array in that field

 

Ya know I never thought of using the MYSQL for cache storage, this is an excellant idea.

The problem of using files as storage, is the permissions, so ya either have to create an empty file and chmod it 0777 or create an empty folder chmod. Both systems have their pros and cons.

 

But using a db, yer not worried about the permissions, ya can store it into a simple table like:

CREATE TABLE cache {
   id INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
   name VARCHAR(30) NOT NULL,
   cache TEXT
}

 

or similar, I really do like this idea rhodesa, cant believe didnt think about doing it before.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.