1

只是关于 Microsoft Velocity 的随机查询。

场景:假设我想要数据库中的所有订单。在 SQL 中,这很好,我可以做到SELECT OrderId,TotalCost... from Orders。这是我的数据库的一次往返,每个人都很高兴。

现在,如果我使用 Memcached 或(正如我现在使用的)Microsoft Velocity (CTP3),没有简单的方法可以做到这一点。我看到的两个选项是(在伪代码中)

FOR EACH ORDER
     Order = cache.TryGet(OrderId)
     if Order is null
            Order = db.Get(OrderId)
END FOR EACH

这将是往返的负载。

另外,考虑我想通过客户获得订单

SQL:Select OrderId....TotalCost from Orders where CustomerId = MyCustomerId

来回一趟,大家都很开心。

使用缓存的解决方案,我确实看到了两种解决方案:

解决方案1:

DOES CustomerOrderIdsForCustomerId EXIST
     NO
           POPULATE CustomerOrderIdsForCustomerId FROM DATABASE
     YES
           FOR EACH OrderId IN CustomerOrdersForCustomerId
                  cache.TryGet(OrderId)
                  IF Order IS NULL
                        Order = db.Get(OrderId)
           END FOR EACH

解决方案 2 是在它自己的缓存对象中保存所有客户订单的序列化列表。减少往返,但似乎很蹩脚。

有人可以阐明这种情况吗?

4

2 回答 2

1

Just because you have a cache doesn't mean you have to use it for every query! In this instance as you've already identified, it's not really helping you and I'd probably go straight through to the database for this sort of thing.
It depends a bit on your application though - if you think customers are regularly going to be looking at their order history, or you have some function that's analysing orders to see what products are hot, then you might want to use some caching to keep load off your SQL server. In that case, I'd probably go with holding in the cache either a DataTable of the orders, or a collection of Orders and query it with LINQ to show the orders for a customer.

于 2009-11-23T15:04:23.370 回答
0

请记住,缓存不应该是任何数据的永久存储(在您的情况下是订单)。在这种情况下,缓存可以帮助从您的数据库服务器中移除一些负载,但是必须先在缓存中加载订单,然后才能检索它们。话虽如此,如果您使用避免遍历集合的速度,这里有几个选项需要考虑。但是,您总是必须想办法处理不在缓存中的数据。

选项 1:使用区域

您可以创建一个区域并通过一次调用从该区域获取所有对象。在您的场景中,您可以创建一个Orders区域,您可以在其中存储所有订单,然后使用GetObjectsInRegion方法获取缓存中的所有订单。但是请注意,这会带回缓存中的所有订单......这可能会或可能不会拥有您在数据库中拥有的所有订单。

选项 2:使用区域和标签

Velocity 允许您标记放置在缓存区域中的对象,然后使用这些标记检索它们。因此,在您的场景中,您可以使用“订单”标签标记订单对象,然后使用GetObjectsByTag方法来检索它们。由于您可以使用多个标签,您还可以使用他们的客户 ID 标签标记它们,然后以这种方式将它们拉出。

这 2 个选项带有一些注意事项,因此请务必阅读文档: Velocity Tag BasedMethods

于 2009-11-19T20:43:31.790 回答