Skip to content

Commit 0e4b6ac

Browse files
committed
Query cache cleaning on node removal if required
1 parent 212e9b7 commit 0e4b6ac

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Orm/Xtensive.Orm/Caching/LruCache{TKey, TItem}.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ protected virtual void Cleared() { }
239239

240240
#endregion
241241

242+
internal IEnumerable<TKey> GetKeysInternal()
243+
{
244+
foreach (KeyValuePair<TKey, TItem> cachedItem in deque)
245+
yield return cachedItem.Key;
246+
}
247+
242248

243249
// Constructors
244250

Orm/Xtensive.Orm/Orm/StorageNodeManager.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
// Created: 2014.03.13
66

77
using System;
8+
using System.Linq;
89
using JetBrains.Annotations;
10+
using Xtensive.Core;
911
using Xtensive.Orm.Configuration;
1012
using Xtensive.Orm.Providers;
1113
using Xtensive.Orm.Upgrade;
@@ -34,12 +36,24 @@ public bool AddNode([NotNull] NodeConfiguration configuration)
3436
/// Removes node with specified <paramref name="nodeId"/>.
3537
/// </summary>
3638
/// <param name="nodeId">Node identifier.</param>
39+
/// <param name="clearQueryCache">
40+
/// if <see langword="true"/> then cached queries dedicated to the removing node will be removed from cache as well. By default <see langword="false"/>.
41+
/// </param>
3742
/// <returns>True if node was removed, otherwise false.</returns>
38-
public bool RemoveNode([NotNull] string nodeId)
43+
public bool RemoveNode([NotNull] string nodeId, bool clearQueryCache = false)
3944
{
40-
return handlers.StorageNodeRegistry.Remove(nodeId);
45+
var removeResult = handlers.StorageNodeRegistry.Remove(nodeId);
46+
47+
if (removeResult && clearQueryCache) {
48+
var queryCache = (Caching.LruCache<object, Pair<object, Linq.TranslatedQuery>>) handlers.Domain.QueryCache;
49+
foreach (var key in queryCache.GetKeysInternal().Where(k => k is Pair<object, string> p && p.Second == nodeId).ToChainedBuffer()) {
50+
queryCache.RemoveKey(key, true);
51+
}
52+
}
53+
return removeResult;
4154
}
4255

56+
4357
/// <summary>
4458
/// Gets node with the specified <paramref name="nodeId"/>
4559
/// </summary>

0 commit comments

Comments
 (0)