Module: Cache2base
- Defined in:
- lib/cache2base/core.rb,
lib/cache2base/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
'0.0.9'
Class Method Summary collapse
Instance Method Summary collapse
- #add_to_collection(field, loops = 0) ⇒ Object
- #add_to_collections ⇒ Object
- #collection_key(field) ⇒ Object
- #collection_max(field) ⇒ Object
- #delete ⇒ Object
- #field_hash ⇒ Object
- #initialize(hsh = {}, params = {}) ⇒ Object
- #marshal ⇒ Object
- #new? ⇒ Boolean
- #remove_from_collection(field, loops = 0) ⇒ Object
- #remove_from_collections ⇒ Object
- #save ⇒ Object
- #server ⇒ Object
- #valid_primary_key? ⇒ Boolean
Class Method Details
.included(klass) ⇒ Object
:nodoc:
2 3 4 5 6 7 8 9 |
# File 'lib/cache2base/core.rb', line 2 def self.included(klass) # :nodoc: klass.class_eval "@basename ||= self.to_s" klass.class_eval "@ttl ||= 0" klass.class_eval "@collections ||= []" klass.class_eval "@server ||= Cache2base.server" klass.class_eval "attr_accessor :values" klass.extend(ClassMethods) end |
.init!(params = {}) ⇒ Object
11 12 13 |
# File 'lib/cache2base/core.rb', line 11 def self.init!(params = {}) @server = params[:server] end |
.server ⇒ Object
15 16 17 |
# File 'lib/cache2base/core.rb', line 15 def self.server @server||MEMBASE end |
Instance Method Details
#add_to_collection(field, loops = 0) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cache2base/core.rb', line 82 def add_to_collection(field, loops = 0) Array(field).each { |f| return 'could_not_add' if self.send(f).nil? } # still evaluates to true, so add_to_collections does not fail success = server.cas(collection_key(field), self.class.ttl) do |value| value << self.key unless value.include?(self.key) value = value.drop(value.length - collection_max(field)) if collection_max(field) && value.length > collection_max(field) value end unless success if success.nil? success = server.add(collection_key(field), [self.key], self.class.ttl) if success return true else return loops < 5 ? add_to_collection(field, loops+1) : false end else return loops < 5 ? add_to_collection(field, loops+1) : false end end success end |
#add_to_collections ⇒ Object
76 77 78 79 80 |
# File 'lib/cache2base/core.rb', line 76 def add_to_collections self.class.collections.each do |field| raise "Could not add field #{field} collection" unless add_to_collection(field) end end |
#collection_key(field) ⇒ Object
68 69 70 |
# File 'lib/cache2base/core.rb', line 68 def collection_key(field) self.class.collection_key(Hash[Array(field).collect {|f| [f, self.send(f)]}]) end |
#collection_max(field) ⇒ Object
72 73 74 |
# File 'lib/cache2base/core.rb', line 72 def collection_max(field) self.class.collection_max(field) end |
#delete ⇒ Object
50 51 52 53 |
# File 'lib/cache2base/core.rb', line 50 def delete remove_from_collections server.delete(self.key) end |
#field_hash ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/cache2base/core.rb', line 59 def field_hash @values #o = {} #self.class.fields.each do |field| # o[field] = self.send(field) if !self.send(field).nil? #end #o end |
#initialize(hsh = {}, params = {}) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/cache2base/core.rb', line 28 def initialize(hsh = {}, params = {}) @new_instance = params[:new_instance].nil? ? true : params[:new_instance] #@values ||= hsh @values ||= {} hsh.each_pair do |k,v| self.send(:"#{k}=", v) if self.respond_to?(k) end end |
#marshal ⇒ Object
55 56 57 |
# File 'lib/cache2base/core.rb', line 55 def marshal Marshal.dump(self.field_hash) end |
#new? ⇒ Boolean
37 38 39 |
# File 'lib/cache2base/core.rb', line 37 def new? @new_instance end |
#remove_from_collection(field, loops = 0) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/cache2base/core.rb', line 112 def remove_from_collection(field, loops=0) Array(field).each { |f| return 'could_not_add' if self.send(f).nil? } #return 'could_not_remove' if self.send(field).nil? # still evaluates to true, so remove_from_collections does not fail success = server.cas(collection_key(field), self.class.ttl) do |value| value.delete(self.key) value end unless success if success.nil? return true # return true because theres no collection to remove from else return loops < 5 ? remove_from_collection(field, loops+1) : false # race conditions end end success end |
#remove_from_collections ⇒ Object
106 107 108 109 110 |
# File 'lib/cache2base/core.rb', line 106 def remove_from_collections self.class.collections.each do |field| raise "Could not remove field #{field} collection" unless remove_from_collection(field) end end |
#save ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/cache2base/core.rb', line 41 def save raise "Invalid Primary Key" unless valid_primary_key? add_to_collections result = @new_instance ? server.add(self.key, self.marshal, self.class.ttl) : server.set(self.key, self.marshal, self.class.ttl) raise 'Duplicate Primary Key' unless result @new_instance = false self end |
#server ⇒ Object
19 20 21 |
# File 'lib/cache2base/core.rb', line 19 def server self.class.server end |
#valid_primary_key? ⇒ Boolean
23 24 25 26 |
# File 'lib/cache2base/core.rb', line 23 def valid_primary_key? self.class.primary_key.each { |f| return false if self.send(f).nil? } true end |