Class: LaunchDarkly::Impl::ExpiringCache Private

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/expiring_cache.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A thread-safe cache with maximum number of entries and TTL. Adapted from https://github.com/SamSaffron/lru_redux/blob/master/lib/lru_redux/ttl/cache.rb under MIT license with the following changes:

* made thread-safe
* removed many unused methods
* reading a key does not reset its expiration time, only writing
* expiration is measured on a monotonic clock, so wall-clock steps cannot
retain entries past their TTL or evict them early

Since:

  • 5.5.0

Instance Method Summary collapse

Constructor Details

#initialize(max_size, ttl, clock: MONOTONIC_CLOCK) ⇒ ExpiringCache

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ExpiringCache.

Parameters:

  • clock (#call) (defaults to: MONOTONIC_CLOCK) —

    returns seconds on a monotonic clock; injectable for tests

Since:

  • 5.5.0



18
19
20
21
22
23
24
25
# File 'lib/ldclient-rb/impl/expiring_cache.rb', line 18

def initialize(max_size, ttl, clock: MONOTONIC_CLOCK)
  @max_size = max_size
  @ttl = ttl
  @clock = clock
  @data_lru = {}
  @data_ttl = {}
  @lock = Mutex.new
end

Instance Method Details

#[](key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



27
28
29
30
31
32
# File 'lib/ldclient-rb/impl/expiring_cache.rb', line 27

def [](key)
  @lock.synchronize do
    ttl_evict
    @data_lru[key]
  end
end

#[]=(key, val) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ldclient-rb/impl/expiring_cache.rb', line 34

def []=(key, val)
  @lock.synchronize do
    ttl_evict

    @data_lru.delete(key)
    @data_ttl.delete(key)

    @data_lru[key] = val
    @data_ttl[key] = @clock.call

    if @data_lru.size > @max_size
      key, _ = @data_lru.first # hashes have a FIFO ordering in Ruby

      @data_ttl.delete(key)
      @data_lru.delete(key)
    end

    val
  end
end

#clear ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



64
65
66
67
68
69
# File 'lib/ldclient-rb/impl/expiring_cache.rb', line 64

def clear
  @lock.synchronize do
    @data_lru.clear
    @data_ttl.clear
  end
end

#delete(key) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 5.5.0



55
56
57
58
59
60
61
62
# File 'lib/ldclient-rb/impl/expiring_cache.rb', line 55

def delete(key)
  @lock.synchronize do
    ttl_evict

    @data_lru.delete(key)
    @data_ttl.delete(key)
  end
end