class Fluent::Plugin::MemcachedStorage

Attributes

store[R]

Public Class Methods

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

  @store = {}
end

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/storage_memcached.rb, line 30
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

  @serializer = case @serializer
               when :yajl
                 Yajl
               when :json
                 JSON
               when :marshal
                 Marshal
               end

  options = {
    threadsafe: true,
    namespace: @namespace,
    compress: @compress,
    serializer: @serializer,
    expires_in: @expires_in,
    failover: @failover
  }
  options[:username] = @username if @username
  options[:password] = @password if @password

  @memcached = Dalli::Client.new(@hosts, options)

  object = @memcached.get(@path)
  if object
    begin
      data = @serializer.load(object)
      raise Fluent::ConfigError, "Invalid contents (not object) in plugin memcached storage: '#{@path}'" unless data.is_a?(Hash) unless data.is_a?(Hash)
    rescue => e
      log.error "failed to read data from plugin memcached storage", path: @path, error: e
      raise Fluent::ConfigError, "Unexpected error: failed to read data from plugin memcached storage: '#{@path}'"
    end
  end
end
delete(key) click to toggle source
# File lib/fluent/plugin/storage_memcached.rb, line 115
def delete(key)
  @store.delete(key.to_s)
end
fetch(key, defval) click to toggle source
# File lib/fluent/plugin/storage_memcached.rb, line 107
def fetch(key, defval)
  @store.fetch(key.to_s, defval)
end
get(key) click to toggle source
# File lib/fluent/plugin/storage_memcached.rb, line 103
def get(key)
  @store[key.to_s]
end
load() click to toggle source
# File lib/fluent/plugin/storage_memcached.rb, line 79
def load
  begin
    json_string = @memcached.get(@path)
    json = @serializer.load(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 memcached", path: @path, error: e
  end
end
multi_workers_ready?() click to toggle source
# File lib/fluent/plugin/storage_memcached.rb, line 75
def multi_workers_ready?
  true
end
put(key, value) click to toggle source
# File lib/fluent/plugin/storage_memcached.rb, line 111
def put(key, value)
  @store[key.to_s] = value
end
save() click to toggle source
# File lib/fluent/plugin/storage_memcached.rb, line 94
def save
  begin
    json_string = @serializer.dump(@store)
    @memcached.set(@path, json_string)
  rescue => e
    log.error "failed to save data for plugin storage to memcached", path: @path, error: e
  end
end
update(key, &block) click to toggle source
# File lib/fluent/plugin/storage_memcached.rb, line 119
def update(key, &block)
  @store[key.to_s] = block.call(@store[key.to_s])
end