Class: Picky::Backends::Redis::List

Inherits:
Basic show all
Defined in:
lib/picky/backends/redis/list.rb

Instance Attribute Summary

Attributes inherited from Basic

#client, #namespace

Instance Method Summary collapse

Methods inherited from Basic

#empty, #initial, #initialize, #load, #reset, #retrieve, #to_s

Constructor Details

This class inherits a constructor from Picky::Backends::Redis::Basic

Instance Method Details

#[](key) ⇒ Object

Get a collection.

Internal API method for the index.



56
57
58
59
60
61
62
# File 'lib/picky/backends/redis/list.rb', line 56

def [] key
  list = client.zrange "#{namespace}:#{key}", :'0', :'-1'
  
  DirectlyManipulable.make self, list, key
  
  list
end

#[]=(key, values) ⇒ Object

Set a single list.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/picky/backends/redis/list.rb', line 66

def []= key, values
  delete key

  redis_key = "#{namespace}:#{key}"
  i = 0
  values.each do |value|
    i += 1
    client.zadd redis_key, i, value
  end
  
  DirectlyManipulable.make self, values, key
  
  values
end

#clearObject

Clear the index for this list.

Note: Perhaps we can use a server only command.

This is not the optimal way to do it.


14
15
16
17
18
19
# File 'lib/picky/backends/redis/list.rb', line 14

def clear
  redis_key = "#{namespace}:*"
  client.keys(redis_key).each do |key|
    client.del key
  end
end

#delete(key) ⇒ Object

Deletes the list for the key.



32
33
34
# File 'lib/picky/backends/redis/list.rb', line 32

def delete key
  client.del "#{namespace}:#{key}"
end

#dump(hash) ⇒ Object

Writes the hash into Redis.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/picky/backends/redis/list.rb', line 38

def dump hash
  unless @realtime
    clear
    hash.each_pair do |key, values|
      redis_key = "#{namespace}:#{key}"
      i = 0
      values.each do |value|
        i += 1
        client.zadd redis_key, i, value
      end
    end
  end
end

#inject(initial, &block) ⇒ Object

Inject.



83
84
85
86
87
88
89
90
# File 'lib/picky/backends/redis/list.rb', line 83

def inject initial, &block
  redis_keys = "#{namespace}:*"
  client.keys(redis_keys).each do |redis_key|
    key = redis_key[/:([^\:]+)$/, 1]
    initial = block.call initial, [key, self[key]]
  end
  initial
end

#sizeObject

Size of the list(s).



23
24
25
26
27
28
# File 'lib/picky/backends/redis/list.rb', line 23

def size
  redis_key = "#{namespace}:*"
  client.keys(redis_key).inject(0) do |total, key|
    total + client.zcard(key)
  end
end