Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function all()
- {
- if (Cache::has('bus_stops')) {
- return Cache::get('bus_stops');
- }
- return $this->cacheBusStops();
- }
- public function create(...)
- {
- // do the creation...
- $this->cacheBusStops();
- // or
- event(new BusStopCreated($busStop));
- }
- public function update(...)
- {
- // do the updating...
- $this->cacheBusStops();
- // or
- event(new BusStopUpdated($busStop));
- }
- // either inside the repository for now
- // or put inside a Listener maybe CacheBusStopsListener
- protected function cacheBusStops()
- {
- Cache::forever('bus_stops', BusStop::all());
- // you can use this in the UI to show the last updated of your record
- Cache::forever(
- 'bus_stops_last_updated_at',
- (string) BusStop::orderBy('updated_at', 'desc')
- ->first()
- ->updated_at
- );
- return $stops;
- }
- EventServiceProvider
- ...
- $listens => [
- BusStopCreated::class => [CacheBusStopsListener::class],
- BusStopUpdated::class => [CacheBusStopsListener::class],
- ];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement