Module: Mudis::Namespace
- Included in:
- Mudis
- Defined in:
- lib/mudis/namespace.rb
Overview
Namespace module handles logical key separation and scoping
Instance Method Summary collapse
-
#clear_namespace(namespace:) ⇒ Object
Clears all keys in a specific namespace.
-
#keys(namespace:) ⇒ Object
Returns all keys in a specific namespace.
-
#with_namespace(namespace) ⇒ Object
Executes a block with a specific namespace, restoring the old namespace afterwards.
Instance Method Details
#clear_namespace(namespace:) ⇒ Object
Clears all keys in a specific namespace
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mudis/namespace.rb', line 15 def clear_namespace(namespace:) raise ArgumentError, "namespace is required" unless namespace prefix = "#{namespace}:" buckets.times do |idx| mutex = @mutexes[idx] store = @stores[idx] mutex.synchronize do keys_to_delete = store.keys.select { |key| key.start_with?(prefix) } keys_to_delete.each { |key| evict_key(idx, key) } end end end |
#keys(namespace:) ⇒ Object
Returns all keys in a specific namespace
7 8 9 10 11 12 |
# File 'lib/mudis/namespace.rb', line 7 def keys(namespace:) raise ArgumentError, "namespace is required" unless namespace prefix = "#{namespace}:" all_keys.select { |key| key.start_with?(prefix) }.map { |key| key.delete_prefix(prefix) } end |
#with_namespace(namespace) ⇒ Object
Executes a block with a specific namespace, restoring the old namespace afterwards
31 32 33 34 35 36 37 |
# File 'lib/mudis/namespace.rb', line 31 def with_namespace(namespace) old_ns = Thread.current[:mudis_namespace] Thread.current[:mudis_namespace] = namespace yield ensure Thread.current[:mudis_namespace] = old_ns end |