cache_in.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. module Sessions::CacheIn
  2. # rubocop:disable Style/ClassVars
  3. @@data = {}
  4. @@data_time = {}
  5. @@expires_in = {}
  6. @@expires_in_ttl = {}
  7. # rubocop:enable Style/ClassVars
  8. def self.delete(key)
  9. @@data.delete(key)
  10. @@data_time.delete(key)
  11. end
  12. def self.set(key, value, params = {})
  13. if params[:expires_in]
  14. @@expires_in[key] = Time.zone.now + params[:expires_in]
  15. @@expires_in_ttl[key] = params[:expires_in]
  16. end
  17. @@data[ key ] = value
  18. @@data_time[ key ] = Time.zone.now
  19. end
  20. def self.expired(key, params = {})
  21. # expire if value never was set
  22. return true if !@@data.include? key
  23. # ignore_expire
  24. return false if params[:ignore_expire]
  25. # set re_expire
  26. if params[:re_expire]
  27. if @@expires_in[key]
  28. @@expires_in[key] = Time.zone.now + @@expires_in_ttl[key]
  29. end
  30. return false
  31. end
  32. # check if expired
  33. if @@expires_in[key]
  34. return true if @@expires_in[key] < Time.zone.now
  35. return false
  36. end
  37. # return false if key was set without expires_in
  38. false
  39. end
  40. def self.get(key, params = {})
  41. return if expired( key, params)
  42. @@data[ key ]
  43. end
  44. end