Skip to content

Commit 4f6f3c7

Browse files
committed
Add ignore_nil config for saving memory
1 parent 25c3346 commit 4f6f3c7

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

lib/splitclient-rb/cache/adapters/cache_adapter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class CacheAdapter
88
def_delegators :@adapter, :initialize_set, :set_bool, :pipelined
99

1010
def initialize(config)
11-
@cache = LruRedux::TTL::ThreadSafeCache.new(config.max_cache_size, config.cache_ttl)
11+
@cache = LruRedux::TTL::ThreadSafeCache.new(config.max_cache_size, config.cache_ttl, config.ignore_nil)
1212
@adapter = config.cache_adapter
1313
end
1414

lib/splitclient-rb/cache/observers/impression_observer.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ class ImpressionObserver
44
LAST_SEEN_CACHE_SIZE = 500000
55

66
def initialize
7-
@cache = LruRedux::TTL::ThreadSafeCache.new(LAST_SEEN_CACHE_SIZE)
7+
@cache = LruRedux::TTL::ThreadSafeCache.new(LAST_SEEN_CACHE_SIZE, true)
88
@impression_hasher = Hashers::ImpressionHasher.new
99
end
1010

1111
def test_and_set(impression)
1212
return if impression.nil?
13-
13+
1414
hash = @impression_hasher.process(impression)
1515
previous = @cache[hash]
1616
@cache[hash] = impression[:m]

lib/splitclient-rb/split_config.rb

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class SplitConfig
2323
# @option opts [Int] :impressions_queue_size Size of the impressions queue in the memory repository. Once reached, newer impressions will be dropped
2424
# @option opts [Int] :impressions_bulk_size Max number of impressions to be sent to the backend on each post
2525
# @option opts [#log] :impression_listener this object will capture all impressions and process them through `#log`
26+
# @option opts [Boolean] :ignore_nil (true) The value to enable or disable caching nil values.
2627
# @option opts [Int] :cache_ttl Time to live in seconds for the memory cache values when using Redis.
2728
# @option opts [Int] :max_cache_size Max number of items to be held in the memory cache before prunning when using Redis.
2829
# @return [type] SplitConfig with configuration options
@@ -70,6 +71,7 @@ def initialize(opts = {})
7071
@machine_name = SplitConfig.machine_hostname(@ip_addresses_enabled, opts[:machine_name], opts[:cache_adapter] || SplitConfig.default_cache_adapter)
7172
@machine_ip = SplitConfig.machine_ip(@ip_addresses_enabled, opts[:machine_ip], opts[:cache_adapter] || SplitConfig.default_cache_adapter)
7273

74+
@ignore_nil = opts[:ignore_nil] || SplitConfig.ignore_nil
7375
@cache_ttl = opts[:cache_ttl] || SplitConfig.cache_ttl
7476
@max_cache_size = opts[:max_cache_size] || SplitConfig.max_cache_size
7577

@@ -221,6 +223,7 @@ def initialize(opts = {})
221223
attr_accessor :machine_ip
222224
attr_accessor :machine_name
223225

226+
attr_accessor :ignore_nil
224227
attr_accessor :cache_ttl
225228
attr_accessor :max_cache_size
226229

@@ -591,6 +594,15 @@ def self.transport_debug
591594
false
592595
end
593596

597+
#
598+
# The default ignore_nil value
599+
#
600+
#
601+
# @return [boolean]
602+
def self.ignore_nil
603+
true
604+
end
605+
594606
#
595607
# The default cache time to live
596608
#
@@ -599,13 +611,15 @@ def self.cache_ttl
599611
5
600612
end
601613

614+
#
602615
# The default max cache size
603616
#
604617
# @return [int]
605618
def self.max_cache_size
606619
500
607620
end
608621

622+
#
609623
# The default max key size
610624
#
611625
# @return [int]

0 commit comments

Comments
 (0)