Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. public function all()
  2. {
  3. if (Cache::has('bus_stops')) {
  4. return Cache::get('bus_stops');
  5. }
  6.  
  7. return $this->cacheBusStops();
  8. }
  9.  
  10. public function create(...)
  11. {
  12. // do the creation...
  13.  
  14. $this->cacheBusStops();
  15. // or
  16. event(new BusStopCreated($busStop));
  17. }
  18.  
  19. public function update(...)
  20. {
  21. // do the updating...
  22.  
  23. $this->cacheBusStops();
  24.  
  25. // or
  26.  
  27. event(new BusStopUpdated($busStop));
  28. }
  29.  
  30. // either inside the repository for now
  31. // or put inside a Listener maybe CacheBusStopsListener
  32. protected function cacheBusStops()
  33. {
  34. Cache::forever('bus_stops', BusStop::all());
  35.  
  36. // you can use this in the UI to show the last updated of your record
  37. Cache::forever(
  38. 'bus_stops_last_updated_at',
  39. (string) BusStop::orderBy('updated_at', 'desc')
  40. ->first()
  41. ->updated_at
  42. );
  43.  
  44. return $stops;
  45. }
  46.  
  47. EventServiceProvider
  48. ...
  49. $listens => [
  50. BusStopCreated::class => [CacheBusStopsListener::class],
  51. BusStopUpdated::class => [CacheBusStopsListener::class],
  52. ];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement