Class: ToolsCache

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/lib/cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ToolsCache

Returns a new instance of ToolsCache.



5
6
# File 'lib/lib/cache.rb', line 5

def initialize(options = {})
end

Class Method Details

.create_cache_file(cache_name, cache_file, ttl = nil) ⇒ Object

Create a cache file in work area

Parameters:

  • cache_name
  • cache_file
  • ttl (defaults to: nil)

Returns:



15
16
17
18
# File 'lib/lib/cache.rb', line 15

def self.create_cache_file cache_name, cache_file, ttl=nil
  cache = Persistent::Cache.new(cache_file, ttl)
  ToolsUtil.set_variable "#{cache_name}_cache", cache
end

.method_missing(method, *args, &block) ⇒ Object

Add a record in a cache work area

Sample

ToolsCache.create_cache_file 'cmdapi', '/home/francisco/.newcmdapi/cmdapi-persistent.cache' cache = ToolsUtil.get_variable 'cmdapi_cache' cache.clear ToolsCache.cmdapi_set :ldap, => {:name => 'rogerio'} ToolsCache.cmdapi_set :ldap, => {:name => 'wagner'} ToolsCache.cmdapi_set :cloud, [100,200] ToolsCache.cmdapi_set :cloud, 300 ToolsCache.cmdapi_set :cloud, [300,500,'teste'] ToolsCache.cmdapi_set :cloud, "teste2" ToolsCache.cmdapi_set :cloud, => 1 ToolsCache.cmdapi_set :nat, "texto para nat" ToolsCache.cmdapi_set :nat, "texto para nat_ depois da primeira" ToolsCache.cmdapi_unset :nat ToolsCache.cmdapi_set :nat, "texto para nat_ depois da primeira"

Parameters:

  • method
  • args
  • block

Returns:



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lib/cache.rb', line 44

def self.method_missing(method, *args, &block)

  cache_name   = method.to_s.split('_').first
  cache_method = method.to_s.split('_').last
  cache        = ToolsUtil.get_variable "#{cache_name}_cache"

  case cache_method

  when 'set'
    key    = args.extract_first
    values = args.extract_first
    unless cache.key?(key)
      cache[key] = values
    else
      aux = cache[key]
      case aux.class.to_s
      when 'Hash'
        begin
          aux.merge! values
          cache[key] = aux
        rescue Exception => e
          log_file =  ToolsUtil.get_variable "#{logger_name}_log_file"
          ToolsDisplay.show "\tError in ToolsCache:  #{e.exception}", :light_yellow
          exit
        end
      when 'Array'
        aux = cache[key]
        case values.class.to_s
        when 'Array'
          values.each do |value|
            aux.insert(-1,value)
          end
          cache[key] = aux
        else
          aux.insert(-1,values)
          cache[key] = aux
        end
      end

    end

  # when 'set'
  #   key        = args.extract_first
  #   value      = args.extract_first
  #   cache.set(key, value, Time.now)

  when 'setext'
    key        = args.extract_first
    values     = args.extract_first
    unless cache.key?(key)
      ToolsCache.cmdapi_set key, values
    else
      aux = cache[key]
      values.keys.each do |value|
        aux[value] = values[value]
      end
      ToolsCache.cmdapi_set key, aux
    end

  when 'unsetext'
    key          = args.extract_first
    key_internal = args.extract_first
    if cache.key?(key)
      aux = cache[key]
      if aux.key? key_internal
        aux
      end
    end

  when 'unset'
    key = args.extract_first
    cache[key] = nil

  when 'get'
    key = args.extract_first
    return cache[key]

  when 'list'
    result = {}
    cache.each do |key|
      result[key] = cache[key]
    end
    return result

  when 'clear'
     cache.each do |key|
      cache[key] = nil
    end
  end

end