Advertisement
NameL3ss

example buffer redis

Aug 11th, 2020 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. app/services/buffer_store.rb
  2. class BufferStore
  3. NAMESPACE = "waypoints".to_sym
  4. REDIS_WAYPOINTS = Redis.new(url: REDIS_HOST, namespace: namespace)
  5. attr_reader :waypoints, :provider_id
  6.  
  7. def initialize(waypoints = [], provider_id)
  8. @waypoints = waypoints
  9. @provider_id = provider_id
  10. end
  11.  
  12. def store_in_redis
  13. return if waypoints.empty?
  14.  
  15. redis_waypoints.hset("waypoints:#{provider}", 'data', waypoints.to_json) # store as array of hashes
  16. redis_waypoints.hgetall("waypoints:105") #fetch all by provider
  17. #waypoints_value.each do |el|
  18. # REDIS_WAYPOINTS.set("#{SecureRandom.hex(10)}-provider:#{provider_id}", el)
  19. #end
  20. end
  21.  
  22. def waypoints_by_provider
  23. redis_waypoints.hgetall("waypoints:105").to_a.map{|el| [el[0], JSON.parse(el[1])] }
  24. #REDIS_WAYPOINTS.keys("waypoints:*-provider:#{provider_id}").map{|el| JSON.parse(redis.get(el)) }
  25. end
  26.  
  27. def remove_waypoints_by_provider
  28. REDIS_WAYPOINTS.keys("waypoints:*-provider:#{provider_id}").map{|el| redis.del(el) }
  29. end
  30.  
  31. def total_waypoints
  32. redis_waypoints.hgetall("waypoints:105").to_a.map{|el| t = JSON.parse(el[1]); t.respond_to?(:keys) ? 1: t.size }.sum
  33. #REDIS_WAYPOINTS.keys("waypoints:*-provider:#{provider_id}").size
  34. end
  35. private
  36.  
  37. def waypoints_value
  38. waypoints.to_json
  39. end
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement