module Lucky::Memoizable

Direct including types

Defined in:

lucky/memoizable.cr

Macro Summary

Macro Detail

macro memoize(method_def) #

Caches the return value of the method. Helpful for expensive methods that are called more than once.

To memoize a method, prefix it with memoize:

class BrowserAction
  memoize def current_user : User
    # Get the current user
  end
end

This will fetch the user record on the first current_user call, then each subsequent call returns the user record.

The memoize method will raise a compile time exception if you forget to include a return type for your method, or if any arguments are missing a type. The result of a set of arguments is only kept until the passed arguments change. Once they change, passing previous arguments will re-run the memoized method. Equality (==) is used for checking on argument updates.


[View source]