Class: Fluent::Plugin::RedisStorage

Inherits:
Storage
  • Object
show all
Defined in:
lib/fluent/plugin/storage_redis.rb

Constant Summary collapse

DEFAULT_TTL_VALUE =
-1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRedisStorage

Returns a new instance of RedisStorage.



22
23
24
25
26
# File 'lib/fluent/plugin/storage_redis.rb', line 22

def initialize
  super

  @store = {}
end

Instance Attribute Details

#storeObject (readonly)

for test



20
21
22
# File 'lib/fluent/plugin/storage_redis.rb', line 20

def store
  @store
end

Instance Method Details

#configure(conf) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/fluent/plugin/storage_redis.rb', line 28

def configure(conf)
  super

  unless @path
    if conf && !conf.arg.empty?
      @path = conf.arg
    else
      raise Fluent::ConfigError, "path or conf.arg for <storage> is required."
    end
  end

  options = {
    host: @host,
    port: @port,
    thread_safe: true,
    db: @db_number
  }
  options[:password] = @password if @password

  @redis = Redis.new(options)

  object = @redis.get(@path)
  if object
    begin
      data = Yajl::Parser.parse(object)
      raise Fluent::ConfigError, "Invalid contents (not object) in plugin redis storage: '#{@path}'" unless data.is_a?(Hash) unless data.is_a?(Hash)
    rescue => e
      log.error "failed to read data from plugin redis storage", path: @path, error: e
      raise Fluent::ConfigError, "Unexpected error: failed to read data from plugin redis storage: '#{@path}'"
    end
  end
end

#delete(key) ⇒ Object



111
112
113
# File 'lib/fluent/plugin/storage_redis.rb', line 111

def delete(key)
  @store.delete(key.to_s)
end

#fetch(key, defval) ⇒ Object



103
104
105
# File 'lib/fluent/plugin/storage_redis.rb', line 103

def fetch(key, defval)
  @store.fetch(key.to_s, defval)
end

#get(key) ⇒ Object



99
100
101
# File 'lib/fluent/plugin/storage_redis.rb', line 99

def get(key)
  @store[key.to_s]
end

#loadObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fluent/plugin/storage_redis.rb', line 69

def load
  begin
    json_string = @redis.get(@path)
    json_string ||= "{}" # for ttl support.
    json = Yajl::Parser.parse(json_string)
    unless json.is_a?(Hash)
      log.error "broken content for plugin storage (Hash required: ignored)", type: json.class
      log.debug "broken content", content: json_string
      return
    end
    @store = json
  rescue => e
    log.error "failed to load data for plugin storage from redis", path: @path, error: e
  end
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/fluent/plugin/storage_redis.rb', line 61

def multi_workers_ready?
  true
end

#persistent_always?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/fluent/plugin/storage_redis.rb', line 65

def persistent_always?
  true
end

#put(key, value) ⇒ Object



107
108
109
# File 'lib/fluent/plugin/storage_redis.rb', line 107

def put(key, value)
  @store[key.to_s] = value
end

#saveObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fluent/plugin/storage_redis.rb', line 85

def save
  begin
    json_string = Yajl::Encoder.encode(@store)
    @redis.pipelined {
      @redis.multi do
        @redis.set(@path, json_string)
        @redis.expire(@path, @ttl) if @ttl > 0
      end
    }
  rescue => e
    log.error "failed to save data for plugin storage to redis", path: @path, error: e
  end
end

#update(key, &block) ⇒ Object



115
116
117
# File 'lib/fluent/plugin/storage_redis.rb', line 115

def update(key, &block)
  @store[key.to_s] = block.call(@store[key.to_s])
end