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
- #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
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/cache2base/core.rb', line 279 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
180 181 182 |
# File 'lib/cache2base/core.rb', line 180 def basename @basename end |
#clean_nil_keys(fields, keys) ⇒ Object
269 270 271 272 273 274 275 276 277 |
# File 'lib/cache2base/core.rb', line 269 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
224 225 226 227 |
# File 'lib/cache2base/core.rb', line 224 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
229 230 231 |
# File 'lib/cache2base/core.rb', line 229 def collection_max(field) @collection_settings[Array(field).join(',').to_s][:max] end |
#collections ⇒ Object
220 221 222 |
# File 'lib/cache2base/core.rb', line 220 def collections @collections end |
#create(params) ⇒ Object
264 265 266 267 |
# File 'lib/cache2base/core.rb', line 264 def create(params) o = self.new(params) o.save end |
#field_accessor(*fields) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/cache2base/core.rb', line 151 def field_accessor(*fields) fields.each do |field| class_eval "def #{field}; @values[:\"#{field}\"]; end" class_eval "def #{field}=(v); @values[:\"#{field}\"] = v; end" end end |
#fields ⇒ Object
233 234 235 |
# File 'lib/cache2base/core.rb', line 233 def fields @fields end |
#find(fields, params = {}) ⇒ Object
241 242 243 244 245 |
# File 'lib/cache2base/core.rb', line 241 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
247 248 249 250 251 |
# File 'lib/cache2base/core.rb', line 247 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
253 254 255 256 257 258 |
# File 'lib/cache2base/core.rb', line 253 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
260 261 262 |
# File 'lib/cache2base/core.rb', line 260 def from_hash(hsh) self.new(hsh, :new_instance => false) end |
#hash_collection?(field) ⇒ Boolean
237 238 239 |
# File 'lib/cache2base/core.rb', line 237 def hash_collection?(field) @collection_settings[Array(field).join(',').to_s][:hash_key] end |
#hash_key(k) ⇒ Object
184 185 186 |
# File 'lib/cache2base/core.rb', line 184 def hash_key(k) Digest::SHA1.hexdigest(k.to_s) end |
#member_of_collection(fields, params = {}) ⇒ Object
210 211 212 213 214 215 216 217 218 |
# File 'lib/cache2base/core.rb', line 210 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
143 144 145 |
# File 'lib/cache2base/core.rb', line 143 def primary_key @primary_key end |
#server ⇒ Object
147 148 149 |
# File 'lib/cache2base/core.rb', line 147 def server @server end |
#set_basename(name) ⇒ Object
139 140 141 |
# File 'lib/cache2base/core.rb', line 139 def set_basename(name) @basename = name.to_s end |
#set_field(field, params) ⇒ Object
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/cache2base/core.rb', line 195 def set_field(field, params) @fields ||= [] @fields << field ||= {} if params[:hash] [field] ||= {} [field][:hash] = true end class_eval "field_accessor :#{field}" end |
#set_fields(*fields) ⇒ Object
188 189 190 191 192 193 |
# File 'lib/cache2base/core.rb', line 188 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
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/cache2base/core.rb', line 158 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
135 136 137 |
# File 'lib/cache2base/core.rb', line 135 def set_ttl(i) @ttl = i.to_i end |
#ttl ⇒ Object
131 132 133 |
# File 'lib/cache2base/core.rb', line 131 def ttl @ttl end |
#uses_hash?(field) ⇒ Boolean
206 207 208 |
# File 'lib/cache2base/core.rb', line 206 def uses_hash?(field) [field] && [field][:hash] end |