HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Some questions about cache and model

class.sqldriver has a function cache() that is used in only a very few occasions (discussion model/controller and tagging plugin). If I read it correct, you use it like that

...
$value = $sql->select('column')
    ->cache($someKey)
    ->from('table')
...

with the benefit that the query result is fetched from cache if the cache key exists.

Is it correct that this is a shortcut for

$value = Gdn::cache()->get($someKey);
if ($value === Gdn_Cache::CACHEOP_FAILURE) {
    $value = $sql->select('column')
        ->from('table')
    Gdn::cache()->store($someKey, $value)
}

...?

While this would be a handy shortcut, it is much slower to build the query by creating a db object instead of only building it if cache is not set. Could that be a reason why this function is used so sparsely?

And if it is slower, shouldn't DiscussionModel and TagModule use the faster way to access cached data?

I stumbled upon that function because I haven't used the cache by now. And I guess many hobbyists like me did not take advantage of the caching features in Vanilla. I was looking at the Gdn_Model to see how caching is implemented and found out that it is not used there at all. Is this a design decision?

Wouldn't it be good to use caching right in the model for at least getID and getCount? I'd be happy to create a pull request for that!

Comments

  • peregrineperegrine MVP
    edited August 2015

    An item in the developer documentation and a tutorial in tutorials category on the ins and outs of coding with caching, fine points, intro, and how to use in plugins would be nice too!

    Illustrating how staff developed guide on caching with vanilla would be advantageous for pull requests O.o

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

Sign In or Register to comment.