Class: Ultracache::Storage::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/ultracache/storage/redis.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis.



8
9
10
# File 'lib/ultracache/storage/redis.rb', line 8

def initialize(options = {})
  @urls = options[:urls] || ['redis://localhost:6379/1']
end

Instance Method Details

#connectionObject



12
13
14
# File 'lib/ultracache/storage/redis.rb', line 12

def connection
  @connection ||= ::Redis::Distributed.new(@urls)
end

#del(id) ⇒ Object



28
29
30
# File 'lib/ultracache/storage/redis.rb', line 28

def del(id)
  connection.del(id)
end

#get(id) ⇒ Object



20
21
22
# File 'lib/ultracache/storage/redis.rb', line 20

def get(id)
  connection.get(id)
end

#get_queue(id, opts = {}) ⇒ Object



32
33
34
# File 'lib/ultracache/storage/redis.rb', line 32

def get_queue(id, opts={})
  connection.zrevrangebyscore(id, opts[:to] || "+inf", opts[:from] || "-inf")
end

#get_queue_by_rank(id, opts = {}) ⇒ Object



52
53
54
# File 'lib/ultracache/storage/redis.rb', line 52

def get_queue_by_rank(id, opts={})
  connection.zrevrange(id, opts[:from] || 0, opts[:to] || -1)
end

#get_queue_paged(id, opts = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/ultracache/storage/redis.rb', line 36

def get_queue_paged(id, opts={})
  per_page = opts[:per_page] || 20
  page = opts[:page] || 1
  offset = per_page.to_i * (page.to_i - 1)

  connection.zrevrangebyscore(id, opts[:to] || "+inf", opts[:from] || "-inf", :limit => [offset, per_page])
end

#multi_get(*ids) ⇒ Object



24
25
26
# File 'lib/ultracache/storage/redis.rb', line 24

def multi_get(*ids)
  connection.mget(ids)
end

#put_queue(id, key, entry) ⇒ Object



56
57
58
# File 'lib/ultracache/storage/redis.rb', line 56

def put_queue(id, key, entry)
  connection.zadd(id, key, entry)
end

#remove_from_queue(id, val) ⇒ Object



48
49
50
# File 'lib/ultracache/storage/redis.rb', line 48

def remove_from_queue(id, val)
  connection.zrem(id, val)
end

#remove_from_queue_by_range(id, opts = {}) ⇒ Object



44
45
46
# File 'lib/ultracache/storage/redis.rb', line 44

def remove_from_queue_by_range(id, opts={})
  connection.zremrangebyscore(id, opts[:from], opts[:to])
end

#set(id, doc) ⇒ Object



16
17
18
# File 'lib/ultracache/storage/redis.rb', line 16

def set(id, doc)
  connection.set(id, doc)
end