Guest User

Untitled

a guest
May 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #
  2. # Assumes that MEMCACHE is instantiated, using the gem, use the following lines in environment.rb:
  3. #
  4. # require 'memcached'
  5. # MEMCACHE = Memcached.new("localhost:11211")
  6. #
  7. # Usage:
  8. #
  9. # require 'httparty'
  10. # require 'httparty_icebox'
  11. # require 'httparty_icebox_memcached'
  12. #
  13. # class Foo
  14. # include HTTParty
  15. # include HTTParty::Icebox
  16. # cache :store => 'memcached', :timeout => 3600*1 # cached 1 hour
  17. # end
  18. #
  19. #
  20.  
  21. require 'httparty_icebox'
  22.  
  23. include HTTParty::Icebox::Store
  24.  
  25. class MemcachedStore < AbstractStore
  26. include HTTParty::Icebox
  27.  
  28. def initialize(options={})
  29. super;self
  30. end
  31. def set(key, value)
  32. res = MEMCACHE.set(key, value, @timeout)
  33. #puts "MemcachedStore.set, key: #{key}, result: #{res}"
  34. Cache.logger.info("Cache: set (#{key})")
  35. true
  36. end
  37. def get(key)
  38. data = MEMCACHE.get(key) rescue nil
  39. Cache.logger.info("Cache: #{data.nil? ? "miss" : "hit"} (#{key})")
  40. data
  41. end
  42. def exists?(key)
  43. data = MEMCACHE.get(key) rescue nil
  44. !data.nil?
  45. end
  46. def stale?(key)
  47. return true unless exists?(key)
  48. end
  49. end
Add Comment
Please, Sign In to add comment