我有一个 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.
发布评论
评论(3)
我想你可以将它们存储在内存缓存中(分布式或本地缓存,具体取决于你有多少种语言和字典值)。
您可以使用 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.
一种常见的方法是在 url 上提供代码并用作控制器中的参数。然后,除了您在数据库中查询的任何字符串之外,还可以使用该语言代码。
如果您考虑资源文件,解决方案已经为您设置好了:
http://www.hanselman.com/blog/GlobalizationInternationalizationAndLocalizationInASPNETMVC3JavaScriptAndJQueryPart1.aspx
另一件事是根据浏览器请求设置的文化。使用该名称来查询数据库。区域性名称(来自 如何将 CultureInfo 传递给partialView以进行ajax调用?)
现在要具体讨论缓存,只需实现存储库模式即可。当您按“语言”和“字符串”查询时,请检查缓存中是否存在。如果是则返回,否则去数据库检索。这是使用存储库模式的一个很大的优点,缓存详细信息从调用者那里删除。如果您决定采用资源文件实现,那么无论如何都不需要这些,因为有内置的文化支持。
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?)
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.
我最终得到了一个自定义资源提供程序,例如 http://weblogs.asp.net/thangchung/archive/2010/06/25/extending-resource-provider-for-soring-resources-in-the-database.aspx 与较小的修改。
I ended up with a custom resource provider like http://weblogs.asp.net/thangchung/archive/2010/06/25/extending-resource-provider-for-soring-resources-in-the-database.aspx with minor modifications.