java mvc全局变量,Spring MVC中的全局变量

I have created a spring mvc web application. My menus are database i.e they are stored in the databases. Whenever I call index.jsp which is my home page from a different browser, the menu items are retrieved every time from database. How can I stored the menu items in a global variable which can be accessed anytime without hitting the database again or until I restart the server.

解决方案

You requirement can be sufficed in 2 ways:

Server cache:

Use some open source server cache library like ehcache which lets you store data (you app data or data retrieved from database or data received from some 3rd party app call) in server memory. Idea is that instead of performing same 3rd party call which is expected to give same result, do it once and then store it in server cache.

Ehcache works well with Spring framework, and in fact we use ehcache for server side caching for our enterprise application which is based on Spring framework.

Browser database storage:

HTML5 provide IndexedDB API which lets you store data in browser database.

So, you can retrieve the data from server once and then store it in local database, then whenever you need data you can first check if it is present in local database or not, if not then retrieve and store in local db, else fetch from local db.

Advantage of this approach is that you can save even the server call, so lesser network traffic.

Depending on you requirement, you can choose one option.

I would NOT recommend storing data, which is retrieved from database, in global variables. It is really not a scalable approach.

You can find plenty of online tutorials, but 1 would suggest in each case:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值