Skip to content

Instantly share code, notes, and snippets.

@imogenkinsman
Last active December 24, 2015 16:49
Show Gist options
  • Save imogenkinsman/6831326 to your computer and use it in GitHub Desktop.
Save imogenkinsman/6831326 to your computer and use it in GitHub Desktop.
an example of caching
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 !@cache.include?(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