在 C# 中,从数据库加载应用内存中键值对的最佳方法
在我们的 .net 应用程序中,我们需要从数据库加载一些数据(键/值对)并在整个应用程序中使用它。我们不想为每个请求(键)一次又一次地访问数据库来获取值。我想知道这方面标准且有效的方法是什么?
感谢到目前为止所有人的回复..我认为,就我而言(数据不是很大),我会将数据加载到一些静态键值对中,并在需要时使用它。我希望将其添加到业务层,这是我过度简化的结构,在基类中我添加了一个静态构造函数并定义了键值对:
class BaseClass
{
//declare static keyvaluepair (kvp)
static BaseClass()
{
//load kvp from database
}
public void print(string key)
{
//use kvp to get the value for the key
}
}
class ChildA : BaseClass
{
public void somethingA()
{
//call print(key)
}
}
class ChildB : BaseClass
{
public void somethingB()
{
//call print(key)
}}
class main
{
public void GetChildA()
{
ChildA a = new ChildA();
a.somethingA();
}
public void GetChildB()
{
ChildB b = new ChildB();
b.somethingB();
}
}
我从前端调用 Main 类中的方法。我想确保当我调用 a.somethingA() 或 b.somethingB() 时,kvp 最初应该只加载一次。后续调用应该只是从内存中获取数据。上述方法看起来正确还是我搞砸了?
In our .net application we need to load some data (key/value pairs) from the database and use it through out the application. We don't want to hit the database for each request (key) to get the value again and again. I would like to know what is the standard and effecient approach on this?
Thanks to all for the responses till now.. and I think, in my case (data is not very large), I will load the data in some static keyvaluepair and use it when needed. I want this to be added to the business layer, here is my over simplified structure, in the base class I have added a static constructor and defiend the keyvaluepair:
class BaseClass
{
//declare static keyvaluepair (kvp)
static BaseClass()
{
//load kvp from database
}
public void print(string key)
{
//use kvp to get the value for the key
}
}
class ChildA : BaseClass
{
public void somethingA()
{
//call print(key)
}
}
class ChildB : BaseClass
{
public void somethingB()
{
//call print(key)
}}
class main
{
public void GetChildA()
{
ChildA a = new ChildA();
a.somethingA();
}
public void GetChildB()
{
ChildB b = new ChildB();
b.somethingB();
}
}
From the frontend I call the method from the Main class. I want to make sure that when I call a.somethingA() or b.somethingB(), the kvp should only be loaded once initially. And the subsequent calls should just get the data from the memory. Does above approach seems correct or I have messed it up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在程序启动时将表的所有数据(如果不是太大)加载到哈希表中。
然后,您的应用程序可以非常快速地从全局/静态哈希表变量访问键值对。
Load all data of your table (if it is not too large) in a Hashtable at program start.
Then your application can access the key-value-pairs very fast from your global/static Hashtable variable.
两种方法自然是
(初始加载较慢,进一步加快
请求,如果表是大内存使用
大)
你可以
预加载流行的键值对,以及
首先检查它们是否已加载
在变为现实之前在本地内存中
调用数据库。如果您无法弄清楚什么是流行的,您可以引入一些标准来弄清楚,并仅预加载点击次数 > 的键值对。一些常数。当桌子很大时,第三种方法可能是最好的,但您需要找到一种方法来预加载最流行的对,这听起来像是一项非常艰巨的任务。
当然,您需要将最近请求的对保存在内存中,这样即使您实施了第一个策略,也不会对相同的值对进行连续的 sql 调用。
2 approaches come in naturally
(slow initial load, fast further
requests, big memory use if table is
big)
you can
preload popular key value pairs, and
check whether they are loaded first
in local memory before making real
call to db. If you can't figure out whats popular you can introduce some criteria to figure it out, and preload only keyvaluepairs with hit count > some constant . 3rd approach might be best when table is big but you need to find a way to preload most popular pairs which sounds like a pretty hard task.
Of-course you need to save recently requested pairs in memory so you dont make continious sql calls for same value pairs, even if you implement first strategy.
这实际上取决于集合的大小。如果它是一个相对较小的集合,您可能希望在应用程序启动时加载它并从那里读取它。但是,如果它是一个较大的集合,或者仅在正常使用模式下读取某些键,那么在内存容器中编写自己的键会很有帮助,该容器知道如何查询数据库以获取键。然后你可以做类似的事情
NodeCollection 类将维护一个内部键/值集合,它将查询传递的“keyofitem”,如果存在,则返回,如果不存在,则发生数据库查找,获取值,添加到内部集合并返回给调用者。
It depends on the size of the collection really. If it is a relatively smaller collection you might want to load this on the application start and read it from there. However if it is a larger collection or only some keys are read on a normal usage pattern then it would be helpful to write your own in memory container which knows how to query the DB to get a key. You could then do something like
The NodeCollection class will maintain an internal key/value collection which will be queried for the "keyofitem" passed, if it exists it is returned, if not then a DB lookup happens, the value is fetched, is added to the internal collection and returned to the caller.