简体   繁体   English

如何将具有很多属性的数据库数据存储到缓存中?

[英]How to store database data with lots of attributes into cache?

Let's say that I have a table with columns TABLE_ID, CUSTOMER_ID, ACCOUNT_NUMBER, PURCHASE_DATE, PRODUCT_CATEGORY, PRODUCT_PRICE. 假设我有一个表,该表的列为TABLE_ID,CUSTOMER_ID,ACCOUNT_NUMBER,PURCHASE_DATE,PRODUCT_CATEGORY和PRODUCT_PRICE。 This table contains all purchases made in some store. 该表包含在某个商店中进行的所有购买。

Please don't concentrate on changing the database model (there are obvious improvement possibilities) because this is a made-up example and I can't change the actual database model , which is far from perfect. 请不要专心于更改数据库模型(有明显的改进可能性),因为这是一个虚构的示例, 我无法更改实际的数据库模型 ,这远非完美。 The only thing I can change is the code which uses the already existing database model. 我唯一可以更改的是使用现有数据库模型的代码。

Now, I don't want to access the database all the time, so I have to store the data into cache and then read it from there. 现在,我不想一直访问数据库,因此我必须将数据存储到高速缓存中,然后从那里读取数据。 The problem is, my program has to support all sorts of things: 问题是,我的程序必须支持各种各样的东西:

  1. What is the total value of purchases made by customer X on date Y? 客户X在日期Y进行的购买的总价值是多少?
  2. What is the total value of purchases made for products from category X? 购买X类产品的总价值是多少?
  3. Give me a list of total amounts spent grouped by customer_id. 请给我一份按customer_id分组的总花费清单。

etc. 等等

I have to be able to preserve this hierarchy in my cache. 我必须能够在我的缓存中保留此层次结构。
One possible solution is to have a map inside a map inside a map... etc. 一种可能的解决方案是在地图内部的地图中放置地图...等等。
However, that gets messy very quickly, because I need an extra nesting level for every attribute in the table. 但是,这很快就会变得混乱,因为我需要为表中的每个属性增加一个嵌套级别。

Is there a smarter way to do this? 有更聪明的方法吗?

Have you already established that you need a cache? 您是否已经确定需要缓存? Are you sure the performance of your application requires it? 您确定应用程序的性能需要吗? The database itself can optimize queries, have things in memory, etc. 数据库本身可以优化查询,将内容存储在内存中,等等。

If you're sure you need a cache, you also need to think about cache invalidation: is the data changing from beneath your feet, ie is another process changing the data in the database, or is the database data immutable, or is your application the only process modifying your data. 如果确定需要缓存,则还需要考虑缓存失效:数据是否从脚下改变,即是另一个进程在更改数据库中的数据,还是数据库数据是不可变的,或者是您的应用程序修改数据的唯一过程。

What do you want your cache to do? 您想要缓存做什么? Just keep track of queries and results that have been requested so the second time a query is run, you can return the result from the cache? 只需跟踪查询和已请求的结果,以便第二次运行查询,就可以从缓存中返回结果? Or do you want to aggressively pre calculate some aggregates? 还是要积极地预先计算一些总量? Can the cache data fit into your app memory or do you want to use ReferenceMaps for example that shrink when memory gets tight? 缓存数据是否可以容纳到您的应用程序内存中?或者您想使用例如当内存紧缩时会收缩的ReferenceMaps?

For your actual question, why do you need maps inside maps? 对于您的实际问题,为什么在地图内需要地图? You probably should design something that's closer to your business model, and store objects that represent the data in a meaningful way. 您可能应该设计更接近您的业务模型的东西,并以有意义的方式存储代表数据的对象。 You could have each query (PurchasesByCustomer, PurchasesByCategory) represented as an object and store them in different maps so you get some type safety. 您可以将每个查询(PurchasesByCustomer,purchasesByCategory)表示为一个对象,并将它们存储在不同的映射中,以便获得某种类型的安全性。 Similarly don't use maps for the result but the actual objects you want. 同样,不要将地图用于结果,而是要使用实际的对象。

Sorry, your question is quite vague, but hopefully I've given you some food for thoughts. 抱歉,您的问题很模糊,但希望我能给您一些思想上的帮助。

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

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