class Fluent::Plugin::RedisStorage

Constants

DEFAULT_TTL_VALUE

Attributes

store[R]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/storage_redis.rb, line 22
def initialize
  super

  @store = {}
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# 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)
    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) click to toggle source
# File lib/fluent/plugin/storage_redis.rb, line 107
def delete(key)
  @store.delete(key.to_s)
end
fetch(key, defval) click to toggle source
# File lib/fluent/plugin/storage_redis.rb, line 99
def fetch(key, defval)
  @store.fetch(key.to_s, defval)
end
get(key) click to toggle source
# File lib/fluent/plugin/storage_redis.rb, line 95
def get(key)
  @store[key.to_s]
end
load() click to toggle source
# File lib/fluent/plugin/storage_redis.rb, line 65
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?() click to toggle source
# File lib/fluent/plugin/storage_redis.rb, line 61
def multi_workers_ready?
  true
end
put(key, value) click to toggle source
# File lib/fluent/plugin/storage_redis.rb, line 103
def put(key, value)
  @store[key.to_s] = value
end
save() click to toggle source
# File lib/fluent/plugin/storage_redis.rb, line 81
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) click to toggle source
# File lib/fluent/plugin/storage_redis.rb, line 111
def update(key, &block)
  @store[key.to_s] = block.call(@store[key.to_s])
end