Last active
December 24, 2015 16:49
-
-
Save imogenkinsman/6831326 to your computer and use it in GitHub Desktop.
an example of caching
This file contains 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
class Memcached | |
attr_writer :expiration | |
def initialize | |
@cache = {} | |
@expiration = Float::MAX | |
end | |
def set(key, &value) | |
@cache[key] = {value: value.call, time: Time.new} | |
end | |
def get(key, &value) | |
if [email protected]?(key) || (Time.new - @cache[key][:time] >= @expiration) | |
@cache[key] = {value: value.call, time: Time.new} | |
end | |
@cache[key][:value] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment