express寫(xiě)的文章管理系統(tǒng),每個(gè)頁(yè)面的菜單導(dǎo)航欄都是一樣的,但是每個(gè)頁(yè)面都從數(shù)據(jù)庫(kù)去查詢(xún)了一次所有的菜單。
怎么使整個(gè)網(wǎng)站只從數(shù)據(jù)庫(kù)查詢(xún)一次菜單,然后把數(shù)據(jù)給每個(gè)頁(yè)面使用呢?
數(shù)據(jù)庫(kù)用的 MongoDB
工具用的 mongoose
I don’t knownode
, is it possible to load the menu data when it is started and put the menu data into global variables?
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
.