Performance in dynamic menus

Asked

Viewed 127 times

1

I’m building a cakephp and mysql application, and would like to display some menus and items dynamically through the database.

Better example: Display latest updates, latest entries in the database, menu with categories registered in the system, etc.

What happens:

-User accesses home, system requests menus from bank.

-User enters some link, and the system requests again to the database.

I understand that this way, every get on the site, it will in the database again, fetch this information to render the menus.

Doubt:

  • Is there any way to adjust so that the system only creates a request in the database in the user access, not overloading the database with selects to each link within the site?
  • Changing these requests to ajax would be a good solution?
  • Cakephp’s Cachehelper can help with this in some way?
  • 2

    Why, are these access to the bank causing slowness? And yes, caching with Cake mechanisms can be a good solution.

  • It’s actually not running yet, but in my modeling, I realized that with every get this would happen, I would re-order the menus and contents for the bank. This is normal practice?

  • I think this is common, but my focus is another: don’t worry about performance problems that don’t exist yet, IE, don’t waste time with premature optimizations.

  • 1

    I don’t know Cake but I would think about doing so: Create a self-referenced table, make a single select, mount the menu

  • This solution served me in this and another question: http://answall.com/questions/21154/passar-vari%C3%A1veis-do-appcontroller-para-Elements-no-cakephp

1 answer

2

Only request from the database

The simplest way to do this in php is to load the menus differently, for example: save the array of menus in a file serialized in json, every time the site is loaded you read only this file, if this file does not exist you make a database select and create it. useful functions: json_encode(), json_decode, file_put_contents(), file_get_contents().

The performance increases a lot and if you want to modify the menu, just change the bank and delete the file.

Requests in Ajax

You can also load the menu in ajax and keep select persistent to each bank request, this does not increase the final result speed but will make the page load faster although the menu may have a load delay.

They are just tips, but in short: yes you can leave faster.

"Only do a database search when really necessary, use cache and indexing whenever possible"

  • 1

    I thought about it too, setting up a cronjob to delete the json periodically.

  • 1

    taking advantage of the comment of our friend @bfavaretto, this same stream is the implementation of the cache of cake php, you can find caching functions ready in it, which simplifies this process even more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.