【发布时间】:2017-06-22 17:17:31
【问题描述】:
我正在尝试将我自己的博客从 MSSQL/EF 转换为 RavenDB,到目前为止,大部分情况下它都非常轻松,但我遇到了一个问题,我怀疑是“过时的索引”或过度索引自信的缓存,但我无法得到任何实际答案。当我不知道实际问题是什么时,演示比解释更容易。
设置:
// Basic example class
public class ExampleClass{
public int ID {get;set;}
public string Value {get;set;}
}
// Once at application startup:
_docStore = new DocumentStore {
Url = "http://localhost:8080",
DefaultDatabase = "exampleDB",
Credentials = new NetworkCredential("userfoo","passbar"),
};
_docStore.Initialize();
// Querying data:
using (var session = _docStore.OpenSession())
{
var results = _session.Query<ExampleClass>().ToList();
foreach(var result in results)
{
Console.WriteLine(string.Format("ID #{0}: {1}",
result.ID, result.Value));
}
}
问题是: 每当我通过 _docStore 会话以编程方式更新文档时,一切正常。当我在外部(从另一个客户端、在 RavenDB 管理工作室等)编辑数据时,不会返回文档更新。哪里有一些例子。
1) 数据库中的初始值
数据库中的文档:
- ExampleClass/1 { "ID": 1, "Value": "foo", }
- ExampleClass/2 { "ID": 2, "Value": "bar", }
运行查询;控制台结果:
- ID #1:foo
- ID #2:酒吧
2) 通过 _docStore 会话将 ExampleClass/2 的值更新为“BAR”后。
数据库中的文档:
- ExampleClass/1 { "ID": 1, "Value": "foo", }
- ExampleClass/2 { "ID": 2, "Value": "BAR", }
运行查询;控制台结果:
- ID #1:foo
- ID #2:酒吧
3) 通过 RavenDB 工作室将 ExampleClass/2 的值更新为“bah”后
数据库中的文档:
- ExampleClass/1 { "ID": 1, "Value": "foo", }
- ExampleClass/2 { "ID": 2, "Value": "bah", }
运行查询;控制台结果:
- ID #1:foo
- ID #2:酒吧
4) 通过 RavenDB studio 添加 ExampleClass/3 后
数据库中的文档:
- ExampleClass/1 { "ID": 1, "Value": "foo", }
- ExampleClass/2 { "ID": 2, "Value": "bah", }
- ExampleClass/3 { "ID": 3, "Value": "spam", }
运行查询;控制台结果:
- ID #1:foo
- ID #2:酒吧
- ID #3:垃圾邮件
5) 通过 RavenDB studio 删除 ExampleClass/2 后
数据库中的文档:
- ExampleClass/1 { "ID": 1, "Value": "foo", }
- ExampleClass/3 { "ID": 3, "Value": "spam", }
运行查询;控制台结果:
- ID #1:foo
- ID #3:垃圾邮件
6) 通过 RavenDB 工作室更改 ExampleClass/3 值后
数据库中的文档:
- ExampleClass/1 { "ID": 1, "Value": "foo", }
- ExampleClass/3 { "ID": 3, "Value": "SPAM", }
运行查询;控制台结果:
- ID #1:foo
- ID #3:垃圾邮件
6) 通过 RavenDB studio 添加 ExampleClass/4 后
数据库中的文档:
- ExampleClass/1 { "ID": 1, "Value": "foo", }
- ExampleClass/3 { "ID": 3, "Value": "SPAM", }
- ExampleClass/4 { "ID": 4, "Value": "bar", }
运行查询;控制台结果:
- ID #1:foo
- ID #3:垃圾邮件
- ID #4:酒吧
*7) 初始化新 DocumentStore 后(无数据更改)*
数据库中的文档:
- ExampleClass/1 { "ID": 1, "Value": "foo", }
- ExampleClass/3 { "ID": 3, "Value": "SPAM", }
- ExampleClass/4 { "ID": 4, "Value": "bar", }
运行查询;控制台结果:
- ID #1:foo
- ID #3:垃圾邮件
- ID #4:酒吧
希望这能很好地显示出问题所在。添加和删除整个文档反映得很好,但更新在第一次加载之后被完全忽略,这让我怀疑缓存而不是“过时的索引”......但我尝试过这样的事情但没有运气:
// After initialising document store:
_docStore.DisableAggressiveCaching();
_docStore.DatabaseCommands.DisableAllCaching();
_docStore.Conventions.DefaultQueryingConsistency =
ConsistencyOptions.QueryYourWrites;
// Whenever opening a new session:
_session.Advanced.DatabaseCommands.DisableAllCaching();
_session.Advanced.AllowNonAuthoritativeInformation = false;
// When querying:
var result = _session.Query<ExampleClass>()
.Customize(x=>x.WaitForNonStaleResults())
.ToList();
!$#* 是怎么回事? RavenDB 为什么讨厌我?
【问题讨论】:
-
怀疑 IOC 参与其中,我已将温莎城堡从等式中删除,但仍然得到相同的结果。
标签: c# .net caching nosql ravendb