Jump to content

[Resolved] Clearing Cache Via Code?


lukejapanwalker

Recommended Posts

I am updating my product prices automatically using sql, but the prices do not change until the cache is cleared manually from the admin control panel.

Is there a way to clear the cache via script?

Right now I have a sql command hidden in the product description to update the price of the product, what code should I add after that to clear the cache right away?

Thank you for your help.

Luke.

Edited by lukejapanwalker
Resolved
Link to comment
Share on other sites

"Right now I have a sql command hidden in the product description to update the price of the product,"

That's interesting.

In code, you would use this statement to clear the cache of SQL queries:
$GLOBALS['cache']->clear('sql');

But, in CC6, while in admin, the cache is not enabled. Also, if your 'hidden' query uses CubeCart's Database class, an update() will clear the cache anyway.

Link to comment
Share on other sites

Hello bsmither,

Thank you for your reply,

I tried "$GLOBALS['cache']->clear('sql');" in my sql code for my description and nothing changed.

do you know what might be wrong? I'm sure there's probably something I'm just not understanding but I think I just need a way to clear the cache each time someone clicks on a product or something like that.

Thanks again for your help.

Luke.

Link to comment
Share on other sites

You can tell CubeCart just to not use the cache at all. In admin, Store Settings, Advanced tab, Disable Caching.

"I tried "$GLOBALS['cache']->clear('sql');" in my sql code for my description."

Please tell us more about how you are putting this code where you say you are.

A product's description is not executed or evaluated. It is simply given to the template and displayed as is.

Link to comment
Share on other sites

Hello thanks for your reply.

Ok, in my description there is an iframe that links to a php file (located in the root directory of my store) to update the database, like below.

$link = mysqli_connect($servername, $username, $password, $my_db);

$sql = "UPDATE gwm_CubeCart_inventory SET price='99.00' WHERE product_id='".$proid."'";

if ($link->query($sql) === TRUE) {
    echo "New record created successfully";
    //echo "O";
} else {
    echo "Error: " . $sql . "<br>" . $link->error;
    //echo "X";
}

But the price of the item only refreshes when I clear the cache.

I tried admin, Store Settings, Advanced tab, Enable Caching >> selected disable from the drop down menu and hit save.
It worked once but after running the sql code above it doesnt refresh anymore.

I check to make sure it change by looking in phpmyadmin and it did change there but not in my store.

Thanks
Luke.

Link to comment
Share on other sites

The PHP script being called by the iframe is using PHP's own functions to connect - not CubeCart's database class. So the cache is not involved at all. In fact, no part of CubeCart is involved at all.

So, for the moment, until I see more of what you are doing, I find nothing wrong.

But, I now see that even though the iframe script makes a direct call to the database, CubeCart, on the next page request of that product, needs to pull the fresh data.

And that's where we have concerns about the cache.

Link to comment
Share on other sites

I just checked again and disabling "enable caching" under "Performance" works but ony once and I still have to do it manually, is there anything I can add after my SQL queiry to clear the cache after changing the price?

Based on what you said, I believe if I can only clear the cache it should work?

Cheers

Luke.

Edited by lukejapanwalker
Link to comment
Share on other sites

Please read https://forums.cubecart.com/topic/50008-problem-with-the-cache and specifically check your global.inc.php to see whether you have an entry defining which caching method is being used such as 

$glob['cache'] = 'file';
 
This is auto added during upgrades and then means that caching cannot ever be switched off from within store settings !!
 
Remove that entry and then you can control again whether cache is enabled or disabled but if you re-enable it and then have problems with memcache you will need to add the line back in again.  This is why I suggested specifically defining which caching  method is used within the Store Settings and not using the global.inc.php file at all
 
Ian
Link to comment
Share on other sites

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.   Paste as plain text instead

  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...