whitecube / nova-page

Static pages content management for Laravel Nova
https://whitecube.github.io/nova-page
MIT License
238 stars 41 forks source link

Caching feature (or documentation) #13

Open jahsome opened 5 years ago

jahsome commented 5 years ago

Hi there!

This package looks terrific -- kudos for putting it out.

Have you thought about supporting caching? Something where we could set a default TTL in a config would be fantastic. On the Nova end it would have to bust that cache up update as well.

If it's possible to implement this manually, e.g. at the controller level, could you please let me know how? I planned to cook it up myself and submit a PR with documentation, but at first glance through the source and documentation, I couldn't come up with a way to do it.

einardivision commented 5 years ago

This package saves the data in disk so that by itself is kind of cache, since it only has to find it in a file rather then do a database query.

One way i see of cacheing the results giving by this package is to edit it a little bit. Go to src/Pages/Manager.php and define a new function: public function getCurrent() { return $this->current; }

Then all you need to do to get the attributes from the specific site is to: for example if we have this route "/trainers" and we need to pass in the request and a instance of the manager class you changed.

Then you can do this in your routes file (web.php)

Route::get('/', function (Request $request, Manager $page) {
   // My template name is home
    $page->load('home');
    // These are the attributes for this page
    $values = $page->getCurrent()->attributes();
   // Then you can cache it for 60 minutes(using Cache facade for simplicity)
    Cache::put('homepage', $values, 60)
   // Some logic to check the cache, etc and then return it
    return view('index')->with('values', $values)
});

This is a shit mix kind of, dont use this in production

There is a better way of doing this and that would be to initialize the Manager with the variables you stored in cache. Since when you do $page->load('home') you really dont need to pass anything into the view, you can just access it with {{ Page::get('key') }}

If you want help to figure this out, let me know.

this is just knowledge i gathered from using this package, i didnt make it :+1:

wimurk commented 5 years ago

@jake-harris caching the view output is a bad idea. Caching your database results is way more easier in your controller.

$users = cache()->remember('users', 120, function(){
   return User::all();
});

return view()->with(compact('users'));
jahsome commented 5 years ago

@wimurk I'm sorry if I'm dense, but I don't see how that's relevant in the scope of this discussion.

There's nothing to cache from the database because it's not pulling it from the database, rather it's accessing the contents of a file. Ideally I'd like to cache the contents of that file, so it's not a disk IO every request on high-volume pages, e.g. the Home page.

jahsome commented 5 years ago

And to clarify, I'm not asking about how to cache, rather, I don't see a way to "grab" the values with the API of Page/Manager, although @einardivision seems to be on the right track with his example. I'll see if I can play with that.

toonvandenbos commented 5 years ago

Hi there,

Caching would indeed be useful when the cache driver is set to anything else than file (or even database), in which case there would be hardly no difference.

I see two options:

I would be glad to discuss this further before accepting PRs or implementing it myself.

Cheers!

raphcollective commented 5 years ago

This might be more relevant with the database source now