缓存查找性能
我们有一个大型 winforms C# 应用程序,它基本上是一些数据库(CRUD 内容)的前端,我正在尝试为业务对象实现一些内存缓存。
类似于:
List<Customer> customerCache; // Loaded during app. startup
我已经创建了一些代码来使缓存与数据库保持最新。 该代码始终在单独的线程上运行,并且运行良好。 我的问题是,根据缓存的大小,在数据库中执行“select * fromcustomers where id = x”比使用 foreach(foreach Customer cmr in customerCache)循环遍历缓存来查找特定对象要快...
有没有一种方法可以快速搜索缓存中的特定对象? 我打算尝试一些算法或更改我的收藏类型,但我很乐意听取您的建议。
请注意,我们有几个“List xxxCache”并且一切都很快(当然对于小N来说)。 但是当缓存的iten数量增长时(通常> 3000),从数据库读取的速度会更快。
循环浏览我的缓存项目以查找特定项目的最佳方法是什么? 所有业务项都继承自共同的祖先并具有“ID”属性(整数,唯一)。
抱歉我的英语不好,这不是我的主要语言。 此致, 来自巴西的问候。
We have a big winforms C# application, that's basically a frontend for some databases (CRUD stuff) and I'm trying to implement some in memory cache for business objects.
Something like:
List<Customer> customerCache; // Loaded during app. startup
I've already created some code to keep the cache up-to-date with the database. This code run on a separate thread all the time, and is working really well.
My problem is that depending on the size of the cache, it's faster to do a 'select * from customers where id = x' in the database than looping through the cache with a foreach (foreach Customer cmr in customerCache) to find that specific object...
Is there a way to search for specific objects in my cache really fast ? I was going to try some algorithm or changing the type of my collection, but I would appreciate listening to your suggestions.
Please note that we have several 'List xxxCache' and everything is fast (for small N, off course). But when the number of cached itens grow (> 3000 normally) its faster to read from the database.
What's the best way to loop through my cached items to find a specific one ? All business items inherit from a common ancestor and have an 'ID' property(integer, unique).
Sorry for my bad english, it's not my primary language.
Best regards,
Greetings from Brazil.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我们的 Web 表单应用程序也有类似的案例。
我们使用 MS Enterprise Lib Cache 块。
它易于实施和使用。
唯一需要关注的是Cache Key(字符串类型)
缓存.add(键,对象)
缓存.getdata(key)
We have a similar case for the web form application.
We use MS Enterprise Lib Cache block.
It is easy to implement and use.
The only thing you need to focus in Cache Key (string type)
cache.add(key, object)
cache.getdata(key)
请改用
Dictionary
。 它支持基于键的 O(1) 查找。 在本例中,键是 Customer.Id。您可能还想研究其他针对 .Net 的预构建数据库缓存解决方案。
Use
Dictionary<int, Customer>
instead. It supports O(1) lookup based on a key. In this case, key would be Customer.Id.You might also want to look into other pre-built database caching solutions for .Net.
与其使用 List
对象,为什么不使用 :KeyValuePairDictionary 是正确使用的对象(KeyValuePair 是字典包含
**facepalm**
的集合)Insteaf of using a List<T>
object, why not use a :KeyValuePairDictionary is the correct object to use (KeyValuePair is what a dictionary holds a collection of
**facepalm**
)使用与您需要的索引数量一样多的字典。
use as many dictionaries as the number of indexes you need.