使用 ContentService API 删除文档不会刷新缓存

Deleting a document using ContentService API doesn't refresh cache

在 Umbraco 7 中,我使用 ContentService API 来插入、更新或删除文档。插入或更新文档后,立即显示正确的内容,但删除后,由于缓存的原因,可以查看删除的数据。要清除缓存,我从 App_data.

中删除 DistCache 文件夹的数据

如何以编程方式刷新缓存?

这是我的代码:

public string DeleteStudent(int studentID)
{
    IContentService service = ApplicationContext.Current.Services.ContentService;
    if (studentID != 0)
    {
        IContent student = service.GetById(studentID);

        if (student != null && student.ContentType.Alias == "dtStudent")
        {
            service.Delete(student);
            return "Done!";
        }
    }
    return "Error!";
}

通常您不必在删除后手动更新缓存(您可以检查日志文件以了解可能的错误)

如果要手动刷新,可以调用下面的方法

umbraco.library.RefreshContent();

注意:在具有许多节点的 Umbraco 实例中,该方法非常慢

As @Harvey said in the comment, using service.MoveToRecycleBin works fine. That method will unpublish the content, then move it to the recycle bin.

最终代码:

public string DeleteStudent(int studentID)
{
    IContentService service = ApplicationContext.Current.Services.ContentService;
    if (studentID != 0)
    {
        IContent student = service.GetById(studentID);

        if (student != null && student.ContentType.Alias == "dtStudent")
        {
            service.MoveToRecycleBin(student);
            return "Done!";
        }
    }
    return "Error!";
}

您还可以清除 Umbraco 后台的缓存,方法是右键单击“内容”选项卡旁的省略号,然后单击 "Republish entire site"。这在技术上不是一种编程方式,但它是 quick/easy 并且可以完成工作!

    https://i.stack.imgur.com/MIUOh.jpg