Skip to content

Commit

Permalink
Restore InMemory and CachingBotStore to 3.13 implementations (#4810)
Browse files Browse the repository at this point in the history
* restore InMemory and CachingStore implementations

* bump version to 3.15.3.0
  • Loading branch information
Tom Laird-McConnell authored Jun 29, 2018
1 parent 0631d06 commit 57562ab
Show file tree
Hide file tree
Showing 18 changed files with 66 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
82 changes: 35 additions & 47 deletions CSharp/Library/Microsoft.Bot.Builder/ConnectorEx/BotData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,25 @@ public interface IBotDataStore<T>
}

/// <summary>
/// Volatile in-memory implementation of <see cref="IBotDataStore{BotData}"/>
/// Volitile in-memory implementation of <see cref="IBotDataStore{BotData}"/>
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public class InMemoryDataStore : IBotDataStore<BotData>
{
// 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<string, string> store = new ConcurrentDictionary<string, string>();
private readonly Dictionary<BotStoreType, object> locks = new Dictionary<BotStoreType, object>()
{
{ BotStoreType.BotConversationData, new object() },
{ BotStoreType.BotPrivateConversationData, new object() },
{ BotStoreType.BotUserData, new object() }
};

public InMemoryDataStore()
{
}

async Task<BotData> IBotDataStore<BotData>.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);
}
Expand All @@ -119,24 +114,27 @@ async Task IBotDataStore<BotData>.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;
}
}
Expand Down Expand Up @@ -258,14 +256,14 @@ public enum CachingBotDataStoreConsistencyPolicy
}

/// <summary>
/// Caches data for <see cref="BotDataBase{T}"/> and wraps the data in <see cref="BotData"/> to be stored in <see cref="CachingBotDataStore.inner"/>
/// 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.
/// </summary>
public class CachingBotDataStore : IBotDataStore<BotData>
{
private readonly IBotDataStore<BotData> 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<IAddress, CacheEntry> cache = new Dictionary<IAddress, CacheEntry>();
private readonly CachingBotDataStoreConsistencyPolicy dataConsistencyPolicy;

public CachingBotDataStore(IBotDataStore<BotData> inner, CachingBotDataStoreConsistencyPolicy dataConsistencyPolicy)
Expand All @@ -274,31 +272,23 @@ public CachingBotDataStore(IBotDataStore<BotData> inner, CachingBotDataStoreCons
this.dataConsistencyPolicy = dataConsistencyPolicy;
}

public long GetCount() { return cache.GetCount(); }

internal class CacheEntry
{
public BotData BotConversationData { set; get; }
public BotData BotPrivateConversationData { set; get; }
public BotData BotUserData { set; get; }
}

private string GetCacheKey(IAddress key)
{
return $"{key.BotId}.{key.ChannelId}.{key.ConversationId}.{key.UserId}";
}

async Task<bool> IBotDataStore<BotData>.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;
}
Expand All @@ -310,13 +300,12 @@ async Task<bool> IBotDataStore<BotData>.FlushAsync(IAddress key, CancellationTok

async Task<BotData> IBotDataStore<BotData>.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
Expand Down Expand Up @@ -356,15 +345,14 @@ async Task<BotData> IBotDataStore<BotData>.LoadAsync(IAddress key, BotStoreType

async Task IBotDataStore<BotData>.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<BotData> LoadFromInnerAndCache(CacheEntry cacheEntry, BotStoreType botStoreType, IAddress key, CancellationToken token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency id="Autofac" version="3.5.2"/>
<dependency id="Chronic.Signed" version="0.3.2" />
<dependency id="Microsoft.AspNet.WebAPI.Core" version="5.2.3" />
<dependency id="Microsoft.Bot.Connector" version="3.15.2.3" />
<dependency id="Microsoft.Bot.Connector" version="3.15.3.0" />
<dependency id="Microsoft.Rest.ClientRuntime" version="2.3.2" />
<dependency id="Newtonsoft.Json" version="9.0.1" />
<dependency id="System.IdentityModel.Tokens.Jwt" version="5.1.4" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>Microsoft.Bot.Connector.AspNetCore</id>
<version>1.1.3.16</version>
<version>1.1.3.17</version>
<authors>Microsoft</authors>
<owners>microsoft, BotFramework, nugetbotbuilder </owners>
<iconUrl>https://bots.botframework.com/Client/Images/bot-framework-default-7.png</iconUrl>
Expand All @@ -22,7 +22,7 @@
<dependency id="Newtonsoft.Json" version="9.0.1" />
<dependency id="System.IdentityModel.Tokens.Jwt" version="5.1.4" />
<dependency id="Microsoft.AspNetCore.Mvc.Core" version="1.1.4" />
<dependency id="Microsoft.Bot.Connector" version="3.15.2.3" />
<dependency id="Microsoft.Bot.Connector" version="3.15.3.0" />
</group>
</dependencies>
</metadata>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>Microsoft.Bot.Connector.AspNetCore</id>
<version>2.0.1.8</version>
<version>2.0.1.9</version>
<authors>Microsoft</authors>
<owners>microsoft, BotFramework, nugetbotbuilder </owners>
<iconUrl>https://bots.botframework.com/Client/Images/bot-framework-default-7.png</iconUrl>
Expand All @@ -19,7 +19,7 @@
<dependency id="Microsoft.AspNetCore.Authentication.JwtBearer" version="2.0.0" />
<dependency id="Microsoft.AspNetCore.Mvc" version="2.0.0" />
<dependency id="Microsoft.Rest.ClientRuntime" version="2.3.10" />
<dependency id="Microsoft.Bot.Connector" version="3.15.2.3" />
<dependency id="Microsoft.Bot.Connector" version="3.15.3.0" />
</group>
</dependencies>
</metadata>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>Microsoft.Bot.Connector</id>
<version>3.15.2.3</version>
<version>3.15.3.0</version>
<authors>Microsoft</authors>
<owners>microsoft, BotFramework, nugetbotbuilder </owners>
<iconUrl>https://bots.botframework.com/Client/Images/bot-framework-default-7.png</iconUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">C:\Github\Microsoft\BotBuilder\CSharp\Library\Microsoft.Bot.Connector.NetCore\project.lock.json</ProjectAssetsFile>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">C:\source\github\BotBuilder\CSharp\Library\Microsoft.Bot.Connector.NetCore\project.lock.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\V-BRHALE\.nuget\packages\</NuGetPackageFolders>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\tomlm\.nuget\packages\</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">ProjectJson</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">4.7.0</NuGetToolVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -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)]
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>Microsoft.Bot.Connector</id>
<version>3.15.2.3</version>
<version>3.15.3.0</version>
<authors>Microsoft</authors>
<owners>microsoft, BotFramework, nugetbotbuilder </owners>
<iconUrl>https://bots.botframework.com/Client/Images/bot-framework-default-7.png</iconUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Original file line number Diff line number Diff line change
@@ -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)]

0 comments on commit 57562ab

Please sign in to comment.