Browse Source

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

Martin Edenhofer 5 years ago
parent
commit
595dd01c4e

+ 3 - 11
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
 

+ 10 - 1
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] = {

+ 4 - 3
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

+ 9 - 11
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

+ 2 - 10
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
 

+ 9 - 18
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

+ 0 - 56
lib/sessions/cache_in.rb

@@ -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

+ 0 - 44
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