Created
January 27, 2017 15:02
-
-
Save granolocks/3b2a35c3ea7486cb12b2003b7b5afeab to your computer and use it in GitHub Desktop.
cache value with class variables and time out (simple pattern)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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