Framework: [Feature] Get expiration time of a single cache item

Created on 30 May 2016  路  8Comments  路  Source: laravel/framework

While developing a high-loading project, I need to know the date when cache will be expired, so I could refresh its value using background jobs before the user requests it.

The framework doesn't provide information about the expiration of a cache item, so I would like to propose to create a method to implement it so, the method will return Carbon instance of expiration time.

$expiresAt = app('cache')->expiresAt('some_cache_id');

I could implement it for different cache drivers if this feature will helpful for others too.

Most helpful comment

This would be a super useful feature.

All 8 comments

Thanks, vt this isn't possible to easily implement everywhere. See our rate limiter class in the cache component for example. It has to implement expiry on top, because it's not possible natively.

@GrahamCampbell, the limiter class does not contains any information about the cache lifetime cause it was not intended to.

To implement the lifetime saving feature in every driver we could easily use array on top of the cache item like the Illuminate\Cache\FileStore does. So every cache item will look like this:

 $item = ['value' => $value, 'time' => $time]

Did this ever get anywhere? Or was there any similar proposal accepted?

This would be a super useful feature.

I have a need for this feature, exactly as @andrii-androshchuk stated.

Has anyone solved this elegantly? Share your pointers! I can only think of driver-specific hacks. :hammer:

@GrahamCampbell, a few more people have asked about this feature. Maybe it makes sense to provide it?

Have this ever been implemented? :-) Although, this might lead to a practice of using cache to store actual data, instead of just caching it...

Throwing my hat into the "I have a use case" ring, Although I realised miday through writing this up that I had an alternative to address my use case, so have included below incase anyone else winds up here with the same problem.

馃洜 Use Case

The component I am testing currently sets the cache duration dynamically based on application configuration for variable caching rates.

馃挃 Issue

I can't currently test these durations are set across an array of methods that make use of the facility, and as such I cannot guarantee resources are being cached appropriately.

馃 Alternatives

  1. Query the driver directly

    • i.e. directly query TTL {key} for a Redis instance

    • I would need a test for _every_ possible queue driver

    • These tests should be implementation agnostic

  2. Manually delay tests

    • i.e. sleep within a function until an expiration is expected

    • This feels extremely hacky

    • This will slow down my test suite significantly, especially as minimum units is s not ms!

  3. Mock Facade Methods 鉁旓笍

    • Replace Cache::put etc. with my own Mocks

    • Test that they are called with expected values

    • This is the best option I see here and what I have settled on

    • Defers implementation/correct function of delay to Cache

Was this page helpful?
0 / 5 - 0 ratings

Related issues

digirew picture digirew  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

kerbylav picture kerbylav  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments