Class: PuavoRest::IdPool

Inherits:
Object
  • Object
show all
Defined in:
resources/id_pool.rb

Overview

Get unique and sequential IDs from Redis

Class Method Summary (collapse)

Class Method Details

+ (Array<Fixnum>) id_range(namespace, count)

Get range of Ids

Parameters:

  • namespace (String)

    ID namespace. For example puavoNextUidNumber or puavoNextGidNumber

  • count (Fixnum)

    How many IDs should be returned

Returns:

  • (Array<Fixnum>)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'resources/id_pool.rb', line 11

def self.id_range(namespace, count)
  pool = get_redis_id_pool()

  if pool.get(namespace).nil?
    pool.set(namespace, 5000)
  end

  _id_range = (1..count).map do
    pool.incr(namespace)
  end

  return _id_range
end

+ (Object) last_id(namespace)

See what was the last given ID for the namespace

Parameters:

  • namespace (String)

Returns:

  • Fixnum



29
30
31
# File 'resources/id_pool.rb', line 29

def self.last_id(namespace)
  get_redis_id_pool.get(namespace)
end

+ (Object) next_id(namespace)

Get single id for ID the namespace

Parameters:

  • namespace (String)

Returns:

  • Fixnum



37
38
39
# File 'resources/id_pool.rb', line 37

def self.next_id(namespace)
  id_range(namespace, 1).first
end

+ (Object) set_id!(namespace, value)

Set ID sequence to value. Using this can break things badly. Use with care!

Parameters:

  • namespace (String)
  • value (Fixnum)


45
46
47
48
# File 'resources/id_pool.rb', line 45

def self.set_id!(namespace, value)
  pool = get_redis_id_pool()
  pool.set(namespace, value)
end