erikbooij / cache-scheduler
A simple way to get the most out of your cache TTL.
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^7.0
- vimeo/psalm: ^3.0
This package is auto-updated.
Last update: 2025-04-30 02:04:37 UTC
README
This Cache Scheduler is a library that allows you to vary your cache TTLs according to a self defined schedule. I've written a little blog post on why you might want to consider that.
Usage is simple:
-
install it with Composer:
$ composer require erikbooij/cache-scheduler
-
Create a schedule and scheduler:
$schedule = (new Schedule) ->requireUpToDateDataFrom(Schedule::MON, 8, 0) ->allowStaleDataFrom(Schedule::MON, 17, 30) ->requireUpToDateDataFrom(Schedule::TUE, 8, 0) ->allowStaleDataFrom(Schedule::TUE, 17, 30) ->requireUpToDateDataFrom(Schedule::WED, 8, 0) ->allowStaleDataFrom(Schedule::WED, 17, 30) ->requireUpToDateDataFrom(Schedule::THU, 8, 0) ->allowStaleDataFrom(Schedule::THU, 17, 30) ->requireUpToDateDataFrom(Schedule::FRI, 8, 0) ->allowStaleDataFrom(Schedule::FRI, 17, 30); // Create the scheduler, passing it a SystemClock instance to interface with system time $scheduler = (new Scheduler(new SystemClock)) ->setSchedule($schedule) ->setExpirationSpread(ExpirationSpread::minutes(30));
-
Use the scheduler to determine the allowed lifespan of your item in cache:
$cache->set('cache-key', 'cache-value', $scheduler->calculateTimeToLive(3600));
The number passed as the first argument to ->calculateTimeToLive()
is the cache TTL if your data is currently require to be up-to-date (the default TTL).
Instead of attaching the schedule and expiration spread to the scheduler as in the example, you can also pass that as the second and third argument respectively to ->calculateTimeToLive()
. If you use both, the method arguments will override the values attached to the scheduler.