Class: Fluent::Plugin::RedisStorage
- Inherits:
-
Storage
- Object
- Storage
- Fluent::Plugin::RedisStorage
- Defined in:
- lib/fluent/plugin/storage_redis.rb
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
for test.
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #delete(key) ⇒ Object
- #fetch(key, defval) ⇒ Object
- #get(key) ⇒ Object
-
#initialize ⇒ RedisStorage
constructor
A new instance of RedisStorage.
- #load ⇒ Object
- #multi_workers_ready? ⇒ Boolean
- #put(key, value) ⇒ Object
- #save ⇒ Object
- #update(key, &block) ⇒ Object
Constructor Details
#initialize ⇒ RedisStorage
Returns a new instance of RedisStorage.
17 18 19 20 21 |
# File 'lib/fluent/plugin/storage_redis.rb', line 17 def initialize super @store = {} end |
Instance Attribute Details
#store ⇒ Object (readonly)
for test
15 16 17 |
# File 'lib/fluent/plugin/storage_redis.rb', line 15 def store @store end |
Instance Method Details
#configure(conf) ⇒ Object
23 24 25 26 27 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 |
# File 'lib/fluent/plugin/storage_redis.rb', line 23 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 = { host: @host, port: @port, thread_safe: true, db: @db_number } [:password] = @password if @password @redis = Redis.new() 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
98 99 100 |
# File 'lib/fluent/plugin/storage_redis.rb', line 98 def delete(key) @store.delete(key.to_s) end |
#fetch(key, defval) ⇒ Object
90 91 92 |
# File 'lib/fluent/plugin/storage_redis.rb', line 90 def fetch(key, defval) @store.fetch(key.to_s, defval) end |
#get(key) ⇒ Object
86 87 88 |
# File 'lib/fluent/plugin/storage_redis.rb', line 86 def get(key) @store[key.to_s] end |
#load ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/fluent/plugin/storage_redis.rb', line 60 def load begin json_string = @redis.get(@path) 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
56 57 58 |
# File 'lib/fluent/plugin/storage_redis.rb', line 56 def multi_workers_ready? true end |
#put(key, value) ⇒ Object
94 95 96 |
# File 'lib/fluent/plugin/storage_redis.rb', line 94 def put(key, value) @store[key.to_s] = value end |
#save ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/fluent/plugin/storage_redis.rb', line 75 def save begin json_string = Yajl::Encoder.encode(@store) @redis.pipelined { @redis.set(@path, json_string) } rescue => e log.error "failed to save data for plugin storage to redis", path: @path, error: e end end |
#update(key, &block) ⇒ Object
102 103 104 |
# File 'lib/fluent/plugin/storage_redis.rb', line 102 def update(key, &block) @store[key.to_s] = block.call(@store[key.to_s]) end |