Module: Cache2base::ClassMethods

Defined in:
lib/cache2base/core.rb

Instance Method Summary collapse

Instance Method Details

#all(fields, params = {}) ⇒ Object



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/cache2base/core.rb', line 294

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

#basenameObject



189
190
191
# File 'lib/cache2base/core.rb', line 189

def basename
  @basename
end

#clean_nil_keys(fields, keys) ⇒ Object



284
285
286
287
288
289
290
291
292
# File 'lib/cache2base/core.rb', line 284

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



233
234
235
236
# File 'lib/cache2base/core.rb', line 233

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



238
239
240
# File 'lib/cache2base/core.rb', line 238

def collection_max(field)
  @collection_settings[Array(field).join(',').to_s][:max]
end

#collectionsObject



229
230
231
# File 'lib/cache2base/core.rb', line 229

def collections
  @collections
end

#create(params) ⇒ Object



279
280
281
282
# File 'lib/cache2base/core.rb', line 279

def create(params)
  o = self.new(params)
  o.save
end

#delete(fields, params = {}) ⇒ Object



256
257
258
259
260
# File 'lib/cache2base/core.rb', line 256

def delete(fields, params = {})
  o = find(fields, params)
  return nil unless o
  o.delete
end

#field_accessor(*fields) ⇒ Object



160
161
162
163
164
165
# File 'lib/cache2base/core.rb', line 160

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

#fieldsObject



242
243
244
# File 'lib/cache2base/core.rb', line 242

def fields
  @fields
end

#find(fields, params = {}) ⇒ Object



250
251
252
253
254
# File 'lib/cache2base/core.rb', line 250

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



262
263
264
265
266
# File 'lib/cache2base/core.rb', line 262

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



268
269
270
271
272
273
# File 'lib/cache2base/core.rb', line 268

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



275
276
277
# File 'lib/cache2base/core.rb', line 275

def from_hash(hsh)
  self.new(hsh, :new_instance => false)
end

#hash_collection?(field) ⇒ Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/cache2base/core.rb', line 246

def hash_collection?(field)
  @collection_settings[Array(field).join(',').to_s][:hash_key]
end

#hash_key(k) ⇒ Object



193
194
195
# File 'lib/cache2base/core.rb', line 193

def hash_key(k)
  Digest::SHA1.hexdigest(k.to_s)
end

#member_of_collection(fields, params = {}) ⇒ Object



219
220
221
222
223
224
225
226
227
# File 'lib/cache2base/core.rb', line 219

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_keyObject



144
145
146
# File 'lib/cache2base/core.rb', line 144

def primary_key
  @primary_key
end

#serverObject



148
149
150
# File 'lib/cache2base/core.rb', line 148

def server
  @server
end

#server=(other) ⇒ Object



152
153
154
# File 'lib/cache2base/core.rb', line 152

def server=(other)
  @server = other
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



204
205
206
207
208
209
210
211
212
213
# File 'lib/cache2base/core.rb', line 204

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



197
198
199
200
201
202
# File 'lib/cache2base/core.rb', line 197

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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cache2base/core.rb', line 167

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_server(other) ⇒ Object



156
157
158
# File 'lib/cache2base/core.rb', line 156

def set_server(other)
  @server = other
end

#set_ttl(i) ⇒ Object



136
137
138
# File 'lib/cache2base/core.rb', line 136

def set_ttl(i)
  @ttl = i.to_i
end

#ttlObject



132
133
134
# File 'lib/cache2base/core.rb', line 132

def ttl
  @ttl
end

#uses_hash?(field) ⇒ Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/cache2base/core.rb', line 215

def uses_hash?(field)
  @field_meta[field] && @field_meta[field][:hash]
end