Skip to content

Commit

Permalink
Added xmlDoc to get doc files for nugets. Fixed some code style issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaCo committed Sep 9, 2016
1 parent 9d01a76 commit 1d2ade3
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/CacheManager.Core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"buildOptions": {
"allowUnsafe": false,
"languageVersion": "csharp6",
"warningsAsErrors": true
"warningsAsErrors": true,
"xmlDoc": true
},
"copyright": "Copyright (c) 2015 MichaConrad",
"configurations": {
Expand Down
3 changes: 2 additions & 1 deletion src/CacheManager.Couchbase/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"buildOptions": {
"allowUnsafe": false,
"languageVersion": "csharp6",
"warningsAsErrors": true
"warningsAsErrors": true,
"xmlDoc": true
},
"copyright": "Copyright (c) 2015 MichaConrad",
"configurations": {
Expand Down
3 changes: 2 additions & 1 deletion src/CacheManager.Memcached/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"buildOptions": {
"allowUnsafe": false,
"languageVersion": "csharp6",
"warningsAsErrors": true
"warningsAsErrors": true,
"xmlDoc": true
},
"copyright": "Copyright (c) 2015 MichaConrad",
"configurations": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@

namespace CacheManager.MicrosoftCachingMemory
{
/// <summary>
/// Extensions for the configuration builder specific to Microsoft.Extensions.Caching.Memory cache handle.
/// </summary>
public static class MemoryCacheExtensions
{
/// <summary>
/// Extension method to check if a key exists in the given <paramref name="cache"/> instance.
/// </summary>
/// <param name="cache">The cache instance.</param>
/// <param name="key">The key.</param>
/// <returns><c>True</c> if the key exists.</returns>
public static bool Contains(this MemoryCache cache, object key)
{
object temp;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using CacheManager.Core;
using CacheManager.Core.Internal;
Expand All @@ -10,6 +9,10 @@

namespace CacheManager.MicrosoftCachingMemory
{
/// <summary>
/// Implementation of a cache handle using <see cref="Microsoft.Extensions.Caching.Memory"/>.
/// </summary>
/// <typeparam name="TCacheValue">The type of the cache value.</typeparam>
public class MemoryCacheHandle<TCacheValue> : BaseCacheHandle<TCacheValue>
{
private const string DefaultName = "default";
Expand All @@ -19,10 +22,18 @@ public class MemoryCacheHandle<TCacheValue> : BaseCacheHandle<TCacheValue>

private volatile MemoryCache cache = null;

/// <inheritdoc/>
public override int Count => this.cache.Count;

/// <inheritdoc/>
protected override ILogger Logger { get; }

/// <summary>
/// Initializes a new instance of the <see cref="MemoryCacheHandle{TCacheValue}"/> class.
/// </summary>
/// <param name="managerConfiguration">The manager configuration.</param>
/// <param name="configuration">The cache handle configuration.</param>
/// <param name="loggerFactory">The logger factory.</param>
public MemoryCacheHandle(CacheManagerConfiguration managerConfiguration, CacheHandleConfiguration configuration, ILoggerFactory loggerFactory)
: base(managerConfiguration, configuration)
{
Expand All @@ -37,31 +48,37 @@ public MemoryCacheHandle(CacheManagerConfiguration managerConfiguration, CacheHa
this.CreateInstanceToken();
}

/// <inheritdoc/>
public override void Clear()
{
this.cache.Remove(this.instanceKey);
this.CreateInstanceToken();
}

/// <inheritdoc/>
public override void ClearRegion(string region)
{
var regionTokenKey = this.GetRegionTokenKey(region);
this.cache.RemoveChilds(regionTokenKey);
this.cache.Remove(regionTokenKey);
}

/// <inheritdoc/>
protected override CacheItem<TCacheValue> GetCacheItemInternal(string key)
{
return this.GetCacheItemInternal(key, null);
}

/// <inheritdoc/>
protected override CacheItem<TCacheValue> GetCacheItemInternal(string key, string region)
{
string fullKey = this.GetItemKey(key, region);
var item = this.cache.Get(fullKey) as CacheItem<TCacheValue>;

if (item == null)
{
return null;
}

if (IsExpired(item))
{
Expand All @@ -78,25 +95,34 @@ protected override CacheItem<TCacheValue> GetCacheItemInternal(string key, strin
return item;
}

/// <inheritdoc/>
protected override bool RemoveInternal(string key)
{
return this.RemoveInternal(key, null);
}

/// <inheritdoc/>
protected override bool RemoveInternal(string key, string region)
{
var fullKey = this.GetItemKey(key, region);
bool result = this.cache.Contains(fullKey);
if (result) this.cache.Remove(fullKey);
if (result)
{
this.cache.Remove(fullKey);
}

return result;
}

/// <inheritdoc/>
protected override bool AddInternalPrepared(CacheItem<TCacheValue> item)
{
var key = this.GetItemKey(item);

if (this.cache.Contains(key))
{
return false;
}

var options = this.GetOptions(item);
this.cache.Set(key, item, options);
Expand All @@ -105,6 +131,7 @@ protected override bool AddInternalPrepared(CacheItem<TCacheValue> item)
return true;
}

/// <inheritdoc/>
protected override void PutInternalPrepared(CacheItem<TCacheValue> item)
{
var key = this.GetItemKey(item);
Expand All @@ -126,6 +153,7 @@ private void CreateInstanceToken()
AbsoluteExpiration = DateTimeOffset.MaxValue,
SlidingExpiration = TimeSpan.MaxValue,
};

options.RegisterPostEvictionCallback(this.InstanceTokenRemoved);
this.cache.Set(this.instanceKey, new HashSet<object>(), options);
}
Expand All @@ -135,7 +163,10 @@ private void InstanceTokenRemoved(object key, object value, EvictionReason reaso
{
var set = (HashSet<object>)value;
foreach (var item in set)
{
this.cache.Remove(item);
}

this.instanceKey = Guid.NewGuid().ToString();
}

Expand Down Expand Up @@ -184,7 +215,9 @@ private MemoryCacheEntryOptions GetOptions(CacheItem<TCacheValue> item)
var key = this.GetRegionTokenKey(item.Region);

if (!this.cache.Contains(key))
{
this.CreateRegionToken(item.Region);
}
}

var options = new MemoryCacheEntryOptions()
Expand Down Expand Up @@ -221,17 +254,22 @@ private void CreateRegionToken(string region)
AbsoluteExpiration = DateTimeOffset.MaxValue,
SlidingExpiration = TimeSpan.MaxValue,
};

this.cache.Set(key, new HashSet<object>(), options);
}

private void ItemRemoved(object key, object value, EvictionReason reason, object state)
{
var strKey = key as string;
if (string.IsNullOrWhiteSpace(strKey))
{
return;
}

if (reason == EvictionReason.Removed)
{
return;
}

if (strKey.Contains(":"))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using CacheManager.MicrosoftCachingMemory;

namespace CacheManager.Core
Expand All @@ -11,14 +10,39 @@ public static class MicrosoftMemoryCachingBuilderExtensions
{
private const string DefaultName = "default";

/// <summary>
/// Adds a <see cref="MemoryCacheHandle{TCacheValue}" /> using a <see cref="Microsoft.Extensions.Caching.Memory"/> instance with the given <paramref name="instanceName"/>.
/// </summary>
/// <param name="part">The builder part.</param>
/// <param name="instanceName">The name to be used for the <see cref="Microsoft.Extensions.Caching.Memory"/> instance.</param>
/// <returns>The builder part.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="instanceName"/> is null.</exception>
public static ConfigurationBuilderCacheHandlePart WithMicrosoftMemoryCacheHandle(
this ConfigurationBuilderCachePart part, string instanceName)
=> WithMicrosoftMemoryCacheHandle(part, instanceName, false);

/// <summary>
/// Adds a <see cref="MemoryCacheHandle{TCacheValue}" /> using a <see cref="Microsoft.Extensions.Caching.Memory"/>.
/// The name of the cache instance will be 'default'.
/// </summary>
/// <param name="part">The builder part.</param>
/// <returns>The builder part.</returns>
public static ConfigurationBuilderCacheHandlePart WithMicrosoftMemoryCacheHandle(
this ConfigurationBuilderCachePart part)
=> part?.WithHandle(typeof(MemoryCacheHandle<>), DefaultName, false);

/// <summary>
/// Adds a <see cref="MemoryCacheHandle{TCacheValue}" /> using a <see cref="Microsoft.Extensions.Caching.Memory"/> instance with the given <paramref name="instanceName"/>.
/// </summary>
/// <param name="part">The builder part.</param>
/// <param name="instanceName">The name to be used for the cache instance.</param>
/// <param name="isBackplaneSource">Set this to true if this cache handle should be the source of the backplane.
/// This setting will be ignored if no backplane is configured.</param>
/// <returns>
/// The builder part.
/// </returns>
/// <exception cref="System.ArgumentNullException">If part is null.</exception>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="instanceName"/> is null.</exception>
public static ConfigurationBuilderCacheHandlePart WithMicrosoftMemoryCacheHandle(
this ConfigurationBuilderCachePart part, string instanceName, bool isBackplaneSource)
=> part?.WithHandle(typeof(MemoryCacheHandle<>), instanceName, isBackplaneSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"buildOptions": {
"allowUnsafe": false,
"languageVersion": "csharp6",
"warningsAsErrors": true
"warningsAsErrors": true,
"xmlDoc": true
},
"copyright": "Copyright (c) 2015 MichaConrad",
"configurations": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"buildOptions": {
"allowUnsafe": false,
"languageVersion": "csharp6",
"warningsAsErrors": true
"warningsAsErrors": true,
"xmlDoc": true
},
"copyright": "Copyright (c) 2015 MichaConrad",
"configurations": {
Expand Down
3 changes: 2 additions & 1 deletion src/CacheManager.Microsoft.Extensions.Logging/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"buildOptions": {
"allowUnsafe": false,
"languageVersion": "csharp6",
"warningsAsErrors": true
"warningsAsErrors": true,
"xmlDoc": true
},
"copyright": "Copyright (c) 2015 MichaConrad",
"configurations": {
Expand Down
3 changes: 2 additions & 1 deletion src/CacheManager.Serialization.Json/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"buildOptions": {
"allowUnsafe": false,
"languageVersion": "csharp6",
"warningsAsErrors": true
"warningsAsErrors": true,
"xmlDoc": true
},
"copyright": "Copyright (c) 2015 MichaConrad",
"configurations": {
Expand Down
3 changes: 1 addition & 2 deletions src/CacheManager.Serialization.ProtoBuf/ProtoBufCacheItem.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;

using CacheManager.Core;
using ProtoBuf;

namespace CacheManager.Serialization.ProtoBuf
{
[ProtoContract]
public class ProtoBufCacheItem
internal class ProtoBufCacheItem
{
[ProtoMember(1)]
public DateTime CreatedUtc { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace CacheManager.Core
{
/// <summary>
/// Configuration builder extensions for the <c>ProtoBuf</c> based <see cref="CacheManager.Core.Internal.ICacheSerializer"/>.
/// </summary>
public static class ProtoBufConfigurationBuilderExtensions
{
/// <summary>
Expand All @@ -11,7 +14,7 @@ public static class ProtoBufConfigurationBuilderExtensions
/// <returns>The builder instance.</returns>
public static ConfigurationBuilderCachePart WithProtoBufSerializer(this ConfigurationBuilderCachePart part)
{
Utility.Guard.NotNull<ConfigurationBuilderCachePart>(part, nameof(part));
Utility.Guard.NotNull(part, nameof(part));

return part.WithSerializer(typeof(ProtoBufSerializer));
}
Expand Down
10 changes: 10 additions & 0 deletions src/CacheManager.Serialization.ProtoBuf/ProtoBufSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@

namespace CacheManager.Serialization.ProtoBuf
{
/// <summary>
/// Implements the <see cref="ICacheSerializer"/> contract using <c>ProtoBuf</c>.
/// </summary>
public class ProtoBufSerializer : ICacheSerializer
{
private static readonly Type cacheItemType = typeof(ProtoBufCacheItem);

/// <summary>
/// Initializes a new instance of the <see cref="ProtoBufSerializer"/> class.
/// </summary>
public ProtoBufSerializer()
{
}

/// <inheritdoc/>
public object Deserialize(byte[] data, Type target)
{
byte[] destination;
Expand All @@ -34,6 +41,7 @@ public object Deserialize(byte[] data, Type target)
}
}

/// <inheritdoc/>
public CacheItem<T> DeserializeCacheItem<T>(byte[] value, Type valueType)
{
var item = (ProtoBufCacheItem)Deserialize(value, cacheItemType);
Expand All @@ -46,6 +54,7 @@ public CacheItem<T> DeserializeCacheItem<T>(byte[] value, Type valueType)
return item.ToCacheItem<T>(cachedValue);
}

/// <inheritdoc/>
public byte[] Serialize<T>(T value)
{
byte[] output = null;
Expand All @@ -62,6 +71,7 @@ public byte[] Serialize<T>(T value)
return prefix.Concat(output).ToArray();
}

/// <inheritdoc/>
public byte[] SerializeCacheItem<T>(CacheItem<T> value)
{
var cachedValue = Serialize(value.Value);
Expand Down
3 changes: 2 additions & 1 deletion src/CacheManager.Serialization.ProtoBuf/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"buildOptions": {
"allowUnsafe": false,
"languageVersion": "csharp6",
"warningsAsErrors": true
"warningsAsErrors": true,
"xmlDoc": true
},
"copyright": "Copyright (c) 2015 MichaConrad",
"configurations": {
Expand Down
Loading

0 comments on commit 1d2ade3

Please sign in to comment.