简体   繁体   English

重复数据库访问或繁重的参数

[英]Repetitive database access or heavy parameters

I need at some point to load(from a database) a set of texts(~10 000 texts) in a specific language of my website, to then display the website. 我需要在某个时候以我的网站的特定语言从数据库中加载一组文本(约10000文本),然后显示该网站。

Should I be doing this each and every time I'm displaying an element, and this would require a lot of connection attempts? 我应该在每次显示元素时都执行此操作,这将需要进行大量的连接尝试吗? Should I load the whole text once, and pass it as an argument then to the sub-elements that require it? 我是否应该加载整个文本一次,然后将其作为参数传递给需要它的子元素?

I was opting for a singleton class approach(load all once, then access specific element whenever needed), but it looks like this is not a good answer. 我选择了单例类方法(全部加载一次,然后在需要时访问特定元素),但看起来这不是一个好答案。

if you specifically want to cache this 1 result, maybe make it a function with a static variable for caching? 如果您特别想缓存此1结果,也许使其成为具有静态变量的函数进行缓存?

string getEnglish() {
    static string ret = "";
    if (ret == "") { /*actually get the data from db, and put it in ret*/ }
    return ret;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM