Class: JOffice::Redis::CacheModel

Inherits:
Object
  • Object
show all
Defined in:
lib/joffice_redis/cache_model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ CacheModel

Returns a new instance of CacheModel.



162
163
164
# File 'lib/joffice_redis/cache_model.rb', line 162

def initialize(attributes={})
  add_attributes(attributes)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



160
161
162
# File 'lib/joffice_redis/cache_model.rb', line 160

def id
  @id
end

Class Method Details

.[](id) ⇒ Object



62
63
64
# File 'lib/joffice_redis/cache_model.rb', line 62

def [](id)        
  new(:id=>id) if exists?(id)       
end

.bulk_set(&block) ⇒ Object



41
42
43
# File 'lib/joffice_redis/cache_model.rb', line 41

def bulk_set(&block)
  db.bulk_set(&block)
end

.each_values_for_ids(ids, select = []) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/joffice_redis/cache_model.rb', line 118

def each_values_for_ids(ids, select=[])
  values=db.mget(keys_for_ids(select||=all_attributes, ids))        
  i=-1
  ids.each do |id|
    result={:id=>id}
    select.each do |name|
      result[name]=send(:"unbox_value_#{name}", values[i+=1])            
    end
    yield result  
  end                
end

.find_by_batch(options, &block) ⇒ Object



141
142
143
# File 'lib/joffice_redis/cache_model.rb', line 141

def find_by_batch(options, &block)  
  find_by_batch_set(key_id_set, options, &block)        
end

.find_by_batch_set(set_key, options, &block) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/joffice_redis/cache_model.rb', line 145

def find_by_batch_set(set_key, options, &block)                
  if ((keys=db.members(set_key)) && !keys.empty?)       
    i,l,batch_size,select,default=0,keys.length,(options[:batch_size] || 200),(options[:select] || []),(options[:default] || {})
    select-=default.keys
    sl=select.length
    while(i<l)
      next_id=i+batch_size           
      block.call(load_by_ids(keys[i..next_id-1], select, default))
      i=next_id
    end          
  end        
end

.flush_dbObject



36
37
38
# File 'lib/joffice_redis/cache_model.rb', line 36

def flush_db
  db.flush_db
end

.get(id) ⇒ Object



66
67
68
# File 'lib/joffice_redis/cache_model.rb', line 66

def get(id)
  new(:id=>id)
end

.keys_for_id(id, attributes) ⇒ Object



88
89
90
91
92
# File 'lib/joffice_redis/cache_model.rb', line 88

def keys_for_id(id, attributes)                
  attributes.map do |name|
    key_name(id,name)
  end      
end

.keys_for_ids(attributes, ids) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/joffice_redis/cache_model.rb', line 78

def keys_for_ids(attributes, ids)
  result=[]
  ids.each do |id|
    attributes.each do |name|
      result << key_name(id,name)
    end
  end
  result
end

.load_by_ids(ids, select = [], default = {}) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/joffice_redis/cache_model.rb', line 131

def load_by_ids(ids, select=[],default={})
  return [self[ids].ensure_attributes(select)] unless ids.instance_of?(Array)
  return [] if ids.empty?
  items=[]
  each_values_for_ids(ids, select) do |item|
    items << new(item).add_attributes(default)
  end        
  items
end

.load_value(id, name) ⇒ Object



94
95
96
# File 'lib/joffice_redis/cache_model.rb', line 94

def load_value(id, name)
  send(:"unbox_value_#{name}", db.get(key_name(id, name)))
end

.load_values_by_id(id, select = []) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/joffice_redis/cache_model.rb', line 98

def load_values_by_id(id, select=[])
  values=db.mget(keys_for_id(id, select||=all_attributes))
  i=-1        
  select.map do |name|
    send(:"unbox_value_#{name}", values[i+=1])            
  end         
end

.load_values_for_ids(ids, select = []) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/joffice_redis/cache_model.rb', line 106

def load_values_for_ids(ids, select=[])
  values=db.mget(keys_for_ids(select||=all_attributes, ids))
  result=[]
  i=-1
  ids.each do |id|
    select.each do |name|
      result << send(:"unbox_value_#{name}", values[i+=1])            
    end
  end
  result        
end

.loggerObject



31
32
33
# File 'lib/joffice_redis/cache_model.rb', line 31

def logger
  @@logger||=nil
end

.logger=(v) ⇒ Object



26
27
28
# File 'lib/joffice_redis/cache_model.rb', line 26

def logger=(v)
  @@logger=v
end

.model(&block) ⇒ Object



58
59
60
# File 'lib/joffice_redis/cache_model.rb', line 58

def model(&block) 
  JOffice::Redis::ModelFactory.new(self, logger, &block)  
end

.transaction(&block) ⇒ Object

Добавление всех данных в одной транзакции

Redis send:
  MULTI
  COMMAND_1 ...
  COMMAND_2 ...
  COMMAND_N ...
  EXEC or DISCARD


54
55
56
# File 'lib/joffice_redis/cache_model.rb', line 54

def transaction(&block)
  db.transaction(&block)
end

.update(id, attributes = {}) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/joffice_redis/cache_model.rb', line 70

def update(id, attributes={})
  unless attributes.blank?
    item=new(:id=>id)
    attributes.each do |key, value|
      item.send(key, value)
    end
  end
end

Instance Method Details

#add_attributes(attributes = {}) ⇒ Object



166
167
168
169
170
171
# File 'lib/joffice_redis/cache_model.rb', line 166

def add_attributes(attributes={})
  attributes.each do |k,v|
    instance_variable_set("@#{k}", v)       
  end
  self
end

#ensure_attributes(*attribute_names) ⇒ Object



189
190
191
192
193
194
195
196
# File 'lib/joffice_redis/cache_model.rb', line 189

def ensure_attributes(*attribute_names)      
  missed_attributes=[]
  attribute_names.flatten.each do |name|
    missed_attributes << name unless instance_variable_defined?("@#{name}")
  end
  select_attributes(*missed_attributes) unless missed_attributes.empty?
  self
end

#load_allObject



185
186
187
# File 'lib/joffice_redis/cache_model.rb', line 185

def load_all
  select_attributes(self.class.all_attributes)
end

#load_from(attributes, values) ⇒ Object



179
180
181
182
183
# File 'lib/joffice_redis/cache_model.rb', line 179

def load_from(attributes, values)
  attributes.each_with_index do |name, index|
    instance_variable_set("@#{name}", values[index])
  end
end

#select_attributes(*attribute_names) ⇒ Object



198
199
200
201
# File 'lib/joffice_redis/cache_model.rb', line 198

def select_attributes(*attribute_names)
  a=attribute_names.flatten.uniq
  load_from(a, self.class.load_values_by_id(id, a))
end

#setv(name, value) ⇒ Object



173
174
175
176
# File 'lib/joffice_redis/cache_model.rb', line 173

def setv(name, value)
  instance_variable_set("@#{name}", value)
  self
end