Class: Fluent::Plugin::MemcachedStorage

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemcachedStorage

Returns a new instance of MemcachedStorage.



24
25
26
27
28
# File 'lib/fluent/plugin/storage_memcached.rb', line 24

def initialize
  super

  @store = {}
end

Instance Attribute Details

#storeObject (readonly)

for test



22
23
24
# File 'lib/fluent/plugin/storage_memcached.rb', line 22

def store
  @store
end

Instance Method Details

#configure(conf) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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 = {
    thread_safe: true,
    namespace: @namespace,
    compress: @compress,
    serializer: @serializer,
    expires_on: @expires_in,
  }
  options[:username] = @username if @username
  options[:password] = @password if @password

  @memcached = Dalli::Client.new("#{@host}:#{@port}", 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 redis storage", path: @path, error: e
      raise Fluent::ConfigError, "Unexpected error: failed to read data from plugin memcached storage: '#{@path}'"
    end
  end
end

#delete(key) ⇒ Object



118
119
120
# File 'lib/fluent/plugin/storage_memcached.rb', line 118

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

#fetch(key, defval) ⇒ Object



110
111
112
# File 'lib/fluent/plugin/storage_memcached.rb', line 110

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

#get(key) ⇒ Object



106
107
108
# File 'lib/fluent/plugin/storage_memcached.rb', line 106

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

#loadObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fluent/plugin/storage_memcached.rb', line 82

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?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/fluent/plugin/storage_memcached.rb', line 74

def multi_workers_ready?
  true
end

#persistent_always?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/fluent/plugin/storage_memcached.rb', line 78

def persistent_always?
  true
end

#put(key, value) ⇒ Object



114
115
116
# File 'lib/fluent/plugin/storage_memcached.rb', line 114

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

#saveObject



97
98
99
100
101
102
103
104
# File 'lib/fluent/plugin/storage_memcached.rb', line 97

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) ⇒ Object



122
123
124
# File 'lib/fluent/plugin/storage_memcached.rb', line 122

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