Commit b303d471 by ivan Lan

Fix ohm obj ttl

parent 390bb5b0
......@@ -44,9 +44,10 @@ module WeatherModel
obj.update_ttl self.class.instance_variable_get(:@expire)
end
# def self.all_valid
# self.all.to
# end
def self.all
super.select{ |x| x.attributes.empty?}.map(&:delete)
super
end
private
......
......@@ -6,7 +6,7 @@ RSpec.describe WeatherModel do
include WeatherModel
before do
# start an ohm
Ohm.redis = Redic.new("redis://127.0.0.1:6379")
Ohm.redis = Redic.new("redis://127.0.0.1:6379/5")
# run migration
Dir[File.dirname(__FILE__) + '/../db/migrate/*.rb'].each { |file| require file }
ActiveRecord::Base.establish_connection(
......@@ -23,6 +23,8 @@ RSpec.describe WeatherModel do
end
it 'Forecast' do
@expire = 2
Forecast::ShanghaiTen::Ohm.expire @expire
mysql_obj = Forecast::ShanghaiTen.create({
datetime: '2017-05-16',
cityname: 'shanghai',
......@@ -36,23 +38,13 @@ RSpec.describe WeatherModel do
expect(Forecast::ShanghaiTen.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.get_ttl).to eq(5)
expect(Forecast::ShanghaiTen::Ohm.all.first.get_ttl).to eq(@expire)
# 'ohm 对象 同步 mysql对象更新'
mysql_obj.update(temp_low: 99)
expect(Forecast::ShanghaiTen::Ohm.all.first.temp_low).to eq('99'), 'ohm 对象 同步 mysql对象更新'
# p Forecast::ShanghaiTen::Ohm.all.first.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)
sleep 3
expect(Forecast::ShanghaiTen::Ohm.all.count).to eq(0), 'TTL过期对象删除成功'
end
after do
......
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