vapor/vapor

Documentation for cache

henrypratt opened this issue · 5 comments

I can't seem to find any vapor 4 documentation for cache. Is it deprecated?

mkll commented

No it's not deprecated.

See https://github.com/vapor/vapor/releases?q=cache&expanded=true

Configure application cache

app.caches.use(.memory)

Set cached value

app.cache.set("foo", to: "mads")
app.cache.set("foo", to: "mads", expiresIn: .seconds(30))

Get value from cache

app.cache.get("foo", as: String.self)

Use in request handler

req.cache.get("foo", as: String.self)

(Includes .memory cache which stores values in a thread-safe dictionary)

Thank you! Any notes on the time complexity? Is this just a wrapper around NSCache?

mkll commented

@henrypratt Sorry, I have no idea about the internals, I didn’t look closely at the code.

mkll commented

@henrypratt You might be interested in a cache with a fixed capacity and an LRU algorithm:
https://github.com/m-barthelemy/VaporLRUMemoryCache

vzsg commented

Thank you! Any notes on the time complexity? Is this just a wrapper around NSCache?

No, NSCache is not involved (if it's even implemented by swift-corelibs-foundation, I don't know...)
Vapor's MemoryCache is implemented from scratch here: MemoryCache.swift.