0

Is it possible to find the seconds left / TTL when retrieving cache (when set to Redis) via plugins? I'm using short-lived cache (~600 seconds) to put users on "cooldown" to limit how often they can create new forum topics, by creating a user-specific cache whenever they create a topic. Then I can check and see if this cache exists when they create a new topic.

When getting the cache like:

$cache = craft()->cache->get( $cacheKey );

If it returns true (exists,) I'd like to be able to tell the user how long they need to wait until they can post again (like Reddit.)

I've tried methods like

$cache->ttl; // doesn't work; not an object
$cache['ttl']; // doesn't work, no 'ttl' key

Maybe I am missing something in the Cache Service Doc? Or is this meta information simply not available?

Any information would be awesome. Cheers!

  • Just shot them an email. Cheers! – taylor Jan 22 '16 at 20:52
1

Craft is piggybacking off of Yii's caching functionality for 99% of this and their cache classes don't expose any type of TTL functionality. My guess is that because of all the different caching providers they support (memcache, xCache, WinCache, ApcCache, etc.), it looks like Redis is the only one that has this sort of functionality.

There isn't a clean way for us to add this to Craft itself, so I think a proper fix would be to add it to Yii, but considering Yii 1 is in bug fix more and not getting any more enhancements and Yii 2 has dropped support for Redis, that seems unlikely.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.