Class: Fluent::Plugin::Vash

Inherits:
Hash
  • Object
show all
Defined in:
lib/fluent/plugin/vash.rb

Overview

Instance Method Summary collapse

Constructor Details

#initialize(constructor = {}) ⇒ Vash

Returns a new instance of Vash.



7
8
9
10
11
12
13
14
15
# File 'lib/fluent/plugin/vash.rb', line 7

def initialize(constructor = {})
  @register ||= {}
  if constructor.is_a?(Hash)
    super()
    merge(constructor)
  else
    super(constructor)
  end
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
23
24
# File 'lib/fluent/plugin/vash.rb', line 20

def [](key)
  sterilize(key)
  clear(key) if expired?(key)
  regular_reader(key)
end

#[]=(key, *args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fluent/plugin/vash.rb', line 26

def []=(key, *args)
  if args.length == 2
    value, ttl = args[1], args[0]
  elsif args.length == 1
    value, ttl = args[0], 60
  else
    raise ArgumentError, "Wrong number of arguments, expected 2 or 3, received: #{args.length+1}\n"+
      "Example Usage:  volatile_hash[:key]=value OR volatile_hash[:key, ttl]=value"
  end
  sterilize(key)
  ttl(key, ttl)
  regular_writer(key, value)
end

#cleanup!Object



45
46
47
48
# File 'lib/fluent/plugin/vash.rb', line 45

def cleanup!
  now = Time.now.to_i
  @register.map {|k,v| clear(k) if v < now}
end

#clear(key) ⇒ Object



50
51
52
53
54
# File 'lib/fluent/plugin/vash.rb', line 50

def clear(key)
  sterilize(key)
  @register.delete key
  self.delete key
end

#merge(hsh) ⇒ Object



40
41
42
43
# File 'lib/fluent/plugin/vash.rb', line 40

def merge(hsh)
  hsh.map {|key,value| self[sterile(key)] = hsh[key]}
  self
end

#regular_readerObject



18
# File 'lib/fluent/plugin/vash.rb', line 18

alias_method :regular_reader, :[]

#regular_writerObject



17
# File 'lib/fluent/plugin/vash.rb', line 17

alias_method :regular_writer, :[]=