JPad
Snippets
Download
Help
Contact
JPad.cache Function
Jpad.cache can be used to prevent repeating expensive calls on each run The callable only gets ran if a cache entry does not already exist.
Code
// Uncomment either one of these lines and it forces the calculation to be done each time. //JPad.cacheClear("b"); //JPad.cacheClearAll(); Callable<Integer> c = () -> { Dump("calcing"); return 22; }; // By using Cache callable, the function only gets called to generate the value once. // The remainder of the time, it is retrieved from cache. Integer b = JPad.cache("b", Integer.class, c); b = JPad.cache("b", Integer.class, c); b = JPad.cache("b", Integer.class, c); b = JPad.cache("b", Integer.class, c); Dump("b = " + b);
Result
Console
b = 22
Bytecode
Compiled from "RunnContainer.java" public class io.jpad.scratch.RunnContainer { public io.jpad.scratch.RunnContainer(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String...) throws java.lang.Exception; Code: 0: invokedynamic #2, 0 // InvokeDynamic #0:call:()Ljava/util/concurrent/Callable; 5: astore_1 6: ldc #3 // String b 8: ldc #4 // class java/lang/Integer 10: aload_1 11: invokestatic #5 // Method io/jpad/scratch/JPad.cache:(Ljava/lang/String;Ljava/lang/Class;Ljava/util/concurrent/Callable;)Ljava/lang/Object; 14: checkcast #4 // class java/lang/Integer 17: astore_2 18: ldc #3 // String b 20: ldc #4 // class java/lang/Integer 22: aload_1 23: invokestatic #5 // Method io/jpad/scratch/JPad.cache:(Ljava/lang/String;Ljava/lang/Class;Ljava/util/concurrent/Callable;)Ljava/lang/Object; 26: checkcast #4 // class java/lang/Integer 29: astore_2 30: ldc #3 // String b 32: ldc #4 // class java/lang/Integer 34: aload_1 35: invokestatic #5 // Method io/jpad/scratch/JPad.cache:(Ljava/lang/String;Ljava/lang/Class;Ljava/util/concurrent/Callable;)Ljava/lang/Object; 38: checkcast #4 // class java/lang/Integer 41: astore_2 42: ldc #3 // String b 44: ldc #4 // class java/lang/Integer 46: aload_1 47: invokestatic #5 // Method io/jpad/scratch/JPad.cache:(Ljava/lang/String;Ljava/lang/Class;Ljava/util/concurrent/Callable;)Ljava/lang/Object; 50: checkcast #4 // class java/lang/Integer 53: astore_2 54: new #6 // class java/lang/StringBuilder 57: dup 58: invokespecial #7 // Method java/lang/StringBuilder."<init>":()V 61: ldc #8 // String b = 63: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 66: aload_2 67: invokevirtual #10 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder; 70: invokevirtual #11 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 73: invokestatic #12 // Method io/jpad/scratch/Dumper.Dump:(Ljava/lang/Object;)Ljava/lang/Object; 76: pop 77: return private static java.lang.Integer lambda$main$0() throws java.lang.Exception; Code: 0: ldc #13 // String calcing 2: invokestatic #12 // Method io/jpad/scratch/Dumper.Dump:(Ljava/lang/Object;)Ljava/lang/Object; 5: pop 6: bipush 22 8: invokestatic #14 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer; 11: areturn }
Jpad Version:1.06
viewCount:4094
Creator:
admin
Latest Editor:
admin
Tags
jpad
cache
Download
Copy As Markdown:
**code [JPad.cache Function.java](/example/1A/jpad-cache-function)** // Uncomment either one of these lines and it forces the calculation to be done each time. //JPad.cacheClear("b"); //JPad.cacheClearAll(); Callable
c = () -> { Dump("calcing"); return 22; }; // By using Cache callable, the function only gets called to generate the value once. // The remainder of the time, it is retrieved from cache. Integer b = JPad.cache("b", Integer.class, c); b = JPad.cache("b", Integer.class, c); b = JPad.cache("b", Integer.class, c); b = JPad.cache("b", Integer.class, c); Dump("b = " + b); **Result:** b = 22