What languages ​​support return value caching without a template code?

For methods where ...

  • there is a static one-to-one mapping between input and output and
  • the cost of creating an output facility is relatively high, and
  • the method is called multiple times with the same input

... there is a need to cache result values.

In my code, the result caching pattern is repeated many times (pseudo-code in Java, but the question is agnostic):

private static Map<Input, Output> fooResultMap = new HashMap<Input, Output>();
public getFoo(Input input) {
  if (fooResultMap.get(input) != null) {
    return fooResultMap.get(input);
  }
  Output output = null;
  // Some code to obtain the object since we don't have it in the cache.
  fooResultMap.put(input, output);
  return output;
}

Repeating this structure all the time is a clear violation of the DRY principle.

Ideally, I would like the code above to be reduced to the following:

@CacheResult
public getFoo(Input input) {
  Output output = null;
  // Some code to obtain the object since we don't have it in the cache.
  return output;
}

If the theoretical annotation CacheResult takes care of the caching, which I now do manually.

The general term for this type of caching is memoization . "

, , "Memoize" .

Memoize ( , )? - , Java .NET?

+5
9

, CPAN Memoize Perl, :

   # Compute Fibonacci numbers
    sub fib {
      my $n = shift;
      return $n if $n < 2;
      fib($n-1) + fib($n-2);
    }

    use Memoize;
    memoize('fib');
+4

- .Net 'Enterprise Library'

http://msdn.microsoft.com/en-us/library/cc511757.aspx

[CachingCallHandler(0, 0, 30)]
public decimal GetSavingsBalance(int accountNumber)
{
  // Code here to extract balance from database.
  return balance;
}
+2

Python , . decorator module, ( ), JVM, .NET.

+1

Spring, springmodules Java.

Springmodules - 0.8, , . Spring, , , . :

public class TigerCacheableService implements CacheableService {

  @Cacheable(modelId = "testCaching")
  public final String getName(int index) {
    // some implementation.
  }
...
}

. , , ehcache, Spring. ehcache ( / ) , @Cacheable.

+1

@CacheResult Java, , , ASM memoization.

0

Microsoft T-SQL CLR . ...

( , CLR.)

0

, , , OSCache (Java ) . .. , .

"check cache", "return cached" "create and add to cache".

-1

Java , Java

private static final Cache<Input, Output> fooCache = Caches.newInstance(
    new Factory<Input, Output>() { public Output create(Input input) {
        return ... some code ...;
    }}
);
public static Output getFoo(Input input) {
    return fooCache.get(input);
}

, :

private static final Cache<Input, Output> fooCache =
    (Input input) (... some code ...);
public static Output getFoo(Input input) {
    return fooCache.get(input);
}

, AOP-, .

-1

/ Memoization #. , , ReaderWriterLock.

:

public static Func<A, R> Memoize<A, R>(this Func<A, R> f)
{
  var map = new Dictionary<A, R>();
  return a =>
    {
      R value;
      if (map.TryGetValue(a, out value))
        return value;
      value = f(a);
      map.Add(a, value);
      return value;
    };
}
-1

All Articles