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

Cache.set

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

Description

Set a value for ther given $key, to be expired after $duration.

$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:

fun 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