Friday, February 28, 2014

cache read pattern

The basic cache read/get pattern.

// no lock on cache read, lock on cache write
get(key):
    t = null
    // get from cache
    t = cache.get(key)
    if t == null:
        lock(_lock):
            // check cache again after locking
            t = cache.get(key)
            if t == null:
                t = getTimeConsuming(key)
                // insert into cache only if not null
                if t != null:
                    cache.insert(key, t)
    return t

No comments:

Post a Comment