我正在测试一个以这种方式使用积极缓存的存储库类SettingRepository
:
private Settings Settings
{
get
{
using (Session.Advanced.DocumentStore.AggressivelyCacheFor(TimeSpan.FromDays(1)))
{
return Session.Load<Settings>("settings/Settings") ?? new Settings
{
SomeStringProperty = "DefaultValue"
};
}
}
}
public string GetSomeStringProperty()
{
return Settings.SomeStringProperty;
}
我已将文档存储配置为不使用服务器通知,但请注意session.SaveChanges()
:
store.Conventions.ShouldAggressiveCacheTrackChanges = false;
store.Conventions.ShouldSaveChangesForceAggressiveCacheCheck = true;
我正在以下列方式测试缓存机制(NUnit):
// Arrange
var settings = new Settings
{
SomeStringProperty = "SomeValue"
};
Session.Store(settings, "settings/Settings");
Session.SaveChanges();
RenewSession();
var initialRequests = Session.Advanced.NumberOfRequests;
// Act - Assert 1
var someStringProperty = _settingRepository.GetSomeStringProperty();
Assert.That(someStringProperty, Is.EqualTo("SomeValue"));
Assert.That(Session.Advanced.NumberOfRequests, Is.EqualTo(initialRequests + 1));
// Act - Assert 2
someStringProperty = _settingRepository.GetSomeStringProperty();
Assert.That(applicationName, Is.EqualTo("SomeValue"));
Assert.That(Session.Advanced.NumberOfRequests, Is.EqualTo(initialRequests + 1));
// Act - Assert 3
settings.SomeStringValue = "SomeValueModified";
Session.SaveChanges(); // DOES NOT INVALIDATE CACHE
settings = Session.Load<Settings>("settings/Settings");
settings.SomeStringValue = "SomeValueModified";
Session.SaveChanges(); // THIS DOES INVALIDATE CACHE
...
请参阅底部代码中的注释。在跟踪更改之前,我已明确地从商店重新加载Settings
实体,而对象在初始Store
中通过引用传递:
Session.Store(settings, "settings/Settings");
这是设计的吗?我希望从那时起启用更改跟踪。