From 57562abb6012bd1d98d04c09d9039d7d157e65d8 Mon Sep 17 00:00:00 2001 From: Tom Laird-McConnell Date: Thu, 28 Jun 2018 17:17:33 -0700 Subject: [PATCH] Restore InMemory and CachingBotStore to 3.13 implementations (#4810) * restore InMemory and CachingStore implementations * bump version to 3.15.3.0 --- .../Properties/AssemblyInfo.cs | 4 +- .../Properties/AssemblyInfo.cs | 4 +- .../Properties/AssemblyInfo.cs | 4 +- .../Properties/AssemblyInfo.cs | 4 +- .../ConnectorEx/BotData.cs | 82 ++++++++----------- .../Microsoft.Bot.Builder.nuspec | 2 +- .../Properties/AssemblyInfo.cs | 4 +- .../Microsoft.Bot.Connector.AspNetCore.nuspec | 4 +- .../Properties/AssemblyInfo.cs | 4 +- .../AssemblyInfo.cs | 4 +- ...Microsoft.Bot.Connector.AspNetCore2.nuspec | 4 +- .../Microsoft.Bot.Connector.nuspec | 2 +- .../Properties/AssemblyInfo.cs | 4 +- ...icrosoft.Bot.Connector.NetCore.nuget.props | 4 +- .../Properties/AssemblyInfo.cs | 4 +- .../Microsoft.Bot.Connector.nuspec | 2 +- .../Properties/AssemblyInfo.cs | 4 +- .../AssemblyInfo.cs | 4 +- 18 files changed, 66 insertions(+), 78 deletions(-) diff --git a/CSharp/Library/Microsoft.Bot.Builder.Autofac/Properties/AssemblyInfo.cs b/CSharp/Library/Microsoft.Bot.Builder.Autofac/Properties/AssemblyInfo.cs index 355ec7c1b5..09ecd93418 100644 --- a/CSharp/Library/Microsoft.Bot.Builder.Autofac/Properties/AssemblyInfo.cs +++ b/CSharp/Library/Microsoft.Bot.Builder.Autofac/Properties/AssemblyInfo.cs @@ -32,8 +32,8 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("3.15.2.3")] -[assembly: AssemblyFileVersion("3.15.2.3")] +[assembly: AssemblyVersion("3.15.3.0")] +[assembly: AssemblyFileVersion("3.15.3.0")] [assembly: InternalsVisibleTo("Microsoft.Bot.Builder.Tests")] [assembly: InternalsVisibleTo("Microsoft.Bot.Sample.Tests")] diff --git a/CSharp/Library/Microsoft.Bot.Builder.Calling/Properties/AssemblyInfo.cs b/CSharp/Library/Microsoft.Bot.Builder.Calling/Properties/AssemblyInfo.cs index 1f1f2f1604..c9ebfe8932 100644 --- a/CSharp/Library/Microsoft.Bot.Builder.Calling/Properties/AssemblyInfo.cs +++ b/CSharp/Library/Microsoft.Bot.Builder.Calling/Properties/AssemblyInfo.cs @@ -31,8 +31,8 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("3.15.2.3")] -[assembly: AssemblyFileVersion("3.15.2.3")] +[assembly: AssemblyVersion("3.15.3.0")] +[assembly: AssemblyFileVersion("3.15.3.0")] //[assembly: AssemblyKeyFileAttribute(@"..\\..\\buildtools\\35MSSharedLib1024.snk")] //[assembly: AssemblyDelaySignAttribute(true)] diff --git a/CSharp/Library/Microsoft.Bot.Builder.FormFlow.Json/Properties/AssemblyInfo.cs b/CSharp/Library/Microsoft.Bot.Builder.FormFlow.Json/Properties/AssemblyInfo.cs index 8c0dd68121..5f4de2fb1f 100644 --- a/CSharp/Library/Microsoft.Bot.Builder.FormFlow.Json/Properties/AssemblyInfo.cs +++ b/CSharp/Library/Microsoft.Bot.Builder.FormFlow.Json/Properties/AssemblyInfo.cs @@ -33,8 +33,8 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("3.15.2.3")] -[assembly: AssemblyFileVersion("3.15.2.3")] +[assembly: AssemblyVersion("3.15.3.0")] +[assembly: AssemblyFileVersion("3.15.3.0")] [assembly: InternalsVisibleTo("Microsoft.Bot.Builder.Tests")] [assembly: InternalsVisibleTo("Microsoft.Bot.Sample.Tests")] diff --git a/CSharp/Library/Microsoft.Bot.Builder.History/Properties/AssemblyInfo.cs b/CSharp/Library/Microsoft.Bot.Builder.History/Properties/AssemblyInfo.cs index 0a6daaccda..795185252e 100644 --- a/CSharp/Library/Microsoft.Bot.Builder.History/Properties/AssemblyInfo.cs +++ b/CSharp/Library/Microsoft.Bot.Builder.History/Properties/AssemblyInfo.cs @@ -31,8 +31,8 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("3.15.2.3")] -[assembly: AssemblyFileVersion("3.15.2.3")] +[assembly: AssemblyVersion("3.15.3.0")] +[assembly: AssemblyFileVersion("3.15.3.0")] //[assembly: AssemblyKeyFileAttribute(@"..\\..\\buildtools\\35MSSharedLib1024.snk")] //[assembly: AssemblyDelaySignAttribute(true)] diff --git a/CSharp/Library/Microsoft.Bot.Builder/ConnectorEx/BotData.cs b/CSharp/Library/Microsoft.Bot.Builder/ConnectorEx/BotData.cs index ec22d3fb39..77f797673d 100644 --- a/CSharp/Library/Microsoft.Bot.Builder/ConnectorEx/BotData.cs +++ b/CSharp/Library/Microsoft.Bot.Builder/ConnectorEx/BotData.cs @@ -87,15 +87,14 @@ public interface IBotDataStore } /// - /// Volatile in-memory implementation of + /// Volitile in-memory implementation of /// + /// + /// NOTE: This uses an internal dictionary with no culling so it should not be used for production code at all, as it will eventually just use all of your memory. + /// public class InMemoryDataStore : IBotDataStore { - // This should be moved to autofac registration - internal static readonly MemoryCache store = new MemoryCache(nameof(InMemoryDataStore), new NameValueCollection() { { "PhysicalMemoryLimitPercentage", "50" } }); - - private static CacheItemPolicy cacheItemPolicy = new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromMinutes(15) }; - + internal readonly ConcurrentDictionary store = new ConcurrentDictionary(); private readonly Dictionary locks = new Dictionary() { { BotStoreType.BotConversationData, new object() }, @@ -103,14 +102,10 @@ public class InMemoryDataStore : IBotDataStore { BotStoreType.BotUserData, new object() } }; - public InMemoryDataStore() - { - } - async Task IBotDataStore.LoadAsync(IAddress key, BotStoreType botStoreType, CancellationToken cancellationToken) { - string serializedData = (string)store.Get(GetKey(key, botStoreType)); - if (serializedData != null) + string serializedData; + if (store.TryGetValue(GetKey(key, botStoreType), out serializedData)) return Deserialize(serializedData); return new BotData(eTag: String.Empty); } @@ -119,24 +114,27 @@ async Task IBotDataStore.SaveAsync(IAddress key, BotStoreType botStoreT { lock (locks[botStoreType]) { - string storeKey = GetKey(key, botStoreType); - string oldValue = (string)store.Get(storeKey); if (botData.Data != null) { - if (oldValue != null) + store.AddOrUpdate(GetKey(key, botStoreType), (dictionaryKey) => { - ValidateETag(botData, oldValue); - } - botData.ETag = Guid.NewGuid().ToString("n"); - store.Set(GetKey(key, botStoreType), Serialize(botData), cacheItemPolicy); + botData.ETag = Guid.NewGuid().ToString("n"); + return Serialize(botData); + }, (dictionaryKey, value) => + { + ValidateETag(botData, value); + botData.ETag = Guid.NewGuid().ToString("n"); + return Serialize(botData); + }); } else { // remove record on null - if (oldValue != null) + string value; + if (store.TryGetValue(GetKey(key, botStoreType), out value)) { - ValidateETag(botData, oldValue); - store.Remove(storeKey); + ValidateETag(botData, value); + store.TryRemove(GetKey(key, botStoreType), out value); return; } } @@ -258,14 +256,14 @@ public enum CachingBotDataStoreConsistencyPolicy } /// - /// Caches data for and wraps the data in to be stored in + /// Caches changes until FlushAsync() is called + /// NOTE: Despite the name, this is NOT a cache of access of the inner store, but is a change cache of changes that will be pushed to + /// inner store. /// public class CachingBotDataStore : IBotDataStore { private readonly IBotDataStore inner; - // this should be moved to autofac registration - internal static readonly MemoryCache cache = new MemoryCache(nameof(CachingBotDataStore), new NameValueCollection() { { "PhysicalMemoryLimitPercentage", "50" } }); - internal static CacheItemPolicy cacheItemPolicy = new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromMinutes(15) }; + internal readonly Dictionary cache = new Dictionary(); private readonly CachingBotDataStoreConsistencyPolicy dataConsistencyPolicy; public CachingBotDataStore(IBotDataStore inner, CachingBotDataStoreConsistencyPolicy dataConsistencyPolicy) @@ -274,8 +272,6 @@ public CachingBotDataStore(IBotDataStore inner, CachingBotDataStoreCons this.dataConsistencyPolicy = dataConsistencyPolicy; } - public long GetCount() { return cache.GetCount(); } - internal class CacheEntry { public BotData BotConversationData { set; get; } @@ -283,22 +279,16 @@ internal class CacheEntry public BotData BotUserData { set; get; } } - private string GetCacheKey(IAddress key) - { - return $"{key.BotId}.{key.ChannelId}.{key.ConversationId}.{key.UserId}"; - } - async Task IBotDataStore.FlushAsync(IAddress key, CancellationToken cancellationToken) { - var cacheKey = GetCacheKey(key); - CacheEntry entry = (CacheEntry)cache.Get(GetCacheKey(key)); - if (entry != null) + CacheEntry entry; + if (cache.TryGetValue(key, out entry)) { // Removing the cached entry to make sure that we are not leaking // flushed entries when CachingBotDataStore is registered as a singleton object. // Also since this store is not updating ETags on LoadAsync(...), there // will be a conflict if we reuse the cached entries after flush. - cache.Remove(cacheKey); + cache.Remove(key); await this.Save(key, entry, cancellationToken); return true; } @@ -310,13 +300,12 @@ async Task IBotDataStore.FlushAsync(IAddress key, CancellationTok async Task IBotDataStore.LoadAsync(IAddress key, BotStoreType botStoreType, CancellationToken cancellationToken) { - var cacheKey = GetCacheKey(key); - CacheEntry cacheEntry = (CacheEntry)cache.Get(GetCacheKey(key)); + CacheEntry cacheEntry; BotData value = null; - if (cacheEntry == null) + if (!cache.TryGetValue(key, out cacheEntry)) { cacheEntry = new CacheEntry(); - cache.Add(cacheKey, cacheEntry, cacheItemPolicy); + cache.Add(key, cacheEntry); value = await LoadFromInnerAndCache(cacheEntry, botStoreType, key, cancellationToken); } else @@ -356,15 +345,14 @@ async Task IBotDataStore.LoadAsync(IAddress key, BotStoreType async Task IBotDataStore.SaveAsync(IAddress key, BotStoreType botStoreType, BotData value, CancellationToken cancellationToken) { - var cacheKey = GetCacheKey(key); - CacheEntry cacheEntry = (CacheEntry)cache.Get(GetCacheKey(key)); - if (cacheEntry == null) + CacheEntry entry; + if (!cache.TryGetValue(key, out entry)) { - cacheEntry = new CacheEntry(); - cache.Add(cacheKey, cacheEntry, cacheItemPolicy); + entry = new CacheEntry(); + cache.Add(key, entry); } - SetCachedValue(cacheEntry, botStoreType, value); + SetCachedValue(entry, botStoreType, value); } private async Task LoadFromInnerAndCache(CacheEntry cacheEntry, BotStoreType botStoreType, IAddress key, CancellationToken token) diff --git a/CSharp/Library/Microsoft.Bot.Builder/Microsoft.Bot.Builder.nuspec b/CSharp/Library/Microsoft.Bot.Builder/Microsoft.Bot.Builder.nuspec index e4193e9b67..3c67552389 100644 --- a/CSharp/Library/Microsoft.Bot.Builder/Microsoft.Bot.Builder.nuspec +++ b/CSharp/Library/Microsoft.Bot.Builder/Microsoft.Bot.Builder.nuspec @@ -28,7 +28,7 @@ - + diff --git a/CSharp/Library/Microsoft.Bot.Builder/Properties/AssemblyInfo.cs b/CSharp/Library/Microsoft.Bot.Builder/Properties/AssemblyInfo.cs index 906dfdfbe3..f5d4866d2d 100644 --- a/CSharp/Library/Microsoft.Bot.Builder/Properties/AssemblyInfo.cs +++ b/CSharp/Library/Microsoft.Bot.Builder/Properties/AssemblyInfo.cs @@ -33,8 +33,8 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("3.15.2.3")] -[assembly: AssemblyFileVersion("3.15.2.3")] +[assembly: AssemblyVersion("3.15.3.0")] +[assembly: AssemblyFileVersion("3.15.3.0")] [assembly: InternalsVisibleTo("Microsoft.Bot.Builder.Tests")] diff --git a/CSharp/Library/Microsoft.Bot.Connector.AspNetCore/Microsoft.Bot.Connector.AspNetCore.nuspec b/CSharp/Library/Microsoft.Bot.Connector.AspNetCore/Microsoft.Bot.Connector.AspNetCore.nuspec index bbe8733dd1..afefc20f3b 100644 --- a/CSharp/Library/Microsoft.Bot.Connector.AspNetCore/Microsoft.Bot.Connector.AspNetCore.nuspec +++ b/CSharp/Library/Microsoft.Bot.Connector.AspNetCore/Microsoft.Bot.Connector.AspNetCore.nuspec @@ -2,7 +2,7 @@ Microsoft.Bot.Connector.AspNetCore - 1.1.3.16 + 1.1.3.17 Microsoft microsoft, BotFramework, nugetbotbuilder https://bots.botframework.com/Client/Images/bot-framework-default-7.png @@ -22,7 +22,7 @@ - + diff --git a/CSharp/Library/Microsoft.Bot.Connector.AspNetCore/Properties/AssemblyInfo.cs b/CSharp/Library/Microsoft.Bot.Connector.AspNetCore/Properties/AssemblyInfo.cs index d48bd62383..2503cde545 100644 --- a/CSharp/Library/Microsoft.Bot.Connector.AspNetCore/Properties/AssemblyInfo.cs +++ b/CSharp/Library/Microsoft.Bot.Connector.AspNetCore/Properties/AssemblyInfo.cs @@ -12,8 +12,8 @@ [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("1.1.3.16")] -[assembly: AssemblyFileVersion("1.1.3.16")] +[assembly: AssemblyVersion("1.1.3.17")] +[assembly: AssemblyFileVersion("1.1.3.17")] //[assembly: AssemblyKeyFileAttribute(@"..\\..\\buildtools\\35MSSharedLib1024.snk")] //[assembly: AssemblyDelaySignAttribute(true)] diff --git a/CSharp/Library/Microsoft.Bot.Connector.AspNetCore2/AssemblyInfo.cs b/CSharp/Library/Microsoft.Bot.Connector.AspNetCore2/AssemblyInfo.cs index 9003380196..1025655005 100644 --- a/CSharp/Library/Microsoft.Bot.Connector.AspNetCore2/AssemblyInfo.cs +++ b/CSharp/Library/Microsoft.Bot.Connector.AspNetCore2/AssemblyInfo.cs @@ -10,8 +10,8 @@ [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("2.0.1.8")] -[assembly: AssemblyFileVersion("2.0.1.8")] +[assembly: AssemblyVersion("2.0.1.9")] +[assembly: AssemblyFileVersion("2.0.1.9")] //[assembly: AssemblyKeyFileAttribute(@"..\\..\\buildtools\\35MSSharedLib1024.snk")] //[assembly: AssemblyDelaySignAttribute(true)] diff --git a/CSharp/Library/Microsoft.Bot.Connector.AspNetCore2/Microsoft.Bot.Connector.AspNetCore2.nuspec b/CSharp/Library/Microsoft.Bot.Connector.AspNetCore2/Microsoft.Bot.Connector.AspNetCore2.nuspec index 84917e56f6..db63632815 100644 --- a/CSharp/Library/Microsoft.Bot.Connector.AspNetCore2/Microsoft.Bot.Connector.AspNetCore2.nuspec +++ b/CSharp/Library/Microsoft.Bot.Connector.AspNetCore2/Microsoft.Bot.Connector.AspNetCore2.nuspec @@ -2,7 +2,7 @@ Microsoft.Bot.Connector.AspNetCore - 2.0.1.8 + 2.0.1.9 Microsoft microsoft, BotFramework, nugetbotbuilder https://bots.botframework.com/Client/Images/bot-framework-default-7.png @@ -19,7 +19,7 @@ - + diff --git a/CSharp/Library/Microsoft.Bot.Connector.Falcon/Microsoft.Bot.Connector.nuspec b/CSharp/Library/Microsoft.Bot.Connector.Falcon/Microsoft.Bot.Connector.nuspec index 6dab7b72cf..734dd37ded 100644 --- a/CSharp/Library/Microsoft.Bot.Connector.Falcon/Microsoft.Bot.Connector.nuspec +++ b/CSharp/Library/Microsoft.Bot.Connector.Falcon/Microsoft.Bot.Connector.nuspec @@ -2,7 +2,7 @@ Microsoft.Bot.Connector - 3.15.2.3 + 3.15.3.0 Microsoft microsoft, BotFramework, nugetbotbuilder https://bots.botframework.com/Client/Images/bot-framework-default-7.png diff --git a/CSharp/Library/Microsoft.Bot.Connector.Falcon/Properties/AssemblyInfo.cs b/CSharp/Library/Microsoft.Bot.Connector.Falcon/Properties/AssemblyInfo.cs index a1138ec99a..61bf7279d1 100644 --- a/CSharp/Library/Microsoft.Bot.Connector.Falcon/Properties/AssemblyInfo.cs +++ b/CSharp/Library/Microsoft.Bot.Connector.Falcon/Properties/AssemblyInfo.cs @@ -10,8 +10,8 @@ [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("3.15.2.3")] -[assembly: AssemblyFileVersion("3.15.2.3")] +[assembly: AssemblyVersion("3.15.3.0")] +[assembly: AssemblyFileVersion("3.15.3.0")] //[assembly: AssemblyKeyFileAttribute(@"..\\..\\buildtools\\35MSSharedLib1024.snk")] //[assembly: AssemblyDelaySignAttribute(true)] diff --git a/CSharp/Library/Microsoft.Bot.Connector.NetCore/Microsoft.Bot.Connector.NetCore.nuget.props b/CSharp/Library/Microsoft.Bot.Connector.NetCore/Microsoft.Bot.Connector.NetCore.nuget.props index cf2ef785a5..4386218793 100644 --- a/CSharp/Library/Microsoft.Bot.Connector.NetCore/Microsoft.Bot.Connector.NetCore.nuget.props +++ b/CSharp/Library/Microsoft.Bot.Connector.NetCore/Microsoft.Bot.Connector.NetCore.nuget.props @@ -3,9 +3,9 @@ True NuGet - C:\Github\Microsoft\BotBuilder\CSharp\Library\Microsoft.Bot.Connector.NetCore\project.lock.json + C:\source\github\BotBuilder\CSharp\Library\Microsoft.Bot.Connector.NetCore\project.lock.json $(UserProfile)\.nuget\packages\ - C:\Users\V-BRHALE\.nuget\packages\ + C:\Users\tomlm\.nuget\packages\ ProjectJson 4.7.0 diff --git a/CSharp/Library/Microsoft.Bot.Connector.NetCore/Properties/AssemblyInfo.cs b/CSharp/Library/Microsoft.Bot.Connector.NetCore/Properties/AssemblyInfo.cs index b9ff1168c0..cc64dfb4ef 100644 --- a/CSharp/Library/Microsoft.Bot.Connector.NetCore/Properties/AssemblyInfo.cs +++ b/CSharp/Library/Microsoft.Bot.Connector.NetCore/Properties/AssemblyInfo.cs @@ -1,8 +1,8 @@ using System.Reflection; using System.Resources; -[assembly: AssemblyVersion("3.15.2.3")] -[assembly: AssemblyFileVersion("3.15.2.3")] +[assembly: AssemblyVersion("3.15.3.0")] +[assembly: AssemblyFileVersion("3.15.3.0")] //[assembly: AssemblyKeyFileAttribute(@"..\\..\\buildtools\\35MSSharedLib1024.snk")] //[assembly: AssemblyDelaySignAttribute(true)] diff --git a/CSharp/Library/Microsoft.Bot.Connector.NetFramework/Microsoft.Bot.Connector.nuspec b/CSharp/Library/Microsoft.Bot.Connector.NetFramework/Microsoft.Bot.Connector.nuspec index 83a917186a..f31e2555b6 100644 --- a/CSharp/Library/Microsoft.Bot.Connector.NetFramework/Microsoft.Bot.Connector.nuspec +++ b/CSharp/Library/Microsoft.Bot.Connector.NetFramework/Microsoft.Bot.Connector.nuspec @@ -2,7 +2,7 @@ Microsoft.Bot.Connector - 3.15.2.3 + 3.15.3.0 Microsoft microsoft, BotFramework, nugetbotbuilder https://bots.botframework.com/Client/Images/bot-framework-default-7.png diff --git a/CSharp/Library/Microsoft.Bot.Connector.NetFramework/Properties/AssemblyInfo.cs b/CSharp/Library/Microsoft.Bot.Connector.NetFramework/Properties/AssemblyInfo.cs index 8b38d2a427..aa97d1698f 100644 --- a/CSharp/Library/Microsoft.Bot.Connector.NetFramework/Properties/AssemblyInfo.cs +++ b/CSharp/Library/Microsoft.Bot.Connector.NetFramework/Properties/AssemblyInfo.cs @@ -10,8 +10,8 @@ [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("3.15.2.3")] -[assembly: AssemblyFileVersion("3.15.2.3")] +[assembly: AssemblyVersion("3.15.3.0")] +[assembly: AssemblyFileVersion("3.15.3.0")] //[assembly: AssemblyKeyFileAttribute(@"..\\..\\buildtools\\35MSSharedLib1024.snk")] //[assembly: AssemblyDelaySignAttribute(true)] diff --git a/CSharp/Library/Microsoft.Bot.Connector.Standard/AssemblyInfo.cs b/CSharp/Library/Microsoft.Bot.Connector.Standard/AssemblyInfo.cs index 99ac851ac4..db50dde838 100644 --- a/CSharp/Library/Microsoft.Bot.Connector.Standard/AssemblyInfo.cs +++ b/CSharp/Library/Microsoft.Bot.Connector.Standard/AssemblyInfo.cs @@ -1,8 +1,8 @@ using System.Reflection; using System.Runtime.InteropServices; -[assembly: AssemblyVersion("3.15.2.3")] -[assembly: AssemblyFileVersion("3.15.2.3")] +[assembly: AssemblyVersion("3.15.3.0")] +[assembly: AssemblyFileVersion("3.15.3.0")] //[assembly: AssemblyKeyFileAttribute(@"..\\..\\buildtools\\35MSSharedLib1024.snk")] //[assembly: AssemblyDelaySignAttribute(true)]