Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Sdl.Web.Common/packages.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSBuild.Extension.Pack" version="1.9.1" targetFramework="net462" />
<package id="Microsoft.AspNet.Mvc" version="5.3.0" targetFramework="net452" />
<package id="Microsoft.AspNet.Razor" version="3.3.0" targetFramework="net452" />
<package id="Microsoft.AspNet.WebPages" version="3.3.0" targetFramework="net452" />
<package id="Microsoft.Web.Infrastructure" version="2.0.0.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net452" />
<package id="NLog" version="5.2.7" targetFramework="net462" />
<package id="MSBuild.Extension.Pack" version="1.9.1" targetFramework="net48" />
<package id="Microsoft.AspNet.Mvc" version="5.3.0" targetFramework="net48" />
<package id="Microsoft.AspNet.Razor" version="3.3.0" targetFramework="net48" />
<package id="Microsoft.AspNet.WebPages" version="3.3.0" targetFramework="net48" />
<package id="Microsoft.Web.Infrastructure" version="2.0.0.0" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
<package id="NLog" version="5.2.7" targetFramework="net48" />
<package id="Sdl.Dxa.DataModel" version="2.3.0" targetFramework="net48" />
</packages>
12 changes: 6 additions & 6 deletions Sdl.Web.Mvc/packages.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HtmlAgilityPack" version="1.4.9.4" targetFramework="net452" />
<package id="Microsoft.AspNet.Mvc" version="5.3.0" targetFramework="net452" />
<package id="Microsoft.AspNet.Razor" version="3.3.0" targetFramework="net452" />
<package id="Microsoft.AspNet.WebPages" version="3.3.0" targetFramework="net452" />
<package id="Microsoft.Web.Infrastructure" version="2.0.0.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net452" />
<package id="HtmlAgilityPack" version="1.4.9.4" targetFramework="net48" />
<package id="Microsoft.AspNet.Mvc" version="5.3.0" targetFramework="net48" />
<package id="Microsoft.AspNet.Razor" version="3.3.0" targetFramework="net48" />
<package id="Microsoft.AspNet.WebPages" version="3.3.0" targetFramework="net48" />
<package id="Microsoft.Web.Infrastructure" version="2.0.0.0" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
</packages>
78 changes: 28 additions & 50 deletions Sdl.Web.Tridion/Providers/Caching/KeylockCacheProvider.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using AsyncKeyedLock;
using Sdl.Web.Common;
using Sdl.Web.Common.Interfaces;
using Sdl.Web.Delivery.Caching;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Sdl.Web.Tridion.Caching
{
Expand All @@ -26,35 +25,26 @@ namespace Sdl.Web.Tridion.Caching
/// </summary>
public class KeylockCacheProvider : ICacheProvider
{
private static readonly ConcurrentDictionary<string, object> KeyLocks = new ConcurrentDictionary<string, object>();
private static readonly AsyncKeyedLocker<string> _asyncKeyedLocker = new AsyncKeyedLocker<string>(o =>
{
o.PoolSize = 20;
o.PoolInitialFill = 1;
});

private readonly ICacheProvider<object> _cilCacheProvider = CacheFactory<object>.CreateFromConfiguration();

[ThreadStatic]
private static int _reentriesCount;

public void Store<T>(string key, string region, T value, IEnumerable<string> dependencies = null)
{
Debug.Assert(_cilCacheProvider != null, "_cilCacheProvider != null");

// prevent deadlocks if we're storing something here and in GetOrAdd.
var hash = CalcLockHash(key, region);
lock (KeyLocks.GetOrAdd(hash, new object()))
using (_asyncKeyedLocker.Lock(hash))
{
try
{
T cachedValue;
// only add if we are sure it hasn't already been added
if (!TryGetCachedValue(key, region, out cachedValue))
{
_cilCacheProvider.Set(key, value, region);
}
}
finally
// only add if we are sure it hasn't already been added
if (!TryGetCachedValue(key, region, out T cachedValue))
{
// We don't need the lock anymore
object tempKeyLock;
KeyLocks.TryRemove(hash, out tempKeyLock);
_cilCacheProvider.Set(key, value, region);
}
}
}
Expand All @@ -63,37 +53,25 @@ public void Store<T>(string key, string region, T value, IEnumerable<string> dep

public T GetOrAdd<T>(string key, string region, Func<T> addFunction, IEnumerable<string> dependencies = null)
{
T cachedValue;
if (TryGetCachedValue(key, region, out cachedValue)) return cachedValue;
if (TryGetCachedValue(key, region, out T cachedValue)) return cachedValue;
var hash = CalcLockHash(key, region);
lock (KeyLocks.GetOrAdd(hash, new object()))
using (_asyncKeyedLocker.Lock(hash))
{
try
{
// Try and get from cache again in case it has been added in the meantime
if (TryGetCachedValue(key, region, out cachedValue)) return cachedValue;
// Try and get from cache again in case it has been added in the meantime
if (TryGetCachedValue(key, region, out cachedValue)) return cachedValue;

// Still null, so lets run Func()
Interlocked.Increment(ref _reentriesCount);
cachedValue = addFunction();
if (cachedValue != null)
// Still null, so lets run Func()
cachedValue = addFunction();
if (cachedValue != null)
{
// Note that dependencies are not used?
Debug.Assert(_cilCacheProvider != null, "_cilCacheProvider != null");
if (_cilCacheProvider.Get(key, region) == null)
{
// Note that dependencies are not used?
Debug.Assert(_cilCacheProvider != null, "_cilCacheProvider != null");
if (_cilCacheProvider.Get(key, region) == null)
{
_cilCacheProvider.Set(key, cachedValue, region);
}

return cachedValue;
_cilCacheProvider.Set(key, cachedValue, region);
}
}
finally
{
Interlocked.Decrement(ref _reentriesCount);
// We don't need the lock anymore
object tempKeyLock;
KeyLocks.TryRemove(hash, out tempKeyLock);

return cachedValue;
}
}
return default(T);
Expand All @@ -117,6 +95,6 @@ private bool TryGetCachedValue<T>(string key, string region, out T value)
return false;
}

private static string CalcLockHash(string key, string region) => $"{region}:{key}:{_reentriesCount}";
private static string CalcLockHash(string key, string region) => $"{region}:{key}";
}
}
10 changes: 10 additions & 0 deletions Sdl.Web.Tridion/Sdl.Web.Tridion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
<Compile Include="Providers\Caching\KeylockCacheProvider.cs" />
<Compile Include="Providers\ModelService\SitemapHelpers.cs" />
<Compile Include="Resolvers\Localization\GraphQLMashupLocalizationResolver.cs" />
<Reference Include="AsyncKeyedLock, Version=7.0.1.0, Culture=neutral, PublicKeyToken=c6dde91429ba0f2f, processorArchitecture=MSIL">
<HintPath>..\packages\AsyncKeyedLock.7.0.1\lib\netstandard2.0\AsyncKeyedLock.dll</HintPath>
</Reference>
<Reference Include="DD4T.ContentModel">
<HintPath>..\packages\DD4T.Core.2.2.7\lib\net45\DD4T.ContentModel.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -166,13 +169,19 @@
<Reference Include="System.IO.Compression" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.InteropServices.RuntimeInformation">
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
</Reference>
<Reference Include="System.Spatial">
<HintPath>..\packages\System.Spatial.5.8.4\lib\net40\System.Spatial.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Web.Mvc">
Expand Down Expand Up @@ -261,6 +270,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
Expand Down
31 changes: 31 additions & 0 deletions Sdl.Web.Tridion/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.OData.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.5.4.30215" newVersion="7.5.4.30215" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
41 changes: 22 additions & 19 deletions Sdl.Web.Tridion/packages.config
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DD4T.Core" version="2.2.7" targetFramework="net452" />
<package id="DD4T.Model" version="2.2.2" targetFramework="net452" />
<package id="Microsoft.Data.Edm" version="5.8.4" targetFramework="net462" />
<package id="Microsoft.Data.OData" version="5.8.4" targetFramework="net462" />
<package id="Microsoft.Data.Services.Client" version="5.8.4" targetFramework="net462" />
<package id="Microsoft.Extensions.Caching.Abstractions" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net452" />
<package id="Microsoft.Extensions.Caching.Memory" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net452" />
<package id="Microsoft.Extensions.Caching.Redis" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net452" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net452" />
<package id="Microsoft.Extensions.Options" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net452" />
<package id="Microsoft.Extensions.Primitives" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net452" />
<package id="Microsoft.OData.Client" version="7.6.3" targetFramework="net462" />
<package id="Microsoft.OData.Core" version="7.6.3" targetFramework="net462" />
<package id="Microsoft.OData.Edm" version="7.6.3" targetFramework="net462" />
<package id="Microsoft.Spatial" version="7.6.3" targetFramework="net462" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net462" />
<package id="AsyncKeyedLock" version="7.0.1" targetFramework="net48" />
<package id="DD4T.Core" version="2.2.7" targetFramework="net48" />
<package id="DD4T.Model" version="2.2.2" targetFramework="net48" />
<package id="Microsoft.Data.Edm" version="5.8.4" targetFramework="net48" />
<package id="Microsoft.Data.OData" version="5.8.4" targetFramework="net48" />
<package id="Microsoft.Data.Services.Client" version="5.8.4" targetFramework="net48" />
<package id="Microsoft.Extensions.Caching.Abstractions" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net48" />
<package id="Microsoft.Extensions.Caching.Memory" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net48" />
<package id="Microsoft.Extensions.Caching.Redis" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net48" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net48" />
<package id="Microsoft.Extensions.Options" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net48" />
<package id="Microsoft.Extensions.Primitives" version="1.1.0" allowedVersions="[1.1.0]" targetFramework="net48" />
<package id="Microsoft.OData.Client" version="7.6.3" targetFramework="net48" />
<package id="Microsoft.OData.Core" version="7.6.3" targetFramework="net48" />
<package id="Microsoft.OData.Edm" version="7.6.3" targetFramework="net48" />
<package id="Microsoft.Spatial" version="7.6.3" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
<package id="Sdl.Dxa.DataModel" version="2.3.0" targetFramework="net48" />
<package id="Sdl.Tridion.Api.Client" version="2.3.0" targetFramework="net48" />
<package id="Sdl.Web.Context.Client" version="12.0.1197" targetFramework="net48" />
<package id="Sdl.Web.Context.Image" version="12.0.1197" targetFramework="net48" />
<package id="Sdl.Web.Delivery" version="12.0.1197" targetFramework="net48" />
<package id="Sdl.Tridion.Api.Client" version="2.3.0" targetFramework="net48" />
<package id="StackExchange.Redis.StrongName" version="1.1.605" allowedVersions="[1.1.605]" targetFramework="net452" />
<package id="System.Spatial" version="5.8.4" targetFramework="net462" />
<package id="StackExchange.Redis.StrongName" version="1.1.605" allowedVersions="[1.1.605]" targetFramework="net48" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net48" />
<package id="System.Spatial" version="5.8.4" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
</packages>
Loading