hapijs / catbox

Multi-strategy object caching service
Other
494 stars 72 forks source link

Dynamic staleIn, initial idea #96

Closed jardakotesovec closed 10 years ago

jardakotesovec commented 10 years ago

This should illustrate good enough, what I had in mind for #94 . Let me know if you think it has good direction and I will work on tests, docs and also on options tests.

hueniverse commented 10 years ago

Can you give me an example of such staleIn function?

jardakotesovec commented 10 years ago
function (stored, ttl) {

    var expiresIn = (Date.now() - stored) + ttl;
    return 0.8 * expiresIn;
}
hueniverse commented 10 years ago

Typically, the purpose of staleIn is to refresh the cache while still able to serve stale data, so that there is minimal hit once the item expires. Basically always have valid data in the cache. Why can't you use a negative staleIn value that is deducted from ttl? If you set it to the time it should take to refresh, it will have the same effect.

jardakotesovec commented 10 years ago

Reasoning behind my request of such dynamic function is that I have wide range of ttl. Lets say its from 1 minute up to 1 hour. Function above would provide staleIn for edge cases 48seconds and 48 minutes.

With negative staleIn I could have value like -15 seconds, which would work fine if there is enough traffic. Otherwise having 48 minutes staleIn has benefit of be more likely to be hit during 12 minutes and update itself before expires. And generally I don't have to worry about having staleIn value that will work reasonably good for whole range.

Does it make sense?

But I agree that negative staleIn would mostly suffice. I guess you would prefer negative staleIn for this purpose? Just wondering if its because it does not make API (docs) more complicated and people won't implement wrong staleIn functions and complain about it? :-) Or you actually think that having function would not be beneficial.

hueniverse commented 10 years ago

It's just added complexity that we will need to carry with us moving forward. Are you using catbox directly or via hapi?

jardakotesovec commented 10 years ago

I would like to use it via server methods, but if dynamic/negative staleIn won't be option, I will dynamically generate policies to cover different expiresIn with suitable staleIn, in this case I would use it outside of hapi.

Since Policy already support dynamic ttl, I think it would make sense to make this dynamic ttl work nicely with staleIn option and have Catbox functionality more complete. Actually this clever caching with staleIn and staleTimeout attract my attention when I was exploring available frameworks, because its great fit for app I am working on.

I still vote for staleIn function, because it covers both use cases:

relative staleIn to expiresIn:

function (stored, ttl) {

    var expiresIn = (Date.now() - stored) + ttl;
    return 0.8 * expiresIn;
}

and constant 'negative' staleIn:

function (stored, ttl) {

    var expiresIn = (Date.now() - stored) + ttl;
    return expiresIn - 1000;
}

Implementation inside Policy is very simple, it does not make code less obvious. Just testing options parameter will be more hassle.

From user point of view, its consistent - its just function that returns stalesIn. In case of negative staleIn, it is not really 'stale in'. It has different meaning.

So let me know what you think, you have the last word here.

hueniverse commented 10 years ago

Fix coverage and docs and I'll merge.

jardakotesovec commented 10 years ago

replace by #105

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.