MVC 3 中基于缓存的全球化

发布于 2024-12-12 03:40:21 字数 301 浏览 0 评论 0 原文

我有一个 MVC 3 应用程序,其中所有文本都位于 SQL 数据库中。文本具有每种语言的名称和值。我想将文本存储在缓存对象中,并以某种方式根据一些会话变量将它们获取到我的视图和控制器中。

我的第一个解决方案是将对象 TextHandler 存储在缓存中。 TextHandler 具有所有文本和方法,可帮助按文本名称(作为字符串)和其他一些值提取文本。我还有一些控制器方法和帮助程序,用于根据会话变量从 TextHandler 获取文本。我对这个解决方案并不满意。

我找到了一些这方面的指南,但我真正需要的是一些关于如何做到这一点的指示,而不是实际的代码。

I have an MVC 3 application with all texts in a SQL database. The texts has names and values for each language. I want to store the texts in cache object and somehow get them in my views and in my controllers based on some session variables.

My first solution has been to store an object TextHandler in the Cache. TextHandler has all texts and methods to help extracting texts by their name (as a string) and by some other values. I also have some controller methods and helpers to get texts from TextHandler based on session variables. I'm not satisfied by this solution.

I've found some guides for this, but what I really need are some pointers on how to do it and not actual code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

年华零落成诗 2024-12-19 03:40:21

我想你可以将它们存储在内存缓存中(分布式或本地缓存,具体取决于你有多少种语言和字典值)。

您可以使用 languagecode|name 键,值是该语言中的单词。

例如,您可以将 us|header_name 作为键,将“My Header”作为值。

正如我上面提到的,这些值的存储位置取决于您的要求。如果你认为它们不属于本地内存,你可以随时设置一个 memcache 结构或一个 LRU 缓存,它保存经常使用的前 N ​​个最流行的值,如果有缓存未命中,它会转到数据库(包含所有语言的字典)查找给定的值,然后是名称和语言。

希望这会有所帮助。

i guess you could store them in an in-memory cache (either distributed or local depending on how many languages and dictionary values you have).

You could use a languagecode|name key and the value is the word in that language.

so you can have us|header_name as the key and "My Header" as the value for example.

As I mentioned above where these values can be stored depends on your requirements. If you think that they don't belong in the local memory you could always setup either a memcache structure or an LRU cache that holds the top N most popular values that get used a lot and if there is a cache miss it goes to the database (that contains your dictionary in all languages) to find the value given then name and language.

Hope this helps somehow.

_畞蕅 2024-12-19 03:40:21

一种常见的方法是在 url 上提供代码并用作控制器中的参数。然后,除了您在数据库中查询的任何字符串之外,还可以使用该语言代码。

如果您考虑资源文件,解决方案已经为您设置好了:
http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx

另一件事是根据浏览器请求设置的文化。使用该名称来查询数据库。区域性名称(来自 如何将 CultureInfo 传递给partialView以进行ajax调用?

public static string GetCurrentCulture()
{
   return Thread.CurrentThread.CurrentCulture.Name;
}

现在要具体讨论缓存,只需实现存储库模式即可。当您按“语言”和“字符串”查询时,请检查缓存中是否存在。如果是则返回,否则去数据库检索。这是使用存储库模式的一个很大的优点,缓存详细信息从调用者那里删除。如果您决定采用资源文件实现,那么无论如何都不需要这些,因为有内置的文化支持。

A common way is to supply the code on the url and used as a parameter in the controller. This language code is then used in addition to whatever string you are querying in the database.

If you would consider resource files, the solution is already set for you:
http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx

The other thing is based on what the browser requests set the culture. use this name to query the database. The culture name (from How to pass CultureInfo to partialView for ajax call?)

public static string GetCurrentCulture()
{
   return Thread.CurrentThread.CurrentCulture.Name;
}

Now to go cache specific, simply implement the repository pattern. When you query by 'language' and 'string' check to see if that exists in the cache. If it is, return it, otherwise go to the database to retrieve it. This is a great advantage of using the Repository Pattern, the cache details are removed from the caller. If you decide to go to a resource file implementation, then none of this is required anyways since there is built in culture support.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文