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

Cache.set

Cache.set($key, $value, $duration) -> false

Description

Set a value for ther given $key, to be expired in $expireSecs seconds.

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

If $duration is set to 0, the key will never expire. It will need to be explicitly removed via Cache.delete.

Short example:

$userId = 1234
$prefs = { darkMode: true, sortOrder: 'desc' }

Cache.set('prefs:' ~ $userId, $prefs, '10 days'))

Full example:

fn getPostsForUser($userId) {

    $key = 'posts:' ~ $userId

    if Cache.has($key) {
        return Cache.get($key)
    }

    // The time-consuming operation to be cached
    $posts = getPostsFromDb($userId)

    Cache.set($key, $posts, '8 hours')

    return $posts
}

See Also