How to use generateFunc, as seen below?
server.cache({
expiresIn: 1000*60*60,
segment: 'test',
generateFunc: function(key,next){}
});
I know generateFunc is used to generate a new cache item if one is not found in the cache when calling get() - straight from the api docs. However, I am still not sure how to use get() with generateFunc()
if I'm using the following in a class object, how can I reference the class variables from the constructor from within generateFunc?
class Test { constructor(){ this.testVar = 'hello'; } register(server) { this.server = server; this.testCache = server.cache({ expiresIn: 1000*60*60, segment: 'test', generateFunc: function(key,next){} }); } }
I don't understand the flow. I need a simple example outline with explanations, but I cannot find any good resources.
From the Documentation:
generateFunc (id, next) - a function used to generate a new cache item if one is not found in the cache when calling get().
- id - the id string or object provided to the get() method.
- next - the method called when the new item is returned with the signature function(err, value, ttl) where:
- err - an error condition.
- value - the new value generated.
- ttl - the cache ttl value in milliseconds. Set to 0 to skip storing in the cache. Defaults to the cache global policy.
get (id, callback), where:
id - the unique item identifier.
callback , function(err, value, cached, report), where:
- err - any errors encountered.
- value - the fetched or generated value.
cached - null if a valid item was not found in the cache, or an object with the following keys:
- item - the cached value.
- stored - the timestamp when the item was stored in the cache.
- ttl - the cache ttl value for the record.
- isStale - true if the item is stale.
set (id, value, ttl, callback), where:
- id - the unique item identifier
- value - the string or object value to be stored.
- ttl - a time-to-live value in milliseconds after which the item is automatically removed from the cache (or is marked invalid).
- callback - a function with the signature function(err).
Answers on your questions may be found in the docs, sources and through experimentation: http://hapijs.com/tutorials/caching
https://github.com/hapijs/catbox
generateFunc
's callback, then catbox handles (err, value)
, then it calls get
's callback. ttl
"Defaults to the cache global policy" . I guess expiresIn
is that policy. set
from generateFunc
, pass value to catbox via next
and it will store it for you. this
. I haven't tested the code: class Test {
constructor(){
this.testVar = 'hello';
}
register(server) {
this.server = server;
const self = this;
this.testCache = server.cache({
expiresIn: 1000*60*60,
segment: 'test',
// Old trick
generateFunc: function(key,next){ console.log(self.testVar); },
// or new trick
generateFunc: (key,next) => console.log(this.testVar) // Read about arrow functions
// or
generateFunc: this._cacheGenerateFunc.bind(this),
});
}
_cacheGenerateFunc(id, next) {/* Your code */}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.