Skip to content

Commit

Permalink
Merge pull request #50 from palkan/fix_cache_config
Browse files Browse the repository at this point in the history
Fix issue #49 Fix configuration cache options
  • Loading branch information
AlexanderShvaykin authored Oct 27, 2020
2 parents 5d3e976 + 757107d commit 4698e50
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## master (unreleased)

- Fixes [#49](https://github.com/palkan/influxer/issues/49) Cache hash configuration cannot be applied.([@AlexanderShvaykin][])
- Fixes [#47](https://github.com/palkan/influxer/issues/47) Can't delete data when retention policy is set for a metric. ([@MPursche][])

## 1.2.1 (2020-07-09)

- Support for setting timezone in queries to configure influx time calculations, e.g., time epoch aggregation ([@jklimke][])
Expand Down Expand Up @@ -90,3 +93,4 @@ See [changelog](https://github.com/palkan/influxer/blob/1.0.0/Changelog.md) for
[@MPursche]: https://github.com/MPursche
[@jklimke]: https://github.com/jklimke
[@dimiii]: https://github.com/dimiii
[@AlexanderShvaykin]: https://github.com/AlexanderShvaykin
19 changes: 16 additions & 3 deletions lib/influxer/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,28 @@ class Config < Anyway::Config
:denormalize,
:udp,
:async,
:cache_enabled,
:cache,
database: "db",
time_precision: "ns",
cache: false,
time_duration_suffix_enabled: false

def load(*)
super
# we want pass @cache value as options to cache store, so we want it to be a Hash
@cache = {}.with_indifferent_access if @cache == true
if cache_enabled.nil?
self.cache_enabled = cache_enabled_value
end
end

def cache=(value)
super
self.cache_enabled = cache_enabled_value
end

private

def cache_enabled_value
!!cache
end
end
end
8 changes: 4 additions & 4 deletions lib/influxer/rails/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ module Influxer
class Client
def query(sql, options = {})
log_sql(sql) do
if !options.fetch(:cache, true) || Influxer.config.cache == false
super(sql, options)
if !(options.fetch(:cache, true) && Influxer.config.cache_enabled)
super(sql, **options)
else
Rails.cache.fetch(normalized_cache_key(sql), cache_options(sql)) { super(sql, options) }
Rails.cache.fetch(normalized_cache_key(sql), **cache_options(sql)) { super(sql, **options) }
end
end
end
Expand All @@ -34,7 +34,7 @@ def log_sql(sql)
def cache_options(sql = nil)
options = Influxer.config.cache.dup
options[:expires_in] = (options[:cache_now_for] || 60) if /\snow\(\)/.match?(sql)
options
options.symbolize_keys
end

# add prefix; remove whitespaces
Expand Down
15 changes: 15 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,20 @@
Timecop.travel(2.minutes.from_now)
expect(Rails.cache.exist?("influxer:listseries")).to be_falsey
end

it "does not write data to cache if disabled" do
conf.cache = {}
conf.cache_enabled = false

subject.query(q)
expect(Rails.cache.exist?("influxer:listseries")).to be_falsey
end

it "does not write data to cache without cache config" do
conf.cache = nil

subject.query(q)
expect(Rails.cache.exist?("influxer:listseries")).to be_falsey
end
end
end

0 comments on commit 4698e50

Please # to comment.