THT is in early Beta. We welcome contributors & feedback.

Cache.set

Cache.set(key, value, expireSecs)

Description

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

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

Tip: Use the Date module to create an easy-to-read expiration value.

Full example:

function getPostsForUser(userId) {

    let key = 'posts:' ~ userId;

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

    // e.g. get posts from database
    let sql = L'select * from posts where userId = {}';
    sql.fill(userId);
    let posts = Db.selectRows(sql);

    Cache.set(key, posts, Date.hours(8)); 

    return posts;
}