Jump to content

[SOLVED] md5 and caching keys


scottybwoy

Recommended Posts

I'm using a script written by someone else that uses a caching key to determine whether to run part of a script again.

 

It takes the original value that is to be processed, then stores it to determine if it needs to process that same original value again.

 

Here's the code striped to the relevant parts :

foreach($categories as $c) {
$c = trim($c);

$subcats = explode($config['csv']['categories_subcat_delimiter'], $c);
	foreach($subcats as $k => $v) {
		if (!empty($v)) { $subcats[$k] = trim($v); }
	}

	$key = md5(strtolower(implode($config['csv']['categories_subcat_delimiter'], $subcats)));
	if (!isset($categories_cache[$key])) {
		$c_parent_id = 0;
		$category_language = 1

		$c_error = false;
		foreach ($subcats as $sc) {
			$subcat_query = tep_db_query("SELECT c.categories_id FROM " . TABLE_CATEGORIES . " c INNER JOIN " . TABLE_CATEGORIES_DESCRIPTION . " cd ON c.categories_id = cd.categories_id WHERE language_id = '$category_language' AND categories_name = '$sc' AND parent_id = '$c_parent_id'");
			if ($subcat_query != null && tep_db_num_rows($subcat_query) == 1) {
				$fetched = tep_db_fetch_array($subcat_query);
				$c_parent_id = (int)$fetched['categories_id'];
			}	elseif ((bool)$config['settings']['auto_add_categories']) {
				$m_fields = array();
				$m_values = array();

				$m_fields[] = "`parent_id`";
				$m_values[] = "'" . (int)$c_parent_id . "'";

				$m_fields[] = "`date_added`";
				$m_values[] = "'".strftime('%Y-%m-%d %H:%M:%S',time())."'";

				$new_category_query = 'INSERT INTO ' . TABLE_CATEGORIES . ' ('.implode(',',$m_fields).') VALUES('.implode(',',$m_values).')';

				if(!tep_db_query($new_category_query)) {
					csv_import_message(sprintf(CSV_CATEGORY_INSERT_ERROR, $i, $sc), 'error');
					$skipped[$i] = $data[$i];
					continue;
				}

				$c_parent_id = tep_db_insert_id();

				foreach($languages_ids as $lang => $lang_id) {
					$m_fields = array(
						'`categories_id`',
						'`language_id`',
						'`categories_name`',
					);

					$m_values = array(
						"'$c_parent_id'",
						"'$lang_id'",
						"'$sc'",
					);

					$new_category_language_query = tep_db_query('INSERT INTO ' . TABLE_CATEGORIES_DESCRIPTION . ' ('.implode(',',$m_fields).') VALUES('.implode(',',$m_values).')');
				}
			} else {
				$c_error = true;
				break;
			}
		}

		if($c_error) { continue; }

		$categories_cache[$key] = $c_parent_id;
	}

	$cId[] = $categories_cache[$key];
}
}

 

Now I suppose my real question is, whether the md5 hash is relevant for the $categories_cache[$key];

 

Two of the values I have for $c in the foreach loop are "Processors|AMD|AM2" and "Processors|AMD|AM2+"

 

When this is md5 hashed it returns the wrong parent_id and so alters the expected functionality of the rest of the script.  Could this be down to md5 skipping the "+" symbol at the end?  Can I just use "Processors|AMD|AM2+" as the $key value?

Link to comment
Share on other sites

OK, So my problem doesn't lie there.  I have established it by trying it out too.  But I have got closer.

 

The caching code only works with consecutive categories in a row.  i.e. "Processors", "AMD", "AM2" etc.

I've also noticed that the values of $c already have this broken down, so the values of $c are just : "Processors" or "AMD" or "AM2".

But when another sub category comes into play, i.e. "AM2+" it has no way of knowing what it's parent should be, in this case : "AMD".

 

Can anyone look over the code in the first post, to come up with any ideas of how to retain that information in the cache so that it the id can be either found or not found for the rest of the script?

 

Otherwise I think I'll have to take the cache out, which will slow things down, especially as there will be lots of products.  Thanks

Link to comment
Share on other sites

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.