Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app/services/buffer_store.rb
- class BufferStore
- NAMESPACE = "waypoints".to_sym
- REDIS_WAYPOINTS = Redis.new(url: REDIS_HOST, namespace: namespace)
- attr_reader :waypoints, :provider_id
- def initialize(waypoints = [], provider_id)
- @waypoints = waypoints
- @provider_id = provider_id
- end
- def store_in_redis
- return if waypoints.empty?
- redis_waypoints.hset("waypoints:#{provider}", 'data', waypoints.to_json) # store as array of hashes
- redis_waypoints.hgetall("waypoints:105") #fetch all by provider
- #waypoints_value.each do |el|
- # REDIS_WAYPOINTS.set("#{SecureRandom.hex(10)}-provider:#{provider_id}", el)
- #end
- end
- def waypoints_by_provider
- redis_waypoints.hgetall("waypoints:105").to_a.map{|el| [el[0], JSON.parse(el[1])] }
- #REDIS_WAYPOINTS.keys("waypoints:*-provider:#{provider_id}").map{|el| JSON.parse(redis.get(el)) }
- end
- def remove_waypoints_by_provider
- REDIS_WAYPOINTS.keys("waypoints:*-provider:#{provider_id}").map{|el| redis.del(el) }
- end
- def total_waypoints
- redis_waypoints.hgetall("waypoints:105").to_a.map{|el| t = JSON.parse(el[1]); t.respond_to?(:keys) ? 1: t.size }.sum
- #REDIS_WAYPOINTS.keys("waypoints:*-provider:#{provider_id}").size
- end
- private
- def waypoints_value
- waypoints.to_json
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement