location-service-client 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/lib/location.rb +53 -22
  4. data/lib/version.rb +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2039f0d1911215ae70c7d3bf087a058957b3a619
4
- data.tar.gz: 3c93f9335247015659c65b45021833fe4ecd794f
3
+ metadata.gz: 44b1dcf91c7fa1c4b582032349577547fbd00fe6
4
+ data.tar.gz: 8a799c3134b32d07744f66aa3e69c32efc1fd633
5
5
  SHA512:
6
- metadata.gz: 8aca6a08dd4b5efae3b07351c67ff82e254c56e7c79f7bdd9fe6150e65e788044f9986a8138eacddbe9dd84318131a8f8f245f25b15dc86a93bd8f5a90a4ec74
7
- data.tar.gz: 3bddc36e797c7dc4b579d813d279e2829b4e0b4b175b1e0592a09f2501bb6ea9f302374614b243b4b9851e5c7d7f1feec7a7e97701bbe41da0c2408c25e8e234
6
+ metadata.gz: fa5da578d26aa90640c56e03d8b7bf09659251535d43e70e2629d0266d4920295bb2225b66e3c53d2750b70fd1038c0ce58afdd000ab2615ab2314646727c5eb
7
+ data.tar.gz: 496d678862cafce83cdbf99da8f8163ad0210fed5cb9fd112e8d0aa617023b4e2a2d44a0320b89f7dfb861811e67e403fa6e6275a3dbcd6dba2b0d2c040bd7ac
data/CHANGELOG.md CHANGED
@@ -1,4 +1,9 @@
1
1
 
2
+ 0.1.4 6/29/2016
3
+ ==============
4
+
5
+ Caching optimizations. Add benchmarking around finder methods.
6
+
2
7
  0.1.2 6/28/2016
3
8
  ==============
4
9
 
data/lib/location.rb CHANGED
@@ -20,51 +20,78 @@ class Location < Struct.new(:id, :sas_id, :name, :code, :kind, :lat, :lng, :time
20
20
  class << self
21
21
  # For mapping the JSON camelcase data from the location service into the Location structs:
22
22
  KEY_ORDER = [:id, :sasId, :name, :code, :kind, :lat, :lng, :timezone, :parentLocationId, :updatedAt, :createdAt, :childLocations, :beacons].map(&:to_s).freeze
23
- CACHE_KEY = 'location_service_request_cache'.freeze
24
- MEMOS_KEY = 'location_service_request_tables'.freeze
25
-
23
+ CACHE_PRE = "location_service_client".freeze
24
+ BENCHMARK = false.freeze
25
+
26
26
  def tables
27
27
  @t = nil if !testing? # test doesn't have a cache, so memoize in the test env
28
- @t ||= cache.fetch(MEMOS_KEY) do
29
- t = { id_table: {}, code_table: {} }
30
- flattened.each do |l|
28
+ @t ||= cache.fetch("#{CACHE_PRE}_memos") do
29
+ t = { id_table: [], code_table: {} }
30
+ flattened.sort_by(&:id).each do |l|
31
31
  t[:id_table][l.id] = l
32
32
  t[:code_table][l.code] = l
33
33
  end
34
34
  t
35
35
  end
36
- end
36
+ end
37
+
38
+ def bench # e.g. logb(caller[0])
39
+ if !BENCHMARK
40
+ yield
41
+ else
42
+ start = Time.now
43
+ v = yield
44
+ logger.fatal v.try(:name)
45
+ logger.fatal "#{caller[0].split(' ').last} #{(Time.now-start)*1000}ms"
46
+ v
47
+ end
48
+ end
37
49
 
38
50
  def find(id)
39
- tables[:id_table][id]
51
+ bench do
52
+ cache.fetch("#{CACHE_PRE}-id-#{id}") do
53
+ tables[:id_table][id]
54
+ end
55
+ end
40
56
  end
41
57
 
42
58
  def find_by_code(code)
43
- tables[:code_table][code]
59
+ bench do
60
+ cache.fetch("#{CACHE_PRE}-code-#{code}") do
61
+ tables[:code_table][code]
62
+ end
63
+ end
44
64
  end
45
65
 
46
66
  def find_by_parent_code_and_child_name(parent_code, child_name)
47
- airport = find_by_code(parent_code)
48
- airport.try(:child_locations) && airport.child_locations.detect {|l| l.name == child_name }
67
+ bench do
68
+ airport = find_by_code(parent_code)
69
+ airport.try(:child_locations) && airport.child_locations.detect {|l| l.name == child_name }
70
+ end
49
71
  end
50
72
  alias_method :find_by_airport_and_gate, :find_by_parent_code_and_child_name
51
73
 
52
74
  def find_by_codes(codes, child_locations=nil)
53
- child_locations ||= all
54
- codes = [codes].flatten
55
- code = codes.shift
56
- leaf = child_locations.detect{|l| l.code == code }
57
- codes.empty? ? leaf : find_by_codes(codes, leaf.child_locations)
75
+ bench do
76
+ child_locations ||= all
77
+ codes = [codes].flatten
78
+ code = codes.shift
79
+ leaf = child_locations.detect{|l| l.code == code }
80
+ codes.empty? ? leaf : find_by_codes(codes, leaf.child_locations)
81
+ end
58
82
  end
59
83
 
60
84
  def flattened
61
- (all + all.map {|l| l.child_locations}.compact).flatten
85
+ bench do
86
+ (all + all.map {|l| l.child_locations}.compact).flatten
87
+ end
62
88
  end
63
89
 
64
90
  def all
65
- cache.fetch(CACHE_KEY) do
66
- Location.reset # make sure you reset the memo cache on a cache miss
67
- transform_data(request('/locations').try(:compact) || [])
91
+ bench do
92
+ cache.fetch("#{CACHE_PRE}_request") do
93
+ transform_data(request('/locations').try(:compact) || [])
94
+ end
68
95
  end
69
96
  end
70
97
 
@@ -76,8 +103,12 @@ class Location < Struct.new(:id, :sas_id, :name, :code, :kind, :lat, :lng, :time
76
103
  end
77
104
 
78
105
  def reset
79
- Rails.cache.delete(MEMOS_KEY)
80
- Rails.cache.delete(CACHE_KEY)
106
+ flattened.each do |l|
107
+ Rails.cache.delete("#{CACHE_PRE}-id-#{l.id}")
108
+ Rails.cache.delete("#{CACHE_PRE}-code-#{l.code}")
109
+ end
110
+ Rails.cache.delete("#{CACHE_PRE}_memos")
111
+ Rails.cache.delete("#{CACHE_PRE}_request")
81
112
  end
82
113
 
83
114
  def transform_data(data)
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module LocationServiceClient
2
- VERSION = "0.1.3".freeze
2
+ VERSION = "0.1.4".freeze
3
3
 
4
4
  def self.version
5
5
  VERSION
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: location-service-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Buermann