txt: ## is-20cached.md
  is cached
  
  Combined from primary sources listed below.
  
  # [In Routine](#___top "go to top of document")[§](#(Routine)_trait_is_cached "direct link")
  
  See primary documentation
  [in context](/type/Routine#trait_is_cached)
  for **trait is cached**.
  
  ```raku
  multi trait_mod:<is>(Routine $r, :$cached!)
  ```
  
  Causes the return value of a routine to be stored, so that when subsequent calls with the same list of arguments are made, the stored value can be returned immediately instead of re-running the routine.[[1]](#fn1)
  
  Useful when storing and returning the computed value is much faster than re-computing it every time, and when the time saved trumps the cost of the use of more memory.
  
  Even if the arguments passed to the routine are "reference types" (such as objects or arrays), then for the purpose of caching they will only be compared based on their contents. Thus the second invocation will hit the cache in this case:
  
  ```raku
  say foo( [1, 2, 3] );   # runs foo
  say foo( [1, 2, 3] );   # doesn't run foo, uses cached value
  
  ```
  
  Since it's still at the experimental stage, you will have to insert the `use experimental :cached;` statement in any module or script that uses it.
  
  ```raku
  use experimental :cached;
  
  sub nth-prime(Int:D $x where * > 0) is cached {
      say "Calculating {$x}th prime";
      return (2..*).grep(*.is-prime)[$x - 1];
  }
  
  say nth-prime(43);
  say nth-prime(43);
  say nth-prime(43);
  
  ```
  
  produces this output:
  
  ｢text｣ without highlighting
  
  ```
  
  
  ```
  
  Calculating 43th prime
  191
  191
  191
  
  
  ```
  
  
  ```
  
  **Note**: This feature is not thread-safe.
  