diff --git a/lib/sessions/backend/activity_stream.rb b/lib/sessions/backend/activity_stream.rb index 0498d24d4..2901fb225 100644 --- a/lib/sessions/backend/activity_stream.rb +++ b/lib/sessions/backend/activity_stream.rb @@ -1,4 +1,4 @@ -class Sessions::Backend::ActivityStream +class Sessions::Backend::ActivityStream < Sessions::Backend::Base attr_writer :user @@ -46,18 +46,10 @@ class Sessions::Backend::ActivityStream } end - def client_key - "as::load::#{self.class}::#{@user.id}::#{@client_id}" - end - def push + return if !to_run? - # check timeout - timeout = Sessions::CacheIn.get(client_key) - return if timeout - - # set new timeout - Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds }) + @time_now = Time.zone.now.to_i data = load diff --git a/lib/sessions/backend/base.rb b/lib/sessions/backend/base.rb index 1f70fcf09..14da840c8 100644 --- a/lib/sessions/backend/base.rb +++ b/lib/sessions/backend/base.rb @@ -10,10 +10,19 @@ class Sessions::Backend::Base @ttl = ttl @asset_lookup = asset_lookup @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 def asset_push(record, assets) + if !@time_now + @time_now = Time.zone.now.to_i + end class_name = record.class.to_s @asset_lookup[class_name] ||= {} @asset_lookup[class_name][record.id] = { diff --git a/lib/sessions/backend/collections.rb b/lib/sessions/backend/collections.rb index f18f66201..8f8683d71 100644 --- a/lib/sessions/backend/collections.rb +++ b/lib/sessions/backend/collections.rb @@ -7,15 +7,16 @@ class Sessions::Backend::Collections < Sessions::Backend::Base @ttl = ttl @asset_lookup = asset_lookup @backends = backend - @time_now = Time.zone.now.to_i end def push + return if !to_run? + + @time_now = Time.zone.now.to_i + results = [] @backends.each do |backend| - #puts "B: #{backend.inspect}" result = backend.push - #puts "R: #{result.inspect}" if result results.push result end diff --git a/lib/sessions/backend/collections/base.rb b/lib/sessions/backend/collections/base.rb index ee22dbcd2..3e30da062 100644 --- a/lib/sessions/backend/collections/base.rb +++ b/lib/sessions/backend/collections/base.rb @@ -11,7 +11,13 @@ class Sessions::Backend::Collections::Base < Sessions::Backend::Base @ttl = ttl @asset_lookup = asset_lookup @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 def load @@ -20,18 +26,10 @@ class Sessions::Backend::Collections::Base < Sessions::Backend::Base self.class.model.constantize.all.order(id: :asc) end - def client_key - "collections::load::#{self.class}::#{@user.id}::#{@client_id}" - end - def push + return if !to_run? - # check timeout - timeout = Sessions::CacheIn.get(client_key) - return if timeout - - # set new timeout - Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds }) + @time_now = Time.zone.now.to_i # check permission based access if self.class.permissions diff --git a/lib/sessions/backend/ticket_create.rb b/lib/sessions/backend/ticket_create.rb index 1aa7b9771..a6b6601ae 100644 --- a/lib/sessions/backend/ticket_create.rb +++ b/lib/sessions/backend/ticket_create.rb @@ -18,18 +18,10 @@ class Sessions::Backend::TicketCreate < Sessions::Backend::Base ticket_create_attributes end - def client_key - "as::load::#{self.class}::#{@user.id}::#{@client_id}" - end - def push + return if !to_run? - # check timeout - timeout = Sessions::CacheIn.get(client_key) - return if timeout - - # set new timeout - Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds }) + @time_now = Time.zone.now.to_i data = load diff --git a/lib/sessions/backend/ticket_overview_list.rb b/lib/sessions/backend/ticket_overview_list.rb index 31c78166f..381057e9c 100644 --- a/lib/sessions/backend/ticket_overview_list.rb +++ b/lib/sessions/backend/ticket_overview_list.rb @@ -1,8 +1,7 @@ class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base def self.reset(user_id) - key = "TicketOverviewPull::#{user_id}" - Cache.write(key, { needed: true }) + Cache.write("TicketOverviewPull::#{user_id}", { needed: true }) end 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_change = nil @last_ticket_change = nil - @time_now = Time.zone.now.to_i end def load @@ -35,27 +33,20 @@ class Sessions::Backend::TicketOverviewList < Sessions::Backend::Base index_and_lists end - def client_key - "as::load::#{self.class}::#{@user.id}::#{@client_id}" - end + def local_to_run? + return false if !@time_now - def work_needed? - key = "TicketOverviewPull::#{@user.id}" - if Cache.get(key) - Cache.delete(key) - return true - end - return false if Sessions::CacheIn.get(client_key) + result = Cache.get("TicketOverviewPull::#{@user.id}") + Cache.delete("TicketOverviewPull::#{@user.id}") if result + return true if result - true + false end def push + return if !to_run? && !local_to_run? - return if !work_needed? - - # reset check interval - Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds }) + @time_now = Time.zone.now.to_i # check if min one ticket or overview has changed last_overview_change = Overview.latest_change diff --git a/lib/sessions/cache_in.rb b/lib/sessions/cache_in.rb deleted file mode 100644 index f6046a663..000000000 --- a/lib/sessions/cache_in.rb +++ /dev/null @@ -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 diff --git a/test/unit/session_basic_test.rb b/test/unit/session_basic_test.rb index 7d8d8f789..e96c1b8d0 100644 --- a/test/unit/session_basic_test.rb +++ b/test/unit/session_basic_test.rb @@ -2,50 +2,6 @@ require 'test_helper' 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 # create users