MyBB Community Forums

Full Version: Best way to delete a cache
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
my plugin uses the internal cache system of MyBB. It creates a new cache called "foobar". It's no problem to create this cache:

$array = array('a', 'b', 'c', 'd');
$cache->update('foobar', $array);

This new cache "foobar" is listed in Tools & Maintenance > Cache Manager and I can work with it.

But I have a real problem: How can I delete this cache (regardless which cache storage is used)??

I can empty the cache:

$cache->update('foobar', false);

But this is not a good solution, because it still stays there... Sad
not tested

if(is_object($cache->handler))
{
    $cache->handler->delete('foobar');
}

// There is always a copy in the database.
$db->delete_query("datacache", "title='foobar'");

there's nothing in MyBB that actually uses the delete functions of the cache handlers, so you should test whether those actually work too

should probably be done by the cache class itself as a $cache->delete("foobar")
If anyone's using this, I just noticed there's always a copy in the database (doh!) so the else {} has to go.

EDIT: fixed the code snippet above
(2010-12-10, 12:43 PM)frostschutz Wrote: [ -> ]If anyone's using this, I just noticed there's always a copy in the database (doh!) so the else {} has to go.

What do you mean? Sorry but I do not understand Toungue
I edited the code snippet. Silly me didn't think of that before... Blush
Oh I see what you thought. You thought that it either used database or Disk/Memcache/XCache/EAccelerator, right? Toungue
Indeed. That's why you switch to another cache mechanism in the first place - to get some load off the database. But apparently the MyBB caches are meant to be updated only rarely... so it doesn't use the database while you read, but it always uses the database when you write.
Yeah it'll store a hard copy in the database so if you have to switch back to that, it'll automatically be there. Updating a cache doesn't happen on a constant basis, only when you're editing stuff in the ACP really.

This reminds me that I need to add this to two of my plugins; would be useful if there was a default method to do this.
(2010-12-11, 11:06 PM)MattRogowski Wrote: [ -> ]Yeah it'll store a hard copy in the database so if you have to switch back to that, it'll automatically be there.

If it was just about a one time switch, the caches could just be regenerated on demand then. It's actually used as a fallback when something is wrong with the other caches. For example, if the file cache has a file missing. It then queries the cache from the database, and tries to write the file again.

It's fine like that, I just didn't know it worked this way. It's not a bug, just a question of cache design and expectations. MyBB's cache is more like a persistent data storage whereas what I usually expect to be a cache is something more of a temporary nature. So the app has to be able to regenerate what it is caching on the fly.

MyBB apparently isn't able to do that, or just doesn't do that, and relies on the database instead, so that's why it needs to write every change into the database as well. Without it, the fallback wouldn't work and MyBB would break if caches went missing unexpectedly.