Module: Cache2base::ClassMethods
- Defined in:
- lib/cache2base/core.rb
Instance Method Summary collapse
- #all(fields, params = {}) ⇒ Object
- #basename ⇒ Object
- #clean_nil_keys(fields, keys) ⇒ Object
- #collection_key(vhsh) ⇒ Object
- #collection_max(field) ⇒ Object
- #collections ⇒ Object
- #create(params) ⇒ Object
- #delete(fields, params = {}) ⇒ Object
- #field_accessor(*fields) ⇒ Object
- #fields ⇒ Object
- #find(fields, params = {}) ⇒ Object
- #find_by_key(key) ⇒ Object
- #find_by_keys(keys) ⇒ Object
- #from_hash(hsh) ⇒ Object
- #hash_collection?(field) ⇒ Boolean
- #hash_key(k) ⇒ Object
- #member_of_collection(fields, params = {}) ⇒ Object
- #primary_key ⇒ Object
- #server ⇒ Object
- #set_basename(name) ⇒ Object
- #set_field(field, params) ⇒ Object
- #set_fields(*fields) ⇒ Object
- #set_primary_key(mk, params = {}) ⇒ Object
- #set_ttl(i) ⇒ Object
- #ttl ⇒ Object
- #uses_hash?(field) ⇒ Boolean
Instance Method Details
#all(fields, params = {}) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/cache2base/core.rb', line 286 def all(fields, params = {}) keys = server.get(collection_key(fields)) hsh = server.get_multi(keys) nils = [] o = (keys||[]).collect do |key| # to get it back in order since get_multi results in a hash if hsh[key] self.from_hash(Marshal.load(hsh[key])) else nils << key nil end end.compact # I do not like doing "garbage collection" on read, but cant find any other place to put it. clean_nil_keys(fields, nils) if @ttl > 0 && !nils.empty? o end |
#basename ⇒ Object
181 182 183 |
# File 'lib/cache2base/core.rb', line 181 def basename @basename end |
#clean_nil_keys(fields, keys) ⇒ Object
276 277 278 279 280 281 282 283 284 |
# File 'lib/cache2base/core.rb', line 276 def clean_nil_keys(fields, keys) server.cas(collection_key(fields), @ttl) do |current_keys| keys.each do |key| current_keys.delete(key) end current_keys end end |
#collection_key(vhsh) ⇒ Object
225 226 227 228 |
# File 'lib/cache2base/core.rb', line 225 def collection_key(vhsh) keys = vhsh.keys.sort {|a,b| a.to_s <=> b.to_s} "#{@basename}_c_#{hash_collection?(keys) ? hash_key(keys.collect {|field| vhsh[field].to_s.gsub('_','-') }.join("_")) : keys.collect {|field| vhsh[field].to_s.gsub('_','-') }.join("_")}" end |
#collection_max(field) ⇒ Object
230 231 232 |
# File 'lib/cache2base/core.rb', line 230 def collection_max(field) @collection_settings[Array(field).join(',').to_s][:max] end |
#collections ⇒ Object
221 222 223 |
# File 'lib/cache2base/core.rb', line 221 def collections @collections end |
#create(params) ⇒ Object
271 272 273 274 |
# File 'lib/cache2base/core.rb', line 271 def create(params) o = self.new(params) o.save end |
#delete(fields, params = {}) ⇒ Object
248 249 250 251 252 |
# File 'lib/cache2base/core.rb', line 248 def delete(fields, params = {}) o = find(fields, params) return nil unless o o.delete end |
#field_accessor(*fields) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/cache2base/core.rb', line 152 def field_accessor(*fields) fields.each do |field| class_eval "def #{field}; @values[:\"#{field}\"]; end" class_eval "def #{field}=(v); if(v.nil?); @values.delete(:\"#{field}\"); else; @values[:\"#{field}\"] = v; end; end" end end |
#fields ⇒ Object
234 235 236 |
# File 'lib/cache2base/core.rb', line 234 def fields @fields end |
#find(fields, params = {}) ⇒ Object
242 243 244 245 246 |
# File 'lib/cache2base/core.rb', line 242 def find(fields, params = {}) o = server.get(key(fields)) return nil unless o self.from_hash(Marshal.load(o)) end |
#find_by_key(key) ⇒ Object
254 255 256 257 258 |
# File 'lib/cache2base/core.rb', line 254 def find_by_key(key) o = server.get(key) return nil unless o self.from_hash(Marshal.load(o)) end |
#find_by_keys(keys) ⇒ Object
260 261 262 263 264 265 |
# File 'lib/cache2base/core.rb', line 260 def find_by_keys(keys) hsh = server.get_multi(keys) keys.collect do |key| # to get it back in order since get_multi results in a hash hsh[key] ? self.from_hash(Marshal.load(hsh[key])) : nil end.compact end |
#from_hash(hsh) ⇒ Object
267 268 269 |
# File 'lib/cache2base/core.rb', line 267 def from_hash(hsh) self.new(hsh, :new_instance => false) end |
#hash_collection?(field) ⇒ Boolean
238 239 240 |
# File 'lib/cache2base/core.rb', line 238 def hash_collection?(field) @collection_settings[Array(field).join(',').to_s][:hash_key] end |
#hash_key(k) ⇒ Object
185 186 187 |
# File 'lib/cache2base/core.rb', line 185 def hash_key(k) Digest::SHA1.hexdigest(k.to_s) end |
#member_of_collection(fields, params = {}) ⇒ Object
211 212 213 214 215 216 217 218 219 |
# File 'lib/cache2base/core.rb', line 211 def member_of_collection(fields, params = {}) fields = Array(fields).sort { |a,b| a.to_s <=> b.to_s } @collections ||= [] @collections << fields @collection_settings ||= {} @collection_settings[fields.join(",").to_s] = {} @collection_settings[fields.join(",").to_s][:hash_key] = true if params[:hash_key] @collection_settings[fields.join(",").to_s][:max] = params[:max].to_i if params[:max] end |
#primary_key ⇒ Object
144 145 146 |
# File 'lib/cache2base/core.rb', line 144 def primary_key @primary_key end |
#server ⇒ Object
148 149 150 |
# File 'lib/cache2base/core.rb', line 148 def server @server end |
#set_basename(name) ⇒ Object
140 141 142 |
# File 'lib/cache2base/core.rb', line 140 def set_basename(name) @basename = name.to_s end |
#set_field(field, params) ⇒ Object
196 197 198 199 200 201 202 203 204 205 |
# File 'lib/cache2base/core.rb', line 196 def set_field(field, params) @fields ||= [] @fields << field @field_meta ||= {} if params[:hash] @field_meta[field] ||= {} @field_meta[field][:hash] = true end class_eval "field_accessor :#{field}" end |
#set_fields(*fields) ⇒ Object
189 190 191 192 193 194 |
# File 'lib/cache2base/core.rb', line 189 def set_fields(*fields) @fields = @fields ? (@fields + (fields)) : (fields) fields.each do |field| class_eval "field_accessor :#{field}" end end |
#set_primary_key(mk, params = {}) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/cache2base/core.rb', line 159 def set_primary_key(mk, params = {}) @primary_key = Array(mk) #o = '#{self.class}' #c = "#{self}" #h = "#{self}" o = [] c = [] h = [] Array(mk).each_with_index do |v, i| o << '#{self.send(:'+v.to_s+').to_s.gsub(\'_\',\'-\')}' c << '#{Array(pk)['+i.to_s+'].to_s.gsub(\'_\',\'-\')}' h << '#{pk[0][:'+v.to_s+'].to_s.gsub(\'_\',\'-\')}' end o = "#{@basename}_\#{#{params[:hash_key] ? "self.class.hash_key(\"#{o.join("_")}\")" : "\"#{o.join("_")}\""}}" c = "#{@basename}_\#{#{params[:hash_key] ? "hash_key(\"#{c.join("_")}\")" : "\"#{c.join("_")}\""}}" h = "#{@basename}_\#{#{params[:hash_key] ? "hash_key(\"#{h.join("_")}\")" : "\"#{h.join("_")}\""}}" class_eval "def key; \"#{o}\"; end" class_eval "def self.key(*pk); pk.first.is_a?(Hash) ? \"#{h}\" : \"#{c}\"; end" end |
#set_ttl(i) ⇒ Object
136 137 138 |
# File 'lib/cache2base/core.rb', line 136 def set_ttl(i) @ttl = i.to_i end |
#ttl ⇒ Object
132 133 134 |
# File 'lib/cache2base/core.rb', line 132 def ttl @ttl end |
#uses_hash?(field) ⇒ Boolean
207 208 209 |
# File 'lib/cache2base/core.rb', line 207 def uses_hash?(field) @field_meta[field] && @field_meta[field][:hash] end |