Commit 390bb5b0 by ivan Lan

Fix Ohm definition to set the right key

parent da693bf7
...@@ -20,7 +20,7 @@ module WeatherModel ...@@ -20,7 +20,7 @@ module WeatherModel
}, },
options: { options: {
index: [:cityname], index: [:cityname],
expire: 5, expire: 1,
unique: :mysql_id, unique: :mysql_id,
father: nil, father: nil,
son: nil, son: nil,
......
...@@ -8,44 +8,65 @@ module WeatherModel ...@@ -8,44 +8,65 @@ module WeatherModel
after_save :update_ohm after_save :update_ohm
def self.use_settings settings def self.use_settings settings
self.table_name = settings[:table_name] self.table_name = settings[:table_name]
Ohm.use_settings settings self.define_ohm settings
end end
class Ohm < ::Ohm::Model
include ::Ohm::Expire def self.define_ohm settings
def self.use_settings settings const_set('Ohm', Class.new(SourceOhm)).use_settings(settings)
end
attribute :mysql_id
attribute :updated_at def get_ohm
index :mysql_id self.class.const_get('Ohm')
settings[:schema].keys.each { |attr| end
attribute attr
} class SourceOhm < ::Ohm::Model
[:index, :unique, :expire, :son, :dad].each do |option| include ::Ohm::Expire
attrs = settings[:options][option] def self.use_settings settings
attrs.is_a?(Array) ?
attrs.each{ |attr| method(option).call(attr) if attr } : attribute :mysql_id
method(option).call(attrs) if attrs attribute :updated_at
index :mysql_id
settings[:schema].keys.each { |attr|
attribute attr
}
[:index, :unique, :expire, :son, :dad].each do |option|
attrs = settings[:options][option]
attrs.is_a?(Array) ?
attrs.each{ |attr| method(option).call(attr) if attr } :
method(option).call(attrs) if attrs
end
end
# ohm-expire 只处理了create
# 而且 只把 attributes 的值给 TTL了。。。
def update attributes
obj = super(attributes)
obj.update_ttl self.class.instance_variable_get(:@expire)
end end
end
private # def self.all_valid
def self.son his_son # input like :Post # self.all.to
# collection :posts, :Post # end
collection(his_son.to_s.underscore.pluralize.to_sym, his_son) if his_son && SourceSchema.const_defined?(his_son)
end
private
def self.son his_son # input like :Post
# collection :posts, :Post
collection(his_son.to_s.underscore.pluralize.to_sym, his_son) if his_son && SourceSchema.const_defined?(his_son)
end
def self.dad his_dad # input like :Post def self.dad his_dad # input like :Post
# reference :post, :Post # reference :post, :Post
reference(his_dad.to_s.underscore.to_sym, his_dad ) if his_dad && SourceSchema.const_defined?(his_dad) reference(his_dad.to_s.underscore.to_sym, his_dad ) if his_dad && SourceSchema.const_defined?(his_dad)
end
end end
end
private private
def update_ohm def update_ohm
ohm_attrs = self.attributes.except('created_at', 'id').merge(mysql_id: id) ohm_attrs = self.attributes.except('created_at', 'id').merge(mysql_id: id)
# find 找不到 会自动生成 # find 找不到 会自动生成
ohm_obj = Ohm.find(mysql_id: id).first.update(ohm_attrs) ohm_obj = get_ohm.find(mysql_id: id).first.try(:update, ohm_attrs) || get_ohm.create(ohm_attrs)
# rescue Ohm::UniqueIndexViolation mysql_id should be unique # rescue Ohm::UniqueIndexViolation mysql_id should be unique
end end
end end
......
...@@ -35,13 +35,23 @@ RSpec.describe WeatherModel do ...@@ -35,13 +35,23 @@ RSpec.describe WeatherModel do
}) })
expect(Forecast::ShanghaiTen.count).to eq(1) expect(Forecast::ShanghaiTen.count).to eq(1)
expect(Forecast::ShanghaiTen::Ohm.all.count).to eq(1) expect(Forecast::ShanghaiTen::Ohm.all.count).to eq(1)
expect(Forecast::ShanghaiTen::Ohm.all.first.temp_low).to eq(mysql_obj.temp_low.to_s) expect(Forecast::ShanghaiTen::Ohm.all.first.temp_low).to eq(mysql_obj.temp_low.to_s)
# expect(Forecast::ShanghaiTen::Ohm.all.first.get_ttl).to eq(5)
# 'ohm 对象 同步 mysql对象更新' # 'ohm 对象 同步 mysql对象更新'
mysql_obj.update(temp_low: 99) mysql_obj.update(temp_low: 99)
expect(Forecast::ShanghaiTen::Ohm.all.first.temp_low).to eq('99'), 'ohm 对象 同步 mysql对象更新' expect(Forecast::ShanghaiTen::Ohm.all.first.temp_low).to eq('99'), 'ohm 对象 同步 mysql对象更新'
# ttl 失败 # p Forecast::ShanghaiTen::Ohm.all.first.get_ttl
# p Forecast::ShanghaiTen::Ohm.all.to_a.map(&:get_ttl) # sleep 4
# expect(Forecast::ShanghaiTen::Ohm.all.count).to eq(1)
p Forecast::ShanghaiTen::Ohm.all.first.key
# sleep 2
p
p Ohm.redis.call('KEYS', '*ShanghaiTen*')
# p Ohm.redis.call('HGETALL', 'Forecast::ShanghaiTen::Ohm:indices:cityname:shanghai')
# p Forecast::ShanghaiTen::Ohm.find(mysql_id: mysql_obj.id).to_a
# expect(Forecast::ShanghaiTen::Ohm.find(mysql_id: mysql_obj.id)).to eq(0)
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment