Cleanup - moved from file based ttl checks to object based ttl checks.

This commit is contained in:
Martin Edenhofer 2019-07-25 08:55:56 +02:00 committed by Thorsten Eckel
parent 0d8a7b7155
commit 595dd01c4e
8 changed files with 37 additions and 154 deletions

View file

@ -1,4 +1,4 @@
class Sessions::Backend::ActivityStream class Sessions::Backend::ActivityStream < Sessions::Backend::Base
attr_writer :user attr_writer :user
@ -46,18 +46,10 @@ class Sessions::Backend::ActivityStream
} }
end end
def client_key
"as::load::#{self.class}::#{@user.id}::#{@client_id}"
end
def push def push
return if !to_run?
# check timeout @time_now = Time.zone.now.to_i
timeout = Sessions::CacheIn.get(client_key)
return if timeout
# set new timeout
Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds })
data = load data = load

View file

@ -10,10 +10,19 @@ class Sessions::Backend::Base
@ttl = ttl @ttl = ttl
@asset_lookup = asset_lookup @asset_lookup = asset_lookup
@last_change = nil @last_change = nil
@time_now = Time.zone.now.to_i end
def to_run?
return true if !@time_now
return true if Time.zone.now.to_i > (@time_now + @ttl)
false
end end
def asset_push(record, assets) def asset_push(record, assets)
if !@time_now
@time_now = Time.zone.now.to_i
end
class_name = record.class.to_s class_name = record.class.to_s
@asset_lookup[class_name] ||= {} @asset_lookup[class_name] ||= {}
@asset_lookup[class_name][record.id] = { @asset_lookup[class_name][record.id] = {

View file

@ -7,15 +7,16 @@ class Sessions::Backend::Collections < Sessions::Backend::Base
@ttl = ttl @ttl = ttl
@asset_lookup = asset_lookup @asset_lookup = asset_lookup
@backends = backend @backends = backend
@time_now = Time.zone.now.to_i
end end
def push def push
return if !to_run?
@time_now = Time.zone.now.to_i
results = [] results = []
@backends.each do |backend| @backends.each do |backend|
#puts "B: #{backend.inspect}"
result = backend.push result = backend.push
#puts "R: #{result.inspect}"
if result if result
results.push result results.push result
end end

View file

@ -11,7 +11,13 @@ class Sessions::Backend::Collections::Base < Sessions::Backend::Base
@ttl = ttl @ttl = ttl
@asset_lookup = asset_lookup @asset_lookup = asset_lookup
@last_change = nil @last_change = nil
@time_now = Time.zone.now.to_i end
def to_run?
return true if !@time_now
return true if Time.zone.now.to_i > (@time_now + @ttl)
false
end end
def load def load
@ -20,18 +26,10 @@ class Sessions::Backend::Collections::Base < Sessions::Backend::Base
self.class.model.constantize.all.order(id: :asc) self.class.model.constantize.all.order(id: :asc)
end end
def client_key
"collections::load::#{self.class}::#{@user.id}::#{@client_id}"
end
def push def push
return if !to_run?
# check timeout @time_now = Time.zone.now.to_i
timeout = Sessions::CacheIn.get(client_key)
return if timeout
# set new timeout
Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds })
# check permission based access # check permission based access
if self.class.permissions if self.class.permissions

View file

@ -18,18 +18,10 @@ class Sessions::Backend::TicketCreate < Sessions::Backend::Base
ticket_create_attributes ticket_create_attributes
end end
def client_key
"as::load::#{self.class}::#{@user.id}::#{@client_id}"
end
def push def push
return if !to_run?
# check timeout @time_now = Time.zone.now.to_i
timeout = Sessions::CacheIn.get(client_key)
return if timeout
# set new timeout
Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds })
data = load data = load

View file

@ -1,8 +1,7 @@
class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base
def self.reset(user_id) def self.reset(user_id)
key = "TicketOverviewPull::#{user_id}" Cache.write("TicketOverviewPull::#{user_id}", { needed: true })
Cache.write(key, { needed: true })
end end
def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 8) def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 8)
@ -15,7 +14,6 @@ class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base
@last_overview = {} @last_overview = {}
@last_overview_change = nil @last_overview_change = nil
@last_ticket_change = nil @last_ticket_change = nil
@time_now = Time.zone.now.to_i
end end
def load def load
@ -35,27 +33,20 @@ class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base
index_and_lists index_and_lists
end end
def client_key def local_to_run?
"as::load::#{self.class}::#{@user.id}::#{@client_id}" return false if !@time_now
end
def work_needed? result = Cache.get("TicketOverviewPull::#{@user.id}")
key = "TicketOverviewPull::#{@user.id}" Cache.delete("TicketOverviewPull::#{@user.id}") if result
if Cache.get(key) return true if result
Cache.delete(key)
return true
end
return false if Sessions::CacheIn.get(client_key)
true false
end end
def push def push
return if !to_run? && !local_to_run?
return if !work_needed? @time_now = Time.zone.now.to_i
# reset check interval
Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds })
# check if min one ticket or overview has changed # check if min one ticket or overview has changed
last_overview_change = Overview.latest_change last_overview_change = Overview.latest_change

View file

@ -1,56 +0,0 @@
module Sessions::CacheIn
# rubocop:disable Style/ClassVars
@@data = {}
@@data_time = {}
@@expires_in = {}
@@expires_in_ttl = {}
# rubocop:enable Style/ClassVars
def self.delete(key)
@@data.delete(key)
@@data_time.delete(key)
end
def self.set(key, value, params = {})
if params[:expires_in]
@@expires_in[key] = Time.zone.now + params[:expires_in]
@@expires_in_ttl[key] = params[:expires_in]
end
@@data[ key ] = value
@@data_time[ key ] = Time.zone.now
end
def self.expired(key, params = {})
# expire if value never was set
return true if !@@data.include? key
# ignore_expire
return false if params[:ignore_expire]
# set re_expire
if params[:re_expire]
if @@expires_in[key]
@@expires_in[key] = Time.zone.now + @@expires_in_ttl[key]
end
return false
end
# check if expired
if @@expires_in[key]
return true if @@expires_in[key] < Time.zone.now
return false
end
# return false if key was set without expires_in
false
end
def self.get(key, params = {})
return if expired( key, params)
@@data[ key ]
end
end

View file

@ -2,50 +2,6 @@ require 'test_helper'
class SessionBasicTest < ActiveSupport::TestCase class SessionBasicTest < ActiveSupport::TestCase
test 'b cache' do
Sessions::CacheIn.set('last_run_test', true, { expires_in: 1.second })
result = Sessions::CacheIn.get('last_run_test')
assert_equal(true, result, 'check 1')
# should not be expired
result = Sessions::CacheIn.expired('last_run_test')
assert_equal(false, result, 'check 1 - expired')
# should be expired
travel 2.seconds
result = Sessions::CacheIn.expired('last_run_test')
assert_equal(true, result, 'check 1 - expired')
# renew expire
result = Sessions::CacheIn.get('last_run_test', re_expire: true)
assert_equal(true, result, 'check 1 - re_expire')
# should not be expired
result = Sessions::CacheIn.expired('last_run_test')
assert_equal(false, result, 'check 1 - expired')
# ignore expired
travel 2.seconds
result = Sessions::CacheIn.get('last_run_test', ignore_expire: true)
assert_equal(true, result, 'check 1 - ignore_expire')
# should be expired
result = Sessions::CacheIn.expired('last_run_test')
assert_equal(true, result, 'check 2')
result = Sessions::CacheIn.get('last_run_test')
assert_nil(result, 'check 2')
# check delete cache
Sessions::CacheIn.set('last_run_delete', true, { expires_in: 5.seconds })
result = Sessions::CacheIn.get('last_run_delete')
assert_equal(true, result, 'check 1')
Sessions::CacheIn.delete('last_run_delete')
result = Sessions::CacheIn.get('last_run_delete')
assert_nil(result, 'check delete')
travel_back
end
test 'c session create / update' do test 'c session create / update' do
# create users # create users