Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I use the following extension methods to make cached versions of delegates:</p> <pre><code> public static Func&lt;T, TResult&gt; AsCached&lt;T, TResult&gt;(this Func&lt;T, TResult&gt; function) { var cachedResults = new Dictionary&lt;T, TResult&gt;(); return (argument) =&gt; { TResult result; lock (cachedResults) { if (!cachedResults.TryGetValue(argument, out result)) { result = function(argument); cachedResults.Add(argument, result); } } return result; }; } public static Func&lt;T1, T2, TResult&gt; AsCached&lt;T1, T2, TResult&gt;(this Func&lt;T1, T2, TResult&gt; function) { var cachedResults = new Dictionary&lt;Tuple&lt;T1, T2&gt;, TResult&gt;(); return (value1, value2) =&gt; { TResult result; var paramsTuple = new Tuple&lt;T1, T2&gt;(value1, value2); lock(cachedResults) { if (!cachedResults.TryGetValue(paramsTuple, out result)) { result = function(value1, value2); cachedResults.Add(paramsTuple, result); } } return result; }; } public static Func&lt;T1, T2, T3, TResult&gt; AsCached&lt;T1, T2, T3, TResult&gt;(this Func&lt;T1, T2, T3, TResult&gt; function) { var cachedResults = new Dictionary&lt;Tuple&lt;T1, T2, T3&gt;, TResult&gt;(); return (value1, value2, value3) =&gt; { TResult result; var paramsTuple = new Tuple&lt;T1, T2, T3&gt;(value1, value2, value3); lock(cachedResults) { if (!cachedResults.TryGetValue(paramsTuple, out result)) { result = function(value1, value2, value3); cachedResults.Add(paramsTuple, result); } } return result; }; } </code></pre> <p>And so on for N parameters...</p> <p>In case it's not clear from the code, I create a tuple with the arguments, and use the tuple as a key to a dictionary that holds the return values for each set of arguments. Note that every time you call <code>AsCached</code>, you create a separate cache.</p> <p>You can use these methods as follows:</p> <pre><code>private Func&lt;int, SomeEntity&gt; _getCached; public SomeEntity Get(int id) { if (_getCached == null) { Func&lt;int, SomeEntity&gt; func = GetImpl; _getCached = func.AsCached(); } return _getCached(id); } private SomeEntity GetImpl(int id) { return base.GetItem&lt;SomeEntity&gt; ("select * from SomeEntities where id = @idParam", new { idParam = id}); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload