Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removing dependency on System.Runtime.Caching (from Mono) for the client

  • Loading branch information...
commit 41baba7ae5a6459e5923b0cd905f3e890117b39e 1 parent 35738f3
@ayende ayende authored
View
10 Raven.Client.Lightweight/Connection/HttpJsonRequestFactory.cs
@@ -2,10 +2,10 @@
using System.Collections.Specialized;
using System.Net;
using System.Threading;
-using FromMono.System.Runtime.Caching;
using Raven.Abstractions.Extensions;
using Raven.Client.Connection.Profiling;
using Raven.Client.Document;
+using Raven.Client.Util;
using Raven.Json.Linq;
namespace Raven.Client.Connection
@@ -36,7 +36,7 @@ internal void InvokeLogRequest(IHoldProfilingInformation sender, RequestResultAr
handler(sender, e);
}
- private MemoryCache cache;
+ private SimpleCache cache;
internal int NumOfCachedRequests;
@@ -100,7 +100,7 @@ public void ResetCache()
if (cache != null)
cache.Dispose();
- cache = new MemoryCache(typeof(HttpJsonRequest).FullName + ".Cache", new NameValueCollection());
+ cache = new SimpleCache();
NumOfCachedRequests = 0;
}
@@ -140,7 +140,7 @@ public bool DisableHttpCaching
public HttpJsonRequestFactory()
{
- cache = new MemoryCache(typeof(HttpJsonRequest).FullName + ".Cache", new NameValueCollection());
+ cache = new SimpleCache();
}
#else
[ThreadStatic] private static TimeSpan? aggressiveCacheDuration;
@@ -192,7 +192,7 @@ internal void CacheResponse(string url, string text, NameValueCollection headers
Data = text,
Time = DateTimeOffset.Now,
Headers = new NameValueCollection(headers)
- }, new CacheItemPolicy()); // cache as much as possible, for as long as possible, using the default cache limits
+ });
}
/// <summary>
View
21 Raven.Client.Lightweight/Connection/Profiling/ConcurrentBoundedList.cs
@@ -8,11 +8,13 @@ namespace Raven.Client.Connection.Profiling
internal class ConcurrentLruLSet<T>
{
private readonly int maxCapacity;
+ private readonly Action<T> onDrop;
private LinkedList<T> items = new LinkedList<T>();
- public ConcurrentLruLSet(int maxCapacity)
+ public ConcurrentLruLSet(int maxCapacity, Action<T> onDrop = null)
{
this.maxCapacity = maxCapacity;
+ this.onDrop = onDrop;
}
public T FirstOrDefault(Func<T, bool> predicate)
@@ -32,16 +34,27 @@ public void Push(T item)
newList.Remove(item);
newList.AddLast(item);
- if(newList.Count > maxCapacity)
+ LinkedListNode<T> linkedListNode = null;
+ if (newList.Count > maxCapacity)
{
+ linkedListNode = newList.First;
newList.RemoveFirst();
}
- if (Interlocked.CompareExchange(ref items, newList, current) == current)
- return;
+ if (Interlocked.CompareExchange(ref items, newList, current) != current)
+ continue;
+ if (onDrop != null && linkedListNode != null)
+ onDrop(linkedListNode.Value);
+
+ return;
} while (true);
}
+
+ public void Clear()
+ {
+ items = new LinkedList<T>();
+ }
}
}
View
5 Raven.Client.Lightweight/Raven.Client.Lightweight.csproj
@@ -49,9 +49,6 @@
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.VisualBasic" />
- <Reference Include="MissingBitsFromClientProfile">
- <HintPath>..\SharedLibs\MissingBitsFromClientProfile.dll</HintPath>
- </Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\SharedLibs\Newtonsoft.Json.dll</HintPath>
</Reference>
@@ -196,8 +193,10 @@
<Compile Include="Shard\ShardStrategy\ShardSelection\IShardSelectionStrategy.cs" />
<Compile Include="Shard\ShardStrategy\ShardStrategy.cs" />
<Compile Include="StoredEntityEventArgs.cs" />
+ <Compile Include="Util\ConcurrentDictionary.cs" />
<Compile Include="Util\Inflector.cs" />
<Compile Include="Util\ObjectReferenceEqualityComparerer.cs" />
+ <Compile Include="Util\SimpleCache.cs" />
<Compile Include="WhereEqualsParams.cs" />
</ItemGroup>
<ItemGroup>
View
53 Raven.Client.Lightweight/Util/ConcurrentDictionary.cs
@@ -0,0 +1,53 @@
+#if NET_3_5
+using System;
+using System.Collections.Generic;
+
+namespace Raven.Client.Util
+{
+ public class ConcurrentDictionary<K,V>
+ {
+ readonly Dictionary<K,V> items = new Dictionary<K, V>();
+
+ public void AddOrUpdate(K key, V actualValue, Func<K,V,V> updateFactory)
+ {
+ lock(items)
+ {
+ V existing;
+ if (items.TryGetValue(key, out existing))
+ {
+ items[key] = updateFactory(key, existing);
+ }
+ else
+ {
+ items[key] = actualValue;
+ }
+ }
+ }
+
+ public void Clear()
+ {
+ lock(items)
+ {
+ items.Clear();
+ }
+ }
+
+ public bool TryGetValue(K key, out V value)
+ {
+ lock (items)
+ {
+ return items.TryGetValue(key, out value);
+ }
+ }
+
+ public void TryRemove(K key, out V val)
+ {
+ lock (items)
+ {
+ if (items.TryGetValue(key, out val))
+ items.Remove(key);
+ }
+ }
+ }
+}
+#endif
View
44 Raven.Client.Lightweight/Util/SimpleCache.cs
@@ -0,0 +1,44 @@
+using System;
+#if !NET_3_5
+using System.Collections.Concurrent;
+#endif
+using Raven.Client.Connection.Profiling;
+
+namespace Raven.Client.Util
+{
+ public class SimpleCache : IDisposable
+ {
+ readonly ConcurrentLruLSet<string> lruKeys;
+ readonly ConcurrentDictionary<string, object> actualCache;
+
+ public SimpleCache()
+ {
+ actualCache = new ConcurrentDictionary<string, object>();
+ lruKeys = new ConcurrentLruLSet<string>(2048, key =>
+ {
+ object _;
+ actualCache.TryRemove(key, out _);
+ });
+ }
+
+ public void Set(string key, object val)
+ {
+ actualCache.AddOrUpdate(key, val, (s, o) => val);
+ lruKeys.Push(key);
+ }
+
+ public object Get(string key)
+ {
+ object value;
+ if(actualCache.TryGetValue(key, out value))
+ lruKeys.Push(key);
+ return value;
+ }
+
+ public void Dispose()
+ {
+ lruKeys.Clear();
+ actualCache.Clear();
+ }
+ }
+}
View
BIN  SharedLibs/MissingBitsFromClientProfile.dll
Binary file not shown
View
BIN  SharedLibs/MissingBitsFromClientProfile.pdb
Binary file not shown
View
6 default.ps1
@@ -17,16 +17,16 @@ properties {
$server_files = @( "Raven.Server.exe", "Raven.Json.???", "Raven.Studio.xap", "nlog.???", "Newtonsoft.Json.???", "Lucene.Net.???", "Spatial.Net.???", "SpellChecker.Net.???", "ICSharpCode.NRefactory.???", "Rhino.Licensing.???", `
"Esent.Interop.???", "Raven.Abstractions.???", "Raven.Database.???", "Raven.Http.???", "Raven.Storage.Esent.???", "Raven.Storage.Managed.???", "Raven.Munin.???" );
- $client_dlls_3_5 = @( "nlog.???","Newtonsoft.Json.Net35.???", "Raven.Abstractions-3.5.???", "Raven.Json-3.5.???", "Raven.Client.Lightweight-3.5.???", "MissingBitsFromClientProfile.???" );
+ $client_dlls_3_5 = @( "nlog.???","Newtonsoft.Json.Net35.???", "Raven.Abstractions-3.5.???", "Raven.Json-3.5.???", "Raven.Client.Lightweight-3.5.???");
$client_dlls = @( "nlog.???","Raven.Client.MvcIntegration.???", "Newtonsoft.Json.???","Raven.Json.???", "Raven.Abstractions.???", "Raven.Client.Lightweight.???", "Raven.Client.Debug.???", `
- "MissingBitsFromClientProfile.???", "AsyncCtpLibrary.???" );
+ "AsyncCtpLibrary.???" );
$silverlight_dlls = @( "Raven.Client.Silverlight.???", "AsyncCtpLibrary_Silverlight.???", "MissingBitFromSilverlight.???", "Newtonsoft.Json.Silverlight.???");
$all_client_dlls = @( "Raven.Client.Lightweight.???", "Raven.Client.Embedded.???", "Raven.Abstractions.???", "Raven.Http.???", "Raven.Database.???", "Raven.Json.???", `
"Esent.Interop.???", "ICSharpCode.NRefactory.???", "Lucene.Net.???", "Spatial.Net.???", "SpellChecker.Net.???", "nlog.???", "Newtonsoft.Json.???", `
- "Raven.Storage.Esent.???", "Raven.Storage.Managed.???", "Raven.Munin.???", "AsyncCtpLibrary.???", "MissingBitsFromClientProfile.???", "Raven.Studio.xap" );
+ "Raven.Storage.Esent.???", "Raven.Storage.Managed.???", "Raven.Munin.???", "AsyncCtpLibrary.???", "Raven.Studio.xap" );
$test_prjs = @("Raven.Tests.dll", "Raven.Client.VisualBasic.Tests.dll", "Raven.Bundles.Tests.dll" );
}
Please sign in to comment.
Something went wrong with that request. Please try again.