Skip to content

Commit

Permalink
Cache refactor, to have cache reset option accessible for unit tests …
Browse files Browse the repository at this point in the history
…(build fix: clear cache after each test).
  • Loading branch information
jwaliszko committed Oct 13, 2015
1 parent d2acc1a commit e892cb6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public void Setup()
new HttpRequest(string.Empty, "http://tempuri.org", string.Empty),
new HttpResponse(new StringWriter())
);

MapCache.Instance.Clear();
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<ItemGroup>
<Compile Include="Providers\ExpressiveAnnotationsModelValidatorProvider.cs" />
<Compile Include="RequestStorage.cs" />
<Compile Include="MapCache.cs" />
<Compile Include="Validators\AssertThatValidator.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Validators\ExpressiveValidator.cs" />
Expand Down
44 changes: 44 additions & 0 deletions src/ExpressiveAnnotations.MvcUnobtrusive/MapCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* https://github.com/JaroslawWaliszko/ExpressiveAnnotations
* Copyright (c) 2014 Jaroslaw Waliszko
* Licensed MIT: http://opensource.org/licenses/MIT */

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace ExpressiveAnnotations.MvcUnobtrusive
{
internal class MapCache
{
private static readonly MapCache _instance = new MapCache();
private static readonly ConcurrentDictionary<string, CacheItem> _cache = new ConcurrentDictionary<string, CacheItem>();

private MapCache()
{
}

public static MapCache Instance
{
get { return _instance; }
}

public CacheItem GetOrAdd(string key, Func<string, CacheItem> func)
{
return _cache.GetOrAdd(key, func);
}

public void Clear()
{
_cache.Clear();
}
}

internal class CacheItem
{
public IDictionary<string, string> FieldsMap { get; set; }
public IDictionary<string, object> ConstsMap { get; set; }
public IDictionary<string, string> ParsersMap { get; set; }
public IDictionary<string, Guid> ErrFieldsMap { get; set; }
public string FormattedErrorMessage { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Licensed MIT: http://opensource.org/licenses/MIT */

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics;
Expand All @@ -20,8 +19,6 @@ namespace ExpressiveAnnotations.MvcUnobtrusive.Validators
/// <typeparam name="T">Any type derived from <see cref="ExpressiveAttribute" /> class.</typeparam>
public abstract class ExpressiveValidator<T> : DataAnnotationsModelValidator<T> where T : ExpressiveAttribute
{
private static readonly ConcurrentDictionary<string, CacheItem> _cache = new ConcurrentDictionary<string, CacheItem>();

/// <summary>
/// Constructor for expressive model validator.
/// </summary>
Expand All @@ -38,7 +35,7 @@ protected ExpressiveValidator(ModelMetadata metadata, ControllerContext context,
var attribId = string.Format("{0}.{1}", attribute.TypeId, annotatedField).ToLowerInvariant();
FieldAttributeType = string.Format("{0}.{1}", typeof(T).FullName, annotatedField).ToLowerInvariant();

var item = _cache.GetOrAdd(attribId, _ =>
var item = MapCache.Instance.GetOrAdd(attribId, _ =>
{
var parser = new Parser();
parser.RegisterMethods();
Expand Down Expand Up @@ -192,14 +189,5 @@ private void AssertAttribsQuantityAllowed(int count)
throw new InvalidOperationException(
string.Format("No more than {0} unique attributes of the same type can be applied for a single field or property.", max));
}

private class CacheItem
{
public IDictionary<string, string> FieldsMap { get; set; }
public IDictionary<string, object> ConstsMap { get; set; }
public IDictionary<string, string> ParsersMap { get; set; }
public IDictionary<string, Guid> ErrFieldsMap { get; set; }
public string FormattedErrorMessage { get; set; }
}
}
}

0 comments on commit e892cb6

Please sign in to comment.