cache.rb 843 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. module Cache
  2. =begin
  3. delete a cache
  4. Cache.delete('some_key')
  5. =end
  6. def self.delete(key)
  7. Rails.cache.delete(key.to_s)
  8. end
  9. =begin
  10. write a cache
  11. Cache.write(
  12. 'some_key',
  13. { some: { data: { 'structure' } } },
  14. { expires_in: 24.hours, # optional, default 7 days }
  15. )
  16. =end
  17. def self.write(key, data, params = {})
  18. params[:expires_in] ||= 7.days
  19. # in certain cases, caches are deleted by other thread at same
  20. # time, just log it
  21. Rails.cache.write(key.to_s, data, params)
  22. rescue => e
  23. Rails.logger.error "Can't write cache #{key}: #{e.inspect}"
  24. Rails.logger.error e
  25. end
  26. =begin
  27. get a cache
  28. value = Cache.get('some_key')
  29. =end
  30. def self.get(key)
  31. Rails.cache.read(key.to_s)
  32. end
  33. =begin
  34. clear whole cache store
  35. Cache.clear
  36. =end
  37. def self.clear
  38. Rails.cache.clear
  39. end
  40. end