protected override void Setup(FeatureConfigurationContext context) { var store = DocumentStoreManager.GetDocumentStore <StorageType.Subscriptions>(context.Settings); store.Listeners.RegisterListener(new SubscriptionV1toV2Converter()); var persister = new SubscriptionPersister(store); if (context.Settings.GetOrDefault <bool>(RavenDbSubscriptionSettingsExtensions.DoNotAggressivelyCacheSubscriptionsSettingsKey)) { persister.DisableAggressiveCaching = true; } if (!context.Settings.TryGet(RavenDbSubscriptionSettingsExtensions.LegacySubscriptionVersioningKey, out bool useLegacy)) { throw new Exception("RavenDB subscription storage requires using either `persistence.DisableSubscriptionVersioning()` or `persistence.UseLegacyVersionedSubscriptions()` to determine whether legacy versioned subscriptions should be used."); } if (useLegacy) { // This is the default in the persister class, to facilitate tests verifying legacy behavior Log.Warn("RavenDB Persistence is using legacy versioned subscription storage. This capability will be removed in NServiceBus.RavenDB 6.0.0. Subscription documents need to be converted to the new unversioned format, after which `persistence.DisableSubscriptionVersioning()` should be used."); } else { persister.SubscriptionIdFormatter = new NonVersionedSubscriptionIdFormatter(); } if (context.Settings.TryGet(RavenDbSubscriptionSettingsExtensions.AggressiveCacheDurationSettingsKey, out TimeSpan aggressiveCacheDuration)) { persister.AggressiveCacheDuration = aggressiveCacheDuration; } context.Container.ConfigureComponent <ISubscriptionStorage>(_ => persister, DependencyLifecycle.SingleInstance); }
public async Task Subscription_queries_should_be_cached(bool disableAggressiveCache, RequestStatus expectedResultOnSubscriptionQueries) { persister = new SubscriptionPersister(store); persister.DisableAggressiveCaching = disableAggressiveCache; await persister.Subscribe(new Subscriber("TransportAddress1", "Endpoint1"), MessageTypes.MessageA, new ContextBag()); await persister.Subscribe(new Subscriber("TransportAddress2", "Endpoint2"), MessageTypes.MessageA, new ContextBag()); using (var session = store.OpenAsyncSession()) { await session.StoreAsync(new RandomDoc(), "RandomDoc/test").ConfigureAwait(false); await session.SaveChangesAsync().ConfigureAwait(false); } var messageTypes = new[] { MessageTypes.MessageA }; Console.WriteLine("-- First subscriber query..."); requests.Clear(); var subscribers = await persister.GetSubscriberAddressesForMessage(messageTypes, new ContextBag()).ConfigureAwait(false); Assert.AreEqual(2, subscribers.Count()); Assert.AreEqual(1, requests.Count); Assert.AreEqual(RequestStatus.SentToServer, requests[0].Status); Console.WriteLine($"-- Subsequent subscription queries, should be {expectedResultOnSubscriptionQueries}"); for (var i = 0; i < 5; i++) { requests.Clear(); var cachedSubs = await persister.GetSubscriberAddressesForMessage(messageTypes, new ContextBag()).ConfigureAwait(false); Assert.AreEqual(2, cachedSubs.Count()); Assert.AreEqual(1, requests.Count); Assert.AreEqual(expectedResultOnSubscriptionQueries, requests[0].Status); } Console.WriteLine("-- Random doc first query"); using (var session = store.OpenAsyncSession()) { requests.Clear(); await session.LoadAsync<RandomDoc>("RandomDoc/test").ConfigureAwait(false); Assert.AreEqual(1, requests.Count); Assert.AreEqual(RequestStatus.SentToServer, requests[0].Status); } Console.WriteLine("-- Random doc, subsequent loads should be Cached, not AggressivelyCached"); for (var i = 0; i < 5; i++) { using (var session = store.OpenAsyncSession()) { requests.Clear(); await session.LoadAsync<RandomDoc>("RandomDoc/test").ConfigureAwait(false); Assert.AreEqual(1, requests.Count); Assert.AreEqual(RequestStatus.Cached, requests[0].Status); } } }
protected override void Setup(FeatureConfigurationContext context) { var store = DocumentStoreManager.GetDocumentStore <StorageType.Subscriptions>(context.Settings); var persister = new SubscriptionPersister(store); if (context.Settings.GetOrDefault <bool>(RavenDbSubscriptionSettingsExtensions.DoNotAggressivelyCacheSubscriptionsSettingsKey)) { persister.DisableAggressiveCaching = true; } if (context.Settings.TryGet(RavenDbSubscriptionSettingsExtensions.AggressiveCacheDurationSettingsKey, out TimeSpan aggressiveCacheDuration)) { persister.AggressiveCacheDuration = aggressiveCacheDuration; } context.Container.ConfigureComponent <ISubscriptionStorage>(_ => persister, DependencyLifecycle.SingleInstance); }
protected override void Setup(FeatureConfigurationContext context) { var store = DocumentStoreManager.GetDocumentStore<StorageType.Subscriptions>(context.Settings); store.Listeners.RegisterListener(new SubscriptionV1toV2Converter()); var persister = new SubscriptionPersister(store); if (context.Settings.GetOrDefault<bool>(RavenDbSubscriptionSettingsExtensions.DoNotAggressivelyCacheSubscriptionsSettingsKey)) { persister.DisableAggressiveCaching = true; } TimeSpan aggressiveCacheDuration; if (context.Settings.TryGet(RavenDbSubscriptionSettingsExtensions.AggressiveCacheDurationSettingsKey, out aggressiveCacheDuration)) { persister.AggressiveCacheDuration = aggressiveCacheDuration; } context.Container.ConfigureComponent<ISubscriptionStorage>(_ => persister, DependencyLifecycle.SingleInstance); }