express写的文章管理系统,每个页面的菜单导航栏都是一样的,但是每个页面都从数据库去查询了一次所有的菜单。
怎么使整个网站只从数据库查询一次菜单,然后把数据给每个页面使用呢?
数据库用的 MongoDB
工具用的 mongoose
巴扎黑2017-04-26 09:03:39
I don’t knownode
, is it possible to load the menu data when it is started and put the menu data into global variables?
PHP中文网2017-04-26 09:03:39
Cache, is it useful for redis?
Or you bind it to app.locals
, check it, if there is a menu, just get it directly, if not, go to the database to query and bind it.
In this way, you can get it directly every time. Of course, if you update, the menu should also be updated on app.locals
.