本文介绍了微风:从另一个用户从数据库中删除的缓存中删除实体,而不清除整个缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题可能很普遍,但是我找不到任何解决方案。
当用户在客户端的缓存中有实体,而另一个用户(在服务器上)删除了其中的某些实体时,就会发生此问题。然后,当第一个用户想要更新其数据时,不会从缓存中删除已删除的实体。您可以通过在每次更新时清除缓存来解决此问题,但同时也会丢失所有未保存的更改。
我缺少明显的东西吗?

Im facing a problem that probably is quite common but i can't find any solution to it.The problem occurs when a user has entities in its cache on the client and another user removes some of those entities (on the server). When the first user then wants to update its data the removed entities is not removed from the cache. You could solve it by clearing the cache each time you update but then you also looses all non-saved changes.Am I missing something obvious?

示例:

型号:

public class Order
{
    [Key]
    public int Id { get; set; }
    public ICollection<OrderDetail> OrderDetails { get; set; }
}

public class OrderDetail
{
    [Key]
    public int Id { get; set; }
    [ForeignKey("Order")]
    public int Order_Id { get; set; }
    public virtual Order Order { get; set; }
}

客户代码:

function getOrder(orderId, orderObservable) {
    var query = EntityQuery.from("Orders")
                .where("orderId", "==", orderId)
                .expand("orderDetails");
    return manager.executeQuery(query).then(querySucceeded).fail(queryFailed);

    function querySucceeded(data) {
        var order = data.results[0];
        // NOTE: the removed orderdetail is still there 'order.orderDetails'
        orderObservable(order);
    }
}

分步方案:


  1. 用户A查询具有相应orderdetails的订单。

  2. 然后将订单和orderdetails放入缓存中

  3. 用户B删除订单明细并将更改保存到服务器。

  4. 用户A查询以获取订单的最新更新。

  5. 当查询返回时,删除的订单详细信息仍然存在。

  1. User A queries for an order with its corresponding orderdetails.
  2. The order and orderdetails is then placed in the cache.
  3. User B removes an orderdetail and saves the changes to the server.
  4. User A queries to get the latest updates for the order.
  5. When the query returns the removed orderdetail is still there.

在微风文档中,标题为关于清除缓存的重要警告下,有一种解决方案,可通过比较缓存和结果来删除缓存的实体从查询中分离出结果中缺少的实体。

但这在这种情况下不起作用。我猜测这与以下事实有关:orderdetails与订单相关,并且在传递给成功回调之前先从缓存中拾取它。

In the breeze-docs, under the headline "Important Caveats about cache clearing", there is a solution that removes cached entities by comparing the cache and the result from the query and detaches the missing entities in the result.http://www.breezejs.com/documentation/entitymanager-and-cachingBut that doesn't work in this case. I'm guessing it has to do with the fact that orderdetails is related to the order and that it is "picked up" from the cache before it is passed to the success-callback.

感谢所有帮助!

推荐答案

您面临的问题不是Breeze,而是总体设计。想到了两个选项-

The problem you are facing isn't with Breeze, but with design in general. There are a couple of options that come to mind -


  1. 使用SignalR通知您的Web应用程序已发生更改,请分离从高速缓存中删除的所有实体。

  1. Use SignalR to notify your web application that a change has occurred, detach any removed entities from the cache.

使用已存档或已删除的标志,而不是从数据库中删除实体。

Use an archived or deleted flag instead of removing the entities from the database.

两者都有优点和缺点。

使用SignalR,您将需要进行管道工作通知的地方,并围绕删除已删除的实体设置特定的工作流程

With SignalR you will need to get the pipe work in place for notifications and set up a specific work flow around removing deleted entities

manager.detachEntity(entityToDetach);

分离而不是删除的原因是,如果将其设置为Delete,则您的Breeze实体经理仍然认为您需要将更改保留到数据库。

The reason you would detach instead of deleting is because if you set it to deleted then your Breeze entity manager still thinks you need to persist that change to the database.

如果使用标记,则可以简单地设置业务逻辑以忽略标记为已删除或已归档的实体,并且在查询数据库时它将更改返回给该实体并停止显示

If you use a flag then you could simply set your business logic to ignore entities that are flagged as deleted or archived and when you query the DB it will return the change to that entity and stop showing it

myEntity().archived(true);

这里的问题是,如果您的实体与查询不匹配,它将永远不会返回更新的实体让客户端知道它已被存档或删除。另一个警告是,您将在数据库中放置不再活动的信息。

The problem here would be if your entity doesn't match your query it would never return the updated entity to let the client know that it was archived or deleted. The other caveat is that you would have information laying around in your database that isn't active anymore.

根据您要使用的应用程序类型和要求,这些选择中,还是想出另一个。希望有帮助。

Depending on which type of application and requirements you have you should make one of these choices, or come up with another. Hope that helps.

这篇关于微风:从另一个用户从数据库中删除的缓存中删除实体,而不清除整个缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 09:34