Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

One thought I had: Would I benefit from making it inherit from IDictionary<> or from ConcurrentDictionary? From what I hear, you shouldn't inherit from collections you shouldn't inherit from collections, and I don't plan on using this for other purposes, so I'm not sure if it's worth it to make this more robust.

One thought I had: Would I benefit from making it inherit from IDictionary<> or from ConcurrentDictionary? From what I hear, you shouldn't inherit from collections, and I don't plan on using this for other purposes, so I'm not sure if it's worth it to make this more robust.

One thought I had: Would I benefit from making it inherit from IDictionary<> or from ConcurrentDictionary? From what I hear, you shouldn't inherit from collections, and I don't plan on using this for other purposes, so I'm not sure if it's worth it to make this more robust.

added 963 characters in body
Source Link
DLeh
  • 300
  • 3
  • 11
public static class FuncUtil_TimeSensitive
{
 public static readonly TimeSpan ValidTime = TimeSpan.FromHours(6);

 public static Func<A, R> Memoize<A, R>(Func<A, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A) });
 return a => map.GetOrAdd(new { a }, new Lazy<R>(() => f(a))).Value;
 }
 public static Func<A, B, R> Memoize<A, B, R>(this Func<A, B, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A), b = default(B) });
 return (a, b) => map.GetOrAdd(new { a, b }, new Lazy<R>(() => f(a, b))).Value;
 }
 //other overloads for more parametersmethods
 static class DictionaryHelper<Value>
 {
 private static readonly TimeSpan _validTime = new TimeSpan(6, 0, 0);
 public static TimeSensitiveConcurrentDictionary<Key, Value> CreateConcurrent<Key>(Key prototype)
 {
 return new TimeSensitiveConcurrentDictionary<Key, Value>(_validTimeValidTime);
 }
 }
}
public/// class<summary>
/// TimeSensitiveConcurrentDictionary<AStores a value based on the key for a set amoutn of time. After that time has passed, R>accessing the same key will remove the value
{/// </summary>
/// <typeparam name="TKey">Type to privateuse readonlyas ConcurrentDictionary<A,a DateTime>key</typeparam>
/// cacheExpirationDates<typeparam =name="TValue">Type newto ConcurrentDictionary<Ause as a value</typeparam>
public class TimeSensitiveConcurrentDictionary<TKey, DateTime>();TValue>
{
 private readonly ConcurrentDictionary<AConcurrentDictionary<TKey, R>TimeSensitiveConcurrentDictionaryValue<TValue>> cache = new ConcurrentDictionary<AConcurrentDictionary<TKey, R>TimeSensitiveConcurrentDictionaryValue<TValue>>();
 private readonly TimeSpan expirationTime;
 public TimeSensitiveConcurrentDictionary(TimeSpan validTime)
 {
 expirationTime = validTime;
 }
 public RTValue GetOrAdd(ATKey akey, RTValue rnewValue)
 {
 var now = DateTime.Now;
 bool needsToUpdate = false;
 var currentExpirationDate = now.Add(expirationTime);
 if (cache.ContainsKey(akey))
 {
 var expirationDateTimecurrentExpirationDate = cacheExpirationDates[a];cache[key].ExpirationDate;
 if (now > expirationDateTimecurrentExpirationDate)
 {
 TryRemove(akey);
 needsToUpdate = true;
 }
 }
 else
 {
 needsToUpdate = true;
 }
 if (needsToUpdate)
 {
 cacheExpirationDates[a]currentExpirationDate = now.Add(expirationTime);
 }
 return cache.GetOrAdd(akey, rnew TimeSensitiveConcurrentDictionaryValue<TValue>(currentExpirationDate, newValue);).Value;
 }
 public bool TryRemove(ATKey a)
 {
 RTimeSensitiveConcurrentDictionaryValue<TValue> result;
 return cache.TryRemove(a, out result);
 }
 private struct TimeSensitiveConcurrentDictionaryValue<TTSValue>
 {
 private DateTime oldDate;expirationDate;
 cacheExpirationDates.TryRemove(a,private outTTSValue oldDate);value;

 public DateTime ExpirationDate { get { return cache.TryRemoveexpirationDate; } }
 public TTSValue Value { get { return value; } }
 public TimeSensitiveConcurrentDictionaryValue(aDateTime expirationDate, outTTSValue resultvalue);
 {
 this.expirationDate = expirationDate;
 this.value = value;
 }
 }
}
public static class FuncUtil_TimeSensitive
{
 public static Func<A, R> Memoize<A, R>(Func<A, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A) });
 return a => map.GetOrAdd(new { a }, new Lazy<R>(() => f(a))).Value;
 }
 public static Func<A, B, R> Memoize<A, B, R>(this Func<A, B, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A), b = default(B) });
 return (a, b) => map.GetOrAdd(new { a, b }, new Lazy<R>(() => f(a, b))).Value;
 }
 //other overloads for more parameters
 static class DictionaryHelper<Value>
 {
 private static readonly TimeSpan _validTime = new TimeSpan(6, 0, 0);
 public static TimeSensitiveConcurrentDictionary<Key, Value> CreateConcurrent<Key>(Key prototype)
 {
 return new TimeSensitiveConcurrentDictionary<Key, Value>(_validTime);
 }
 }
}
public class TimeSensitiveConcurrentDictionary<A, R>
{
 private readonly ConcurrentDictionary<A, DateTime> cacheExpirationDates = new ConcurrentDictionary<A, DateTime>();
 private readonly ConcurrentDictionary<A, R> cache = new ConcurrentDictionary<A, R>();
 private readonly TimeSpan expirationTime;
 public TimeSensitiveConcurrentDictionary(TimeSpan validTime)
 {
 expirationTime = validTime;
 }
 public R GetOrAdd(A a, R r)
 {
 var now = DateTime.Now;
 bool needsToUpdate = false;
 if (cache.ContainsKey(a))
 {
 var expirationDateTime = cacheExpirationDates[a];
 if (now > expirationDateTime)
 {
 TryRemove(a);
 needsToUpdate = true;
 }
 }
 else
 {
 needsToUpdate = true;
 }
 if (needsToUpdate)
 {
 cacheExpirationDates[a] = now.Add(expirationTime);
 }
 return cache.GetOrAdd(a, r);
 }
 public bool TryRemove(A a)
 {
 R result;
 DateTime oldDate;
 cacheExpirationDates.TryRemove(a, out oldDate);
 return cache.TryRemove(a, out result);
 }
}
public static class FuncUtil_TimeSensitive
{
 public static readonly TimeSpan ValidTime = TimeSpan.FromHours(6);

 public static Func<A, R> Memoize<A, R>(Func<A, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A) });
 return a => map.GetOrAdd(new { a }, new Lazy<R>(() => f(a))).Value;
 }
 public static Func<A, B, R> Memoize<A, B, R>(this Func<A, B, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A), b = default(B) });
 return (a, b) => map.GetOrAdd(new { a, b }, new Lazy<R>(() => f(a, b))).Value;
 }
 //other methods
 static class DictionaryHelper<Value>
 {
 public static TimeSensitiveConcurrentDictionary<Key, Value> CreateConcurrent<Key>(Key prototype)
 {
 return new TimeSensitiveConcurrentDictionary<Key, Value>(ValidTime);
 }
 }
}
/// <summary>
/// Stores a value based on the key for a set amoutn of time. After that time has passed, accessing the same key will remove the value
/// </summary>
/// <typeparam name="TKey">Type to use as a key</typeparam>
/// <typeparam name="TValue">Type to use as a value</typeparam>
public class TimeSensitiveConcurrentDictionary<TKey, TValue>
{
 private readonly ConcurrentDictionary<TKey, TimeSensitiveConcurrentDictionaryValue<TValue>> cache = new ConcurrentDictionary<TKey, TimeSensitiveConcurrentDictionaryValue<TValue>>();
 private readonly TimeSpan expirationTime;
 public TimeSensitiveConcurrentDictionary(TimeSpan validTime)
 {
 expirationTime = validTime;
 }
 public TValue GetOrAdd(TKey key, TValue newValue)
 {
 var now = DateTime.Now;
 bool needsToUpdate = false;
 var currentExpirationDate = now.Add(expirationTime);
 if (cache.ContainsKey(key))
 {
 currentExpirationDate = cache[key].ExpirationDate;
 if (now > currentExpirationDate)
 {
 TryRemove(key);
 needsToUpdate = true;
 }
 }
 else
 {
 needsToUpdate = true;
 }
 if (needsToUpdate)
 {
 currentExpirationDate = now.Add(expirationTime);
 }
 return cache.GetOrAdd(key, new TimeSensitiveConcurrentDictionaryValue<TValue>(currentExpirationDate, newValue)).Value;
 }
 public bool TryRemove(TKey a)
 {
 TimeSensitiveConcurrentDictionaryValue<TValue> result;
 return cache.TryRemove(a, out result);
 }
 private struct TimeSensitiveConcurrentDictionaryValue<TTSValue>
 {
 private DateTime expirationDate;
 private TTSValue value;

 public DateTime ExpirationDate { get { return expirationDate; } }
 public TTSValue Value { get { return value; } }
 public TimeSensitiveConcurrentDictionaryValue(DateTime expirationDate, TTSValue value)
 {
 this.expirationDate = expirationDate;
 this.value = value;
 }
 }
}
added 62 characters in body
Source Link
DLeh
  • 300
  • 3
  • 11
public static class FuncUtil_TimeSensitive
{
 public static Func<A, R> Memoize<A, R>(Func<A, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A) });
 return a => map.GetOrAdd(new { a }, new Lazy<R>(() => f(a))).Value;
 }
 public static Func<A, B, R> Memoize<A, B, R>(this Func<A, B, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A), b = default(B) });
 return (a, b) => map.GetOrAdd(new { a, b }, new Lazy<R>(() => f(a, b))).Value;
 }
 //other overloads for more parameters
 static class DictionaryHelper<Value>
 {
 private static readonly TimeSpan _validTime = new TimeSpan(6, 0, 0);
 public static TimeSensitiveConcurrentDictionary<Key, Value> CreateConcurrent<Key>(Key prototype)
 {
 return new TimeSensitiveConcurrentDictionary<Key, Value>(_validTime);
 }
 }
}
public class TimeSensitiveConcurrentDictionary<A, R>
{
 private readonly ConcurrentDictionary<A, DateTime> cacheExpirationDates = new ConcurrentDictionary<A, DateTime>();
 private readonly ConcurrentDictionary<A, R> cache = new ConcurrentDictionary<A, R>();
 private readonly TimeSpan expirationTime;
 public TimeSensitiveConcurrentDictionary(TimeSpan validTime)
 {
 expirationTime = validTime;
 }
 public R GetOrAdd(A a, R r)
 {
 var now = DateTime.Now;
 bool needsToUpdate = false;
 if (cache.ContainsKey(a))
 {
 var expirationDateTime = cacheExpirationDates[a];
 if (cacheExpirationDates[a]now > nowexpirationDateTime)
 {
 TryRemove(a);
 needsToUpdate = true;
 }
 }
 else
 {
 needsToUpdate = true;
 }
 if (needsToUpdate)
 {
 cacheExpirationDates[a] = now.Add(expirationTime);
 }
 return cache.GetOrAdd(a, r);
 }
 public bool TryRemove(A a)
 {
 R result;
 DateTime oldDate;
 cacheExpirationDates.TryRemove(a, out oldDate);
 return cache.TryRemove(a, out result);
 }
}
public static class FuncUtil_TimeSensitive
{
 public static Func<A, R> Memoize<A, R>(Func<A, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A) });
 return a => map.GetOrAdd(new { a }, new Lazy<R>(() => f(a))).Value;
 }
 public static Func<A, B, R> Memoize<A, B, R>(this Func<A, B, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A), b = default(B) });
 return (a, b) => map.GetOrAdd(new { a, b }, new Lazy<R>(() => f(a, b))).Value;
 }
 //other overloads for more parameters
 static class DictionaryHelper<Value>
 {
 private static readonly TimeSpan _validTime = new TimeSpan(6, 0, 0);
 public static TimeSensitiveConcurrentDictionary<Key, Value> CreateConcurrent<Key>(Key prototype)
 {
 return new TimeSensitiveConcurrentDictionary<Key, Value>(_validTime);
 }
 }
}
public class TimeSensitiveConcurrentDictionary<A, R>
{
 private readonly ConcurrentDictionary<A, DateTime> cacheExpirationDates = new ConcurrentDictionary<A, DateTime>();
 private readonly ConcurrentDictionary<A, R> cache = new ConcurrentDictionary<A, R>();
 private readonly TimeSpan expirationTime;
 public TimeSensitiveConcurrentDictionary(TimeSpan validTime)
 {
 expirationTime = validTime;
 }
 public R GetOrAdd(A a, R r)
 {
 var now = DateTime.Now;
 bool needsToUpdate = false;
 if (cache.ContainsKey(a))
 {
 if (cacheExpirationDates[a] > now)
 {
 TryRemove(a);
 needsToUpdate = true;
 }
 }
 else
 {
 needsToUpdate = true;
 }
 if (needsToUpdate)
 {
 cacheExpirationDates[a] = now.Add(expirationTime);
 }
 return cache.GetOrAdd(a, r);
 }
 public bool TryRemove(A a)
 {
 R result;
 DateTime oldDate;
 cacheExpirationDates.TryRemove(a, out oldDate);
 return cache.TryRemove(a, out result);
 }
}
public static class FuncUtil_TimeSensitive
{
 public static Func<A, R> Memoize<A, R>(Func<A, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A) });
 return a => map.GetOrAdd(new { a }, new Lazy<R>(() => f(a))).Value;
 }
 public static Func<A, B, R> Memoize<A, B, R>(this Func<A, B, R> f)
 {
 var map = DictionaryHelper<Lazy<R>>.CreateConcurrent(new { a = default(A), b = default(B) });
 return (a, b) => map.GetOrAdd(new { a, b }, new Lazy<R>(() => f(a, b))).Value;
 }
 //other overloads for more parameters
 static class DictionaryHelper<Value>
 {
 private static readonly TimeSpan _validTime = new TimeSpan(6, 0, 0);
 public static TimeSensitiveConcurrentDictionary<Key, Value> CreateConcurrent<Key>(Key prototype)
 {
 return new TimeSensitiveConcurrentDictionary<Key, Value>(_validTime);
 }
 }
}
public class TimeSensitiveConcurrentDictionary<A, R>
{
 private readonly ConcurrentDictionary<A, DateTime> cacheExpirationDates = new ConcurrentDictionary<A, DateTime>();
 private readonly ConcurrentDictionary<A, R> cache = new ConcurrentDictionary<A, R>();
 private readonly TimeSpan expirationTime;
 public TimeSensitiveConcurrentDictionary(TimeSpan validTime)
 {
 expirationTime = validTime;
 }
 public R GetOrAdd(A a, R r)
 {
 var now = DateTime.Now;
 bool needsToUpdate = false;
 if (cache.ContainsKey(a))
 {
 var expirationDateTime = cacheExpirationDates[a];
 if (now > expirationDateTime)
 {
 TryRemove(a);
 needsToUpdate = true;
 }
 }
 else
 {
 needsToUpdate = true;
 }
 if (needsToUpdate)
 {
 cacheExpirationDates[a] = now.Add(expirationTime);
 }
 return cache.GetOrAdd(a, r);
 }
 public bool TryRemove(A a)
 {
 R result;
 DateTime oldDate;
 cacheExpirationDates.TryRemove(a, out oldDate);
 return cache.TryRemove(a, out result);
 }
}
added 342 characters in body
Source Link
DLeh
  • 300
  • 3
  • 11
Loading
Source Link
DLeh
  • 300
  • 3
  • 11
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /