Class: ArangoCache
- Inherits:
-
Object
- Object
- ArangoCache
- Defined in:
- lib/ArangoRB_Cache.rb
Overview
CACHE ====
Class Attribute Summary collapse
-
.max(type:, val:) ⇒ Object
Returns the value of attribute max.
Class Method Summary collapse
- .cache(id: nil, data:) ⇒ Object
- .clear(type: nil, id: nil, data: nil) ⇒ Object
- .retrieve ⇒ Object
- .uncache(type: nil, id: nil, data: nil) ⇒ Object
Class Attribute Details
.max(type:, val:) ⇒ Object
Returns the value of attribute max.
36 37 38 |
# File 'lib/ArangoRB_Cache.rb', line 36 def max @max end |
Class Method Details
.cache(id: nil, data:) ⇒ Object
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 |
# File 'lib/ArangoRB_Cache.rb', line 50 def cache(id: nil, data:) val_to_cache = [] data = [data] unless data.is_a? Array if id.nil? data.each do |d| type = d.class.to_s type.slice! "Arango" if @max[type].nil? type = "Other" idCache = "OTH_#{d.class.to_s}_#{Random.rand(10^12)}" else idCache = d.idCache end val_to_cache << [type, idCache, d] end else id = [id] unless id.is_a? Array if data.length == id.length for i in 0...id.length type = data[i].class.to_s type.slice! "Arango" type = "Other" if @max[type].nil? val_to_cache << [type, id[i], data[i]] end else return "Id should be a String or an Array with the same size of the Data Array" end end val_to_cache.each do |val| @cache[val[0]][val[1]] = val[2] @cache[val[0]].shift if @cache[val[0]].length > @max[val[0]] end val_to_cache end |
.clear(type: nil, id: nil, data: nil) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/ArangoRB_Cache.rb', line 129 def clear(type: nil, id: nil, data: nil) if type.nil? && id.nil? && data.nil? @cache = { "Database" => {}, "Collection" => {}, "Document" => {}, "Graph" => {}, "Vertex" => {}, "Edge" => {}, "Index" => {}, "Query" => {},"User" => {}, "Traversal" => {}, "Transaction" => {} } return true end if !type.nil? && id.nil? && data.nil? @cache[type] = {} return true end val_to_clear = [] unless data.nil? data = [data] unless data.is_a? Array data.each do |d| type = d.class.to_s type.slice! "Arango" next if @max[type].nil? || type == "Other" val_to_clear << [type, d.idCache] end end unless type.nil? || id.nil? id = [id] unless id.is_a? Array if type.is_a? Array if type.length == id.length for i in 0...type.length val_to_clear << [type[i], id[i]] end else return "Type should be a String or an Array with the same size of the Id Array" end elsif type.is_a? String id.each do |idCache| val_to_clear << [type, idCache] end else return "Type should be a String or an Array with the same size of the Id Array" end end val_to_clear.each{|val| @cache[val[0]].delete(val[1])} true end |
.retrieve ⇒ Object
46 47 48 |
# File 'lib/ArangoRB_Cache.rb', line 46 def retrieve @cache end |
.uncache(type: nil, id: nil, data: nil) ⇒ Object
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 |
# File 'lib/ArangoRB_Cache.rb', line 88 def uncache(type: nil, id: nil, data: nil) if id.nil? && data.nil? && !type.nil? val_to_uncache = @cache[type].map{|k,v| v} val_to_uncache = val_to_uncache[0] if val_to_uncache.length == 1 return val_to_uncache end val_to_uncache = [] unless data.nil? data = [data] unless data.is_a? Array data.each do |d| type = d.class.to_s type.slice! "Arango" next if @max[type].nil? || type == "Other" idCache = d.idCache val_to_uncache << [type, idCache] end end unless type.nil? || id.nil? id = [id] unless id.is_a? Array if type.is_a? Array if type.length == id.length for i in 0...type.length val_to_uncache << [type[i], id[i]] end else return "Type should be a String or an Array with the same size of the Id Array" end elsif type.is_a? String id.each do |idCache| val_to_uncache << [type, idCache] end else return "Type should be a String or an Array with the same size of the Id Array" end end val_to_uncache = val_to_uncache.map{|val| @cache[val[0]][val[1]]} val_to_uncache = val_to_uncache[0] if val_to_uncache.length == 1 val_to_uncache end |