Skip to main content
Topic: new cache style (Read 6850 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

new cache style

idea: http://laravel.com/docs/cache#cache-usage

Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist.

 
Code: [Select]
    function cache_remember_data($key, $minutes, $default)
    {
        $value = cache_get_data($key);

        if (null === $value) {
            $value = is_callable($default) ? $default() : $default;
            $minutes = intval($minutes) * 60;
            cache_put_data($key, $value, $minutes);
        }

        return $value;
    }

Example:

Code: [Select]
$value = cache_remember_data('my_key', $minutes, function()
{
    ... code ...
   return ...
});



still it is possible to add value by default for cache_get_data:

# Retrieving An Item Or Returning A Default Value
cache_get_data($key, $default, $sec = 120)

Example:

Code: [Select]
$value = cache_get_data('key', function() { return 'hello'; });


Last Edit: December 25, 2013, 11:41:15 am by Inter
Sorry for my English

Re: new function cache_remember_data

Reply #1

I think what you are proposing is basically cache_quick_get (i.e. a cache function with a callback function).
Am I wrong?
Bugs creator.
Features destroyer.
Template killer.

Re: new function cache_remember_data

Reply #2

oops

you are right they are similar

but my function resolves syntax of anonymous functions closings which appeared in php5.3 and less checks and the file require isn't mandatory
Sorry for my English

Re: new function cache_remember_data

Reply #3

Pretty much every cache use will then require creating a closure basically for the sake of creating a closure. Otherwise you're caching something you already have generated inline in the first place.

cache_quick_get isn't used everywhere, for a reason.

Re: new function cache_remember_data

Reply #4

isn't present less code turns out

the code becomes more beautiful and more clear (this my personal judgement)
Code: [Select]
<?php

# old style
function getCategoryList()
{
    global $txt, $smcFunc;

    if (!($cats = cache_get_data('cats_list'))) {
        $cats = [];

        $q = $smcFunc['db_query']('', '
            SELECT cat.id, cat.name, ...',
            []
        );
        if (!$smcFunc['db_num_rows']($q))
            return [];

        while ($row = $smcFunc['db_fetch_assoc']($q)) {
            $cats[$row['id']] = $row;
        }
        $smcFunc['db_free_result']($q);

        cache_put_data('cats_list', $cats, 1800);
    }

   return $cats;
}


# new style
function getMyCats2()
{
    return cache_remember_data('cats_list', 30, function()
    {
        global $txt, $smcFunc;

        $cats = [];

        $q = $smcFunc['db_query']('', '
            SELECT c.id, c.name, ...',
            []
        );
        if (!$smcFunc['db_num_rows']($q))
            return [];

        while ($row = $smcFunc['db_fetch_assoc']($q)) {
            $cats[$row['id']] = $row;
        }
        $smcFunc['db_free_result']($q);

        return $cats;
    });
}
Sorry for my English

Re: new function cache_remember_data

Reply #5

Well done, you've saved, what, 4 lines of code?

The truly sad part is that you're actually going to use more memory and possibly be slower as well (though it's only an issue inside loops) doing it that way.

Re: new function cache_remember_data

Reply #6

He's also using a php 5.4 feature. The array shortcut ;)

Re: new function cache_remember_data

Reply #7

Meh, I hadn't even noticed that. That's what I get from switching between C#, PHP and JavaScript on a regular basis.

Re: new function cache_remember_data

Reply #8

cache_remember_data too long name

may be better to combine these functions into a separate class?

that you think about:

// cache_get_data
Cache::get

// cache_put_data
Cache::put

// cache_remember_data
Cache::rise
Sorry for my English

Re: new function cache_remember_data

Reply #9

The class could be an option, though it would require quite a bit of changes (at least string replace :P).
Maybe we can discuss for 1.1, other opinions?
Bugs creator.
Features destroyer.
Template killer.

Re: new function cache_remember_data

Reply #10

No changes - top class native cache - a forum
example:

[spoiler=Cache class (there may be errors)]
Code: [Select]
<?php

namespace Inter;

class Cache
{
    public static function get($key, $default = null)
    {
        $key = self::prepareKey($key);
        $value = cache_get_data($key);
        if (null === $value && null !== $default) {
            $value = is_callable($default) ? $default() : $default;
        }

        return $value;
    }

    public static function set($key, $data, $minutes = 4)
    {
        $key = self::prepareKey($key);
        $sec = floatval($minutes) * 60;
        cache_put_data($key, $data, $sec);
    }

    public static function rise($key, $minutes, $default)
    {
        $key = self::prepareKey($key);
        $value = cache_get_data($key);
        if (null === $value) {
            $value = is_callable($default) ? $default() : $default;
            $sec = floatval($minutes) * 60;
            cache_put_data($key, $value, $sec);
        }

        return $value;
    }

    public static function del($key)
    {
        $key = self::prepareKey($key);
        cache_put_data($key, null, 0);
    }

    public static function flush()
    {
        clean_cache();
    }

    public static function prepareKey($key)
    {
        global $user_info;

        $groups = implode('', $user_info['groups']);

        return str_replace('{{groups}}', $groups, $key);
    }
}
[/spoiler]
Last Edit: December 08, 2013, 09:27:16 am by Inter
Sorry for my English