Spring MVC中的全局变量 [英] Global Variables in spring mvc

查看:543
本文介绍了Spring MVC中的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Spring MVC Web应用程序.我的菜单是数据库,即它们存储在数据库中.每当我从其他浏览器调用主页的index.jsp时,每次都会从数据库中检索菜单项.如何将菜单项存储在一个全局变量中,该变量可以在任何时间访问,而无需再次访问数据库或直到重新启动服务器.

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:

  1. 服务器缓存:
    使用某些开放源代码服务器缓存库,例如 ehcache 来自第三方的应用调用)在服务器内存中.想法是 与其执行预期会产生相同结果的相同第三方通话,而不是执行一次,然后将其存储在服务器缓存中. .
    Ehcache与Spring框架配合良好,实际上,我们将ehcache用于基于Spring框架的企业应用程序的服务器端缓存.

  1. 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.

浏览器数据库存储:
HTML5提供了 IndexedDB API ,可将数据存储在浏览器数据库中.
因此,您可以一次从服务器检索数据,然后将其存储在本地数据库中,然后每当需要数据时,您都可以先检查其是否存在于本地数据库中,如果不存在,则可以检索并存储在本地db中,否则可以从中获取本地数据库.
这种方法的优点是您甚至可以保存服务器呼叫,从而减少网络流量.

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.

您可以找到很多在线教程,但每种情况下建议1:

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

  • ehcache
  • IndexedDB

这篇关于Spring MVC中的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆