Class: Ultracache::Storage::Redis

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis.



10
11
12
# File 'lib/ultracache/storage/redis.rb', line 10

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

Instance Method Details

#connectionObject



14
15
16
# File 'lib/ultracache/storage/redis.rb', line 14

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

#del(id) ⇒ Object



30
31
32
# File 'lib/ultracache/storage/redis.rb', line 30

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

#get(id) ⇒ Object



22
23
24
# File 'lib/ultracache/storage/redis.rb', line 22

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

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



34
35
36
# File 'lib/ultracache/storage/redis.rb', line 34

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

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



54
55
56
# File 'lib/ultracache/storage/redis.rb', line 54

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

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



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

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



26
27
28
# File 'lib/ultracache/storage/redis.rb', line 26

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

#put_queue(id, key, entry) ⇒ Object



58
59
60
# File 'lib/ultracache/storage/redis.rb', line 58

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

#remove_from_queue(id, val) ⇒ Object



50
51
52
# File 'lib/ultracache/storage/redis.rb', line 50

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

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



46
47
48
# File 'lib/ultracache/storage/redis.rb', line 46

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

#set(id, doc) ⇒ Object



18
19
20
# File 'lib/ultracache/storage/redis.rb', line 18

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