Version: v0.7.1 - Beta.  We welcome contributors & feedback.

Cache.get

Cache.get($key, $default = '', $duration = '1 hour') -> any

Description

Get the cached data for the given string $key.

If the cached value does not exist, it will return $default.

// e.g. get forum posts for the given userId
$posts = Cache.get('posts:' ~ $userId, [])

Function Default

You will often want to calculate a new value for expired cache keys.

You can do this by passing a function as $default that calculates the new value, which will then be set in the cache for the given duration.

$duration can be a number of seconds, or a duration string.

Full example:

// Refresh cached list of forum posts every 8 hours
fn getPostsForUser($userId) {

    $key = 'posts:' ~ $userId

    return Cache.get($key, fn {

        // Time-consuming operation
        return getPostsFromDb($userId)

    }, '8 hours')
}

See Also