Skip to content

Instantly share code, notes, and snippets.

@weppos
Created February 19, 2010 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weppos/308598 to your computer and use it in GitHub Desktop.
Save weppos/308598 to your computer and use it in GitHub Desktop.
module CacheKey
extend self
def expirable(range = :minute, time = ::Time.current)
key = case range
when :always
::Time.current
when :never
nil
when :second
time
when :minute
time.change(:sec => 0)
when :hour
time.change(:min => 0, :sec => 0)
when :day
time.change(:hour => 0, :min => 0, :sec => 0)
when :month
time.change(:day => 1, :hour => 0, :min => 0, :sec => 0)
when :year
time.change(:month => 1, :day => 1, :hour => 0, :min => 0, :sec => 0)
else
raise ArgumentError, "Unsupported expiration range `#{range}'"
end.to_i
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment