Module: Cache2base

Defined in:
lib/cache2base/core.rb,
lib/cache2base/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
'0.0.5'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

:nodoc:



2
3
4
5
6
7
8
# 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.extend(ClassMethods)
end

.init!(params = {}) ⇒ Object



10
11
12
# File 'lib/cache2base/core.rb', line 10

def self.init!(params = {})
  @server = params[:server]
end

.serverObject



14
15
16
# File 'lib/cache2base/core.rb', line 14

def self.server
  @server||MEMBASE
end

Instance Method Details

#add_to_collection(field, loops = 0) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cache2base/core.rb', line 74

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
  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_collectionsObject



68
69
70
71
72
# File 'lib/cache2base/core.rb', line 68

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



64
65
66
# File 'lib/cache2base/core.rb', line 64

def collection_key(field)
  self.class.collection_key(Hash[Array(field).collect {|f| [f, self.send(f)]}])
end

#deleteObject



47
48
49
50
# File 'lib/cache2base/core.rb', line 47

def delete
  remove_from_collections
  server.delete(self.key)
end

#field_hashObject



56
57
58
59
60
61
62
# File 'lib/cache2base/core.rb', line 56

def field_hash
  o = {}
  self.class.fields.each do |field|
    o[field] = self.send(field) if !self.send(field).nil?
  end
  o
end

#initialize(hsh = {}, params = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/cache2base/core.rb', line 27

def initialize(hsh = {}, params = {})
  @new_instance = params[:new_instance].nil? ? true : params[:new_instance]
  hsh.each_pair do |k,v|
    self.send(:"#{k}=", v)
  end
end

#marshalObject



52
53
54
# File 'lib/cache2base/core.rb', line 52

def marshal
  Marshal.dump(self.field_hash)
end

#new?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/cache2base/core.rb', line 34

def new?
  @new_instance
end

#remove_from_collection(field, loops = 0) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cache2base/core.rb', line 103

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_collectionsObject



97
98
99
100
101
# File 'lib/cache2base/core.rb', line 97

def remove_from_collections
  self.class.collections.each do |field|
    raise "Could not remove field #{field} collection" unless remove_from_collection(field)
  end
end

#saveObject



38
39
40
41
42
43
44
45
# File 'lib/cache2base/core.rb', line 38

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

#serverObject



18
19
20
# File 'lib/cache2base/core.rb', line 18

def server
  self.class.server
end

#valid_primary_key?Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/cache2base/core.rb', line 22

def valid_primary_key?
  self.class.primary_key.each { |f| return false if self.send(f).nil? }
  true
end