Class: AtomicCache::Keyspace

Inherits:
Object
  • Object
show all
Defined in:
lib/atomic_cache/key/keyspace.rb

Constant Summary collapse

DEFAULT_SEPARATOR =
':'
LOCK_VALUE =
1
PRIMITIVE_CLASSES =
[Numeric, String, Symbol]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace:, root: nil, separator: nil, timestamp_formatter: nil) ⇒ Keyspace

Returns a new instance of Keyspace.

Parameters:

  • namespace (Object, Array<Object>)

    segment(s) with which to prefix the keyspace

  • root (Object) (defaults to: nil)

    logical ‘root’ or primary identifier for this keyspace

  • separator (String) (defaults to: nil)

    character or string to separate keyspace segments

  • timestamp_formatter (Proc) (defaults to: nil)

    function to turn Time -> String



22
23
24
25
26
27
28
# File 'lib/atomic_cache/key/keyspace.rb', line 22

def initialize(namespace:, root: nil, separator: nil, timestamp_formatter: nil)
  @timestamp_formatter = timestamp_formatter || DefaultConfig.instance.timestamp_formatter
  @separator = separator || DefaultConfig.instance.separator
  @namespace = []
  @namespace = normalize_segments(namespace) if namespace.present?
  @root = root || @namespace.last
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



16
17
18
# File 'lib/atomic_cache/key/keyspace.rb', line 16

def namespace
  @namespace
end

#rootObject (readonly)

Returns the value of attribute root.



16
17
18
# File 'lib/atomic_cache/key/keyspace.rb', line 16

def root
  @root
end

Instance Method Details

#child(namespace) ⇒ AtomicCache::Keyspace

Create a new Keyspace, extending the namespace with the given segments and retaining this keyspace’s root and separator

Parameters:

  • namespace (Array<Object>)

    segments to concat onto this keyspace

Returns:



35
36
37
38
39
# File 'lib/atomic_cache/key/keyspace.rb', line 35

def child(namespace)
  throw ArgumentError.new("Prefix must be an Array but was #{namespace.class}") unless namespace.is_a?(Array)
  joined_namespacees = @namespace.clone.concat(namespace)
  self.class.new(namespace: joined_namespacees)
end

#key(suffix = nil) ⇒ String

Get a string key for this keyspace, optionally suffixed by the given value

Parameters:

  • suffix (String, Symbol, Numeric) (defaults to: nil)

    an optional suffix

Returns:

  • (String)

    a key



45
46
47
# File 'lib/atomic_cache/key/keyspace.rb', line 45

def key(suffix=nil)
  flattened_key(suffix)
end

#last_known_key_keyObject

the key the last_known_key is stored at, thus key key.



58
59
60
# File 'lib/atomic_cache/key/keyspace.rb', line 58

def last_known_key_key
  @last_known_key ||= flattened_key('lkk')
end

#last_mod_time_keyObject



49
50
51
# File 'lib/atomic_cache/key/keyspace.rb', line 49

def last_mod_time_key
  @last_mod_time_key ||= flattened_key('lmt')
end

#lock_keyObject



53
54
55
# File 'lib/atomic_cache/key/keyspace.rb', line 53

def lock_key
  @lock_key ||= flattened_key('lock')
end