Skip to content

Instantly share code, notes, and snippets.

@granolocks
Created January 27, 2017 15:02
Show Gist options
  • Select an option

  • Save granolocks/3b2a35c3ea7486cb12b2003b7b5afeab to your computer and use it in GitHub Desktop.

Select an option

Save granolocks/3b2a35c3ea7486cb12b2003b7b5afeab to your computer and use it in GitHub Desktop.
cache value with class variables and time out (simple pattern)
module ValueCache
@@last_set = 0
@@last_val = nil
def get_val
# do not re-process more than once every 10s
if Time.now.to_i - 10 > @@last_set
@@last_val = [1,2,3,4,5,6].sample
@@last_set = Time.now.to_i
end
@@last_val
end
module_function :get_val
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment