Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuke Immutable Collections #453

Merged
merged 3 commits into from
Apr 16, 2014
Merged
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
1 change: 0 additions & 1 deletion Octokit.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<dependencies>
<group>
<dependency id="Microsoft.Net.Http" />
<dependency id="Microsoft.Bcl.Immutable" />
</group>
<group targetFramework="net45">
</group>
Expand Down
36 changes: 9 additions & 27 deletions Octokit/Helpers/ConcurrentCache.cs
Original file line number Diff line number Diff line change
@@ -1,54 +1,36 @@
#if PORTABLE
using System;
using System.Collections.Immutable;
using System.Collections.Generic;
using System.Threading;

namespace Octokit
{
/// <summary>
/// This is in lieu of ConcurrentDictionary. PCL runtime, specifically windows phone 8
/// does not have access to ConcurrentDictionary.
/// Source of implementation is here:
/// http://stackoverflow.com/questions/18367839/alternative-to-concurrentdictionary-for-portable-class-library
/// Relies on Microsoft.BCL.Immutable for the ImmutableDictionary.
/// does not have access to ConcurrentDictionary. We just use a lock.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class ConcurrentCache<TKey, TValue>
{
IImmutableDictionary<TKey, TValue> _cache = ImmutableDictionary.Create<TKey, TValue>();
Dictionary<TKey, TValue> _cache = new Dictionary<TKey, TValue>();

public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory)
{
//cannot be null
if (valueFactory == null)
throw new ArgumentNullException("valueFactory");

TValue newValue = default(TValue);
bool newValueCreated = false;
while (true)
lock (_cache)
{
var oldCache = _cache;
TValue value;
if (oldCache.TryGetValue(key, out value))
return value;

// Value not found; create it if necessary
if (!newValueCreated)
if (_cache.ContainsKey(key))
{
newValue = valueFactory(key);
newValueCreated = true;
return _cache[key];
}

// Add the new value to the cache
var newCache = oldCache.Add(key, newValue);
if (Interlocked.CompareExchange(ref _cache, newCache, oldCache) == oldCache)
{
// Cache successfully written
return newValue;
}
// Failed to write the new cache because another thread
// already changed it; try again.
var ret = valueFactory(key);
_cache[key] = ret;
return ret;
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions Octokit/Octokit-Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,6 @@
</CodeAnalysisDictionary>
</ItemGroup>
<ItemGroup>
<Reference Include="System.Collections.Immutable">
<HintPath>..\packages\Microsoft.Bcl.Immutable.1.0.30\lib\portable-net45+win8+wp8\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http">
<HintPath>..\packages\Microsoft.Net.Http.2.2.18\lib\portable-net40+sl4+win8+wp71\System.Net.Http.dll</HintPath>
</Reference>
Expand All @@ -345,4 +342,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
118 changes: 0 additions & 118 deletions packages/Microsoft.Bcl.Immutable.1.0.30/License-Stable.rtf

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.