Class: Mudis::Bound

Inherits:
Object
  • Object
show all
Defined in:
lib/mudis/bound.rb

Overview

Scoped wrapper for caller-bound access with optional per-caller policy.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace:, default_ttl: nil, max_ttl: nil, max_value_bytes: nil) ⇒ Bound

Returns a new instance of Bound.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
# File 'lib/mudis/bound.rb', line 8

def initialize(namespace:, default_ttl: nil, max_ttl: nil, max_value_bytes: nil)
  raise ArgumentError, "namespace is required" if namespace.nil? || namespace.to_s.empty?

  @namespace = namespace
  @default_ttl = default_ttl
  @max_ttl = max_ttl
  @max_value_bytes = max_value_bytes
  @inflight_mutexes_lock = Mutex.new
  @inflight_mutexes = {}
end

Instance Attribute Details

#namespace ⇒ Object (readonly)

Returns the value of attribute namespace.



19
20
21
# File 'lib/mudis/bound.rb', line 19

def namespace
  @namespace
end

Instance Method Details

#clear(key) ⇒ Object



54
55
56
# File 'lib/mudis/bound.rb', line 54

def clear(key)
  delete(key)
end

#clear_namespace ⇒ Object



76
77
78
# File 'lib/mudis/bound.rb', line 76

def clear_namespace
  Mudis.clear_namespace(namespace: @namespace)
end

#delete(key) ⇒ Object



38
39
40
# File 'lib/mudis/bound.rb', line 38

def delete(key)
  Mudis.delete(key, namespace: @namespace)
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/mudis/bound.rb', line 42

def exists?(key)
  Mudis.exists?(key, namespace: @namespace)
end

#fetch(key, expires_in: nil, force: false, singleflight: false) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/mudis/bound.rb', line 46

def fetch(key, expires_in: nil, force: false, singleflight: false)
  return fetch_without_lock(key, expires_in:, force:) { yield } unless singleflight

  with_inflight_lock(key) do
    fetch_without_lock(key, expires_in:, force:) { yield }
  end
end

#inspect(key) ⇒ Object



64
65
66
# File 'lib/mudis/bound.rb', line 64

def inspect(key)
  Mudis.inspect(key, namespace: @namespace)
end

#keys ⇒ Object



68
69
70
# File 'lib/mudis/bound.rb', line 68

def keys
  Mudis.keys(namespace: @namespace)
end

#metrics ⇒ Object



72
73
74
# File 'lib/mudis/bound.rb', line 72

def metrics
  Mudis.metrics(namespace: @namespace)
end

#read(key) ⇒ Object



21
22
23
# File 'lib/mudis/bound.rb', line 21

def read(key)
  Mudis.read(key, namespace: @namespace)
end

#replace(key, value, expires_in: nil) ⇒ Object



58
59
60
61
62
# File 'lib/mudis/bound.rb', line 58

def replace(key, value, expires_in: nil)
  return if exceeds_max_value_bytes?(value)

  Mudis.replace(key, value, expires_in: effective_ttl(expires_in), namespace: @namespace)
end

#update(key) ⇒ Object



31
32
33
34
35
36
# File 'lib/mudis/bound.rb', line 31

def update(key)
  Mudis.update(key, namespace: @namespace) do |current|
    next_value = yield(current)
    exceeds_max_value_bytes?(next_value) ? current : next_value
  end
end

#write(key, value, expires_in: nil) ⇒ Object



25
26
27
28
29
# File 'lib/mudis/bound.rb', line 25

def write(key, value, expires_in: nil)
  return if exceeds_max_value_bytes?(value)

  Mudis.write(key, value, expires_in: effective_ttl(expires_in), namespace: @namespace)
end