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,10 +8,19 @@ module WeatherModel ...@@ -8,10 +8,19 @@ 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
def self.define_ohm settings
const_set('Ohm', Class.new(SourceOhm)).use_settings(settings)
end
def get_ohm
self.class.const_get('Ohm')
end
class SourceOhm < ::Ohm::Model
include ::Ohm::Expire include ::Ohm::Expire
def self.use_settings settings def self.use_settings settings
...@@ -28,6 +37,17 @@ module WeatherModel ...@@ -28,6 +37,17 @@ module WeatherModel
method(option).call(attrs) if attrs method(option).call(attrs) if attrs
end end
end end
# ohm-expire 只处理了create
# 而且 只把 attributes 的值给 TTL了。。。
def update attributes
obj = super(attributes)
obj.update_ttl self.class.instance_variable_get(:@expire)
end
# def self.all_valid
# self.all.to
# end
private private
def self.son his_son # input like :Post def self.son his_son # input like :Post
...@@ -41,11 +61,12 @@ module WeatherModel ...@@ -41,11 +61,12 @@ module WeatherModel
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