Class: KSConnect::API::Plugin::Data

Inherits:
Object
  • Object
show all
Includes:
Logs
Defined in:
lib/ksconnect/api/plugin/data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logs

included

Constructor Details

#initialize(plugin_name, domain_name, opts = {}) ⇒ Data

Returns a new instance of Data.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ksconnect/api/plugin/data.rb', line 12

def initialize(plugin_name, domain_name, opts={})
  @plugin_name = plugin_name
  @domain_name = domain_name

  @type = opts[:type] || "data"
  @use_cache = opts[:use_cache].nil? ? true : !!opts[:use_cache]
  @auto_notify = !!opts[:auto_notify]

  @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

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/ksconnect/api/plugin/data.rb', line 10

def type
  @type
end

Instance Method Details

#[](field) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/ksconnect/api/plugin/data.rb', line 43

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



27
28
29
30
31
32
33
# File 'lib/ksconnect/api/plugin/data.rb', line 27

def []=(field, value)
  @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



71
72
73
74
75
76
77
# File 'lib/ksconnect/api/plugin/data.rb', line 71

def delete(field)
  $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

#getallObject



54
55
56
57
58
59
60
61
62
# File 'lib/ksconnect/api/plugin/data.rb', line 54

def getall
  $redis.with { |redis|
    if @use_cache
      @cache = redis.hgetall(key) if @cache.empty?; @cache
    else
      redis.hgetall(key)
    end
  }
end

#keyObject



79
80
81
82
# File 'lib/ksconnect/api/plugin/data.rb', line 79

def key
  set_data_uuid unless @cache_uuid
  "kloudsec_data:#{@cache_uuid}"
end

#reloadObject



64
65
66
67
68
69
# File 'lib/ksconnect/api/plugin/data.rb', line 64

def reload
  $redis.with { |redis|
    @cache = redis.hgetall(key) if @use_cache
    @cache
  }
end

#setall(hash) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/ksconnect/api/plugin/data.rb', line 35

def setall(hash)
  @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