Class: Anemone::Storage::Redis

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

Constant Summary

MARSHAL_FIELDS =
%w(links visited fetched)

Instance Method Summary (collapse)

Constructor Details

- (Redis) initialize(opts = {})

Returns a new instance of Redis



9
10
11
12
13
# File 'lib/anemone/storage/redis.rb', line 9

def initialize(opts = {})
  @redis = ::Redis.new(opts)
  @key_prefix = opts[:key_prefix] || 'anemone'
  keys.each { |key| delete(key) }
end

Instance Method Details

- (Object) [](key)



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

def [](key)
  rkey = "#{@key_prefix}:pages:#{key.to_s}"
  rget(rkey)
end

- (Object) []=(key, value)



20
21
22
23
24
25
26
27
28
29
# File 'lib/anemone/storage/redis.rb', line 20

def []=(key, value)
  rkey = "#{@key_prefix}:pages:#{key.to_s}"
  hash = value.to_hash
  MARSHAL_FIELDS.each do |field|
    hash[field] = Marshal.dump(hash[field])
  end
  hash.each do |field, value|
    @redis.hset(rkey, field, value)
  end
end

- (Object) close



66
67
68
# File 'lib/anemone/storage/redis.rb', line 66

def close
  @redis.quit
end

- (Object) delete(key)



31
32
33
34
35
36
# File 'lib/anemone/storage/redis.rb', line 31

def delete(key)
  rkey = "#{@key_prefix}:pages:#{key.to_s}"
  page = self[key]
  @redis.del(rkey)
  page
end

- (Object) each



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

def each
  rkeys = @redis.keys("#{@key_prefix}:pages:*")
  rkeys.each do |rkey|
    page = rget(rkey)
    yield page.url.to_s, page
  end
end

- (Boolean) has_key?(key)

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/anemone/storage/redis.rb', line 61

def has_key?(key)
  rkey = "#{@key_prefix}:pages:#{key.to_s}"
  @redis.exists(rkey)
end

- (Object) keys



55
56
57
58
59
# File 'lib/anemone/storage/redis.rb', line 55

def keys
  keys = []
  self.each { |k, v| keys << k.to_s }
  keys
end

- (Object) merge!(hash)



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

def merge!(hash)
  hash.each { |key, value| self[key] = value }
  self
end

- (Object) size



51
52
53
# File 'lib/anemone/storage/redis.rb', line 51

def size
  @redis.keys("#{@key_prefix}:pages:*").size
end