Class: Ohm::Set

Inherits:
Object
  • Object
show all
Defined in:
lib/ohm/set.rb

Constant Summary collapse

LUA_POP =
File.expand_path("../lua/pop.lua", __FILE__)

Instance Method Summary collapse

Instance Method Details

#pop(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ohm/set.rb', line 5

def pop(options = {})
  if options[:by]
    options.merge!(:by => to_key(options.delete(:by)))
  end

  if options.has_key?(:limit)
    raise ArgumentError, 'limit options is not supported. always limit: [0, 1]'
  end

  if options.has_key?(:get)
    raise ArgumentError, 'get options is not supported.'
  end

  if options.has_key?(:store)
    raise ArgumentError, 'store options is not supported.'
  end

  options.merge!(limit: [0, 1])

  ids = []
  ops = []

  expr = ["SORT", key, *Utils.sort_options(options)]
  ops.push(Stal.compile(expr, ids, ops))

  response = script(
    LUA_POP,
    0,
    ops.to_msgpack,
    ids.to_msgpack,
    @model.name.to_msgpack,
    @model.uniques.to_msgpack,
    @model.tracked.to_msgpack
  )

  return nil unless response

  @model.new(Utils.dict(response))
end

#script(file, *args) ⇒ Object

NOTE This method is the same as Ohm::Model#script



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ohm/set.rb', line 46

def script(file, *args)
  cache = LUA_CACHE[redis.url]

  if cache.key?(file)
    sha = cache[file]
  else
    src = File.read(file)
    sha = redis.call("SCRIPT", "LOAD", src)

    cache[file] = sha
  end

  redis.call("EVALSHA", sha, *args)
end