Class: Trahald::RedisClient

Inherits:
BackendBase show all
Defined in:
lib/trahald/redis-client.rb

Constant Summary collapse

KEY_SET =

track all page names.

".keys"
MODIFIED_DATE =

for using cache.

".modified"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ RedisClient

TODO: same



14
15
16
17
# File 'lib/trahald/redis-client.rb', line 14

def initialize(url)
  @redis = Redis.new(:url => url)
  @params = Hash.new
end

Class Method Details

.init_repo_if_needed(dir) ⇒ Object



67
68
69
# File 'lib/trahald/redis-client.rb', line 67

def self.init_repo_if_needed(dir)
  # do nothing.
end

Instance Method Details

#add!(name, body) ⇒ Object

This method does not set data to Redis DB. To confirm, use commit! after add!.



25
26
27
# File 'lib/trahald/redis-client.rb', line 25

def add!(name, body)
  @params[name] = body
end

#article(name) ⇒ Object



19
20
21
22
# File 'lib/trahald/redis-client.rb', line 19

def article(name)
  json = @redis.zrange(name, -1, -1).first # nil unless zrange(..).any?
  if json; Article.from_json(json) else nil end
end

#body(name) ⇒ Object



29
30
31
32
# File 'lib/trahald/redis-client.rb', line 29

def body(name)
  a = article name
  if a; a.body else nil end
end

#commit!(message) ⇒ Object

message is not used.



35
36
37
38
39
40
41
42
43
44
# File 'lib/trahald/redis-client.rb', line 35

def commit!(message)
  date = Time.now
  @params.each{|name, body|
    json = Article.new(name, body, date).to_json
    zcard = @redis.zcard name
    @redis.zadd name, zcard+1, json
    @redis.sadd KEY_SET, name
  }
  @redis.set MODIFIED_DATE, date.to_s
end

#dataObject



51
52
53
54
55
56
# File 'lib/trahald/redis-client.rb', line 51

def data
  @redis.smembers(KEY_SET).map do |name|
    a = article name
    MarkdownBody.new(name, a.body, a.date).summary
  end
end

#flush!Object

CAUTION! This method flush data on current db.



47
48
49
# File 'lib/trahald/redis-client.rb', line 47

def flush!
  @redis.flushdb
end

#last_modifiedObject



58
59
60
61
# File 'lib/trahald/redis-client.rb', line 58

def last_modified
  date = @redis.get(MODIFIED_DATE)
  if date; Time.parse date else Time.now end
end

#listObject



63
64
65
# File 'lib/trahald/redis-client.rb', line 63

def list
  @redis.smembers(KEY_SET).sort
end