Class: KMDB::GlobalUID

Inherits:
Object
  • Object
show all
Defined in:
lib/kmdb/models/global_uid.rb

Overview

Efficiently generate cross-process globally unique IDs pernamespace, using Redis. IDs start at 1 and increment monotonically; each client is handed Ids in batches of 100.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ns) ⇒ GlobalUID

Returns a new instance of GlobalUID.



17
18
19
20
21
# File 'lib/kmdb/models/global_uid.rb', line 17

def initialize(ns)
  @ns = ns
  @major = nil
  @minor = nil
end

Class Method Details

.get(ns = 'value') ⇒ Object



11
12
13
14
15
# File 'lib/kmdb/models/global_uid.rb', line 11

def self.get(ns = 'value')
  @instances ||= {}
  @instances[ns] ||= new(ns)
  @instances[ns].get
end

Instance Method Details

#getObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/kmdb/models/global_uid.rb', line 23

def get
  if @major.nil? || @minor > BATCH_SIZE
    @major = _redis.incr(@ns) % (1 << 48)
    @minor = 1
  end

  uid = (@major-1) * BATCH_SIZE + @minor
  @minor += 1
  return uid
end