Class: KSConnect::API::Plugin::Data
- Inherits:
-
Object
- Object
- KSConnect::API::Plugin::Data
- Includes:
- Logs
- Defined in:
- lib/ksconnect/api/plugin/data.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #[](field) ⇒ Object
- #[]=(field, value) ⇒ Object
- #delete(field) ⇒ Object
- #getall ⇒ Object
-
#initialize(plugin_name, domain_name, opts = {}) ⇒ Data
constructor
A new instance of Data.
- #key ⇒ Object
- #reload ⇒ Object
- #setall(hash) ⇒ Object
Methods included from Logs
Constructor Details
#initialize(plugin_name, domain_name, opts = {}) ⇒ Data
Returns a new instance of Data.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ksconnect/api/plugin/data.rb', line 13 def initialize(plugin_name, domain_name, opts={}) @plugin_name = plugin_name @domain_name = domain_name @type = opts[:type] || "data" @auto_notify = !!opts[:auto_notify] @use_cache = true if ENV['KSCONNECT_READ_ONLY'] @read_only = true @use_cache = false @auto_notify = false end @cache = ActiveSupport::HashWithIndifferentAccess.new if @use_cache @cache_uuid = nil Redis.current ||= Redis.new(driver: :hiredis) $redis ||= ConnectionPool.new(size: MAX_THREADS, timeout: 8) { Redis.current } end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
11 12 13 |
# File 'lib/ksconnect/api/plugin/data.rb', line 11 def type @type end |
Instance Method Details
#[](field) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ksconnect/api/plugin/data.rb', line 52 def [](field) $redis.with { |redis| if @use_cache @cache = redis.hgetall(key) if @cache.empty? @cache[field] else redis.hget(key, field) end } end |
#[]=(field, value) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/ksconnect/api/plugin/data.rb', line 34 def []=(field, value) raise 'Cannot modify key in read only mode' if @read_only @cache[field] = value if @use_cache $redis.with { |redis| redis.hset(key, field, value) redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify } end |
#delete(field) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/ksconnect/api/plugin/data.rb', line 81 def delete(field) raise 'Cannot delete key in read only mode' if @read_only $redis.with { |redis| @cache.delete(field) if @use_cache redis.hdel(key, field) redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify } end |
#getall ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/ksconnect/api/plugin/data.rb', line 63 def getall $redis.with { |redis| if @use_cache @cache = redis.hgetall(key) if @cache.empty?; @cache else redis.hgetall(key) end } end |
#key ⇒ Object
90 91 92 93 |
# File 'lib/ksconnect/api/plugin/data.rb', line 90 def key set_data_uuid unless @cache_uuid "kloudsec_data:#{@cache_uuid}" end |
#reload ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/ksconnect/api/plugin/data.rb', line 73 def reload return nil unless @use_cache $redis.with { |redis| @cache = redis.hgetall(key) @cache } end |
#setall(hash) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/ksconnect/api/plugin/data.rb', line 43 def setall(hash) raise 'Cannot modify keys in read only mode' if @read_only @cache.merge!(hash) if @use_cache $redis.with { |redis| redis.mapped_hmset(key, hash) redis.publish("core:push", { plugin_name: @plugin_name, domain_name: @domain_name, request_type: 'update' }.to_json) if @auto_notify } end |