Browse Source

Improved cache backend. Heavy reduce of sql queries.

Martin Edenhofer 10 years ago
parent
commit
be31823d89

+ 2 - 2
.gitignore

@@ -13,7 +13,7 @@
 # Ignore all logfiles and tempfiles.
 /log/*.log
 /tmp/websocket/*
-/tmp/cache/*
+/tmp/cache*
 /tmp/pids/*
 /public/assets/*.*
 /public/assets/app/*
@@ -40,4 +40,4 @@ Gemfile.lock
 db/schema.rb
 
 # Ignore Rubymine config
-/.idea
+/.idea

+ 62 - 0
app/models/application_model.rb

@@ -445,6 +445,68 @@ returns
 
 =begin
 
+activate latest change on create, update, touch and destroy
+
+class Model < ApplicationModel
+  latest_change_support
+end
+
+=end
+
+  def self.latest_change_support
+    after_create  :latest_change_set_from_observer
+    after_update  :latest_change_set_from_observer
+    after_touch   :latest_change_set_from_observer
+    after_destroy :latest_change_set_from_observer_destroy
+  end
+
+  def latest_change_set_from_observer
+    self.class.latest_change_set(self.updated_at)
+  end
+  def latest_change_set_from_observer_destroy
+    self.class.latest_change_set(nil)
+  end
+
+  def self.latest_change_set(updated_at)
+    key        = "#{self.new.class.name}_latest_change"
+    expires_in = 31_536_000 # 1 year
+
+    if updated_at == nil
+      Cache.delete( key )
+    else
+      Cache.write( key, updated_at, { :expires_in => expires_in } )
+    end
+  end
+
+=begin
+
+  get latest updated_at object timestamp
+
+  latest_change = Ticket.latest_change
+
+returns
+
+  result = timestamp
+
+=end
+
+  def self.latest_change
+    key        = "#{self.new.class.name}_latest_change"
+    updated_at = Cache.get( key )
+
+    # if we do not have it cached, do lookup
+    if !updated_at
+      o = self.select(:updated_at).order(updated_at: :desc).limit(1).first
+      if o
+        updated_at = o.updated_at
+        self.latest_change_set(updated_at)
+      end
+    end
+    updated_at
+  end
+
+=begin
+
 activate client notify support on create, update, touch and destroy
 
 class Model < ApplicationModel

+ 4 - 1
app/models/email_address.rb

@@ -4,4 +4,7 @@ class EmailAddress < ApplicationModel
   has_many                :groups,   :after_add => :cache_update, :after_remove => :cache_update
   validates               :realname, :presence => true
   validates               :email,    :presence => true
-end
+
+  latest_change_support
+
+end

+ 2 - 1
app/models/group.rb

@@ -8,4 +8,5 @@ class Group < ApplicationModel
 
   activity_stream_support  :role => Z_ROLENAME_ADMIN
   history_support
-end
+  latest_change_support
+end

+ 1 - 1
app/models/organization.rb

@@ -15,5 +15,5 @@ class Organization < ApplicationModel
   history_support
   search_index_support
   notify_clients_support
-
+  latest_change_support
 end

+ 2 - 1
app/models/role.rb

@@ -5,4 +5,5 @@ class Role < ApplicationModel
   has_and_belongs_to_many   :users, :after_add => :cache_update, :after_remove => :cache_update
   validates                 :name,  :presence => true
   activity_stream_support   :role => Z_ROLENAME_ADMIN
-end
+  latest_change_support
+end

+ 2 - 1
app/models/signature.rb

@@ -3,4 +3,5 @@
 class Signature < ApplicationModel
   has_many                :groups,  :after_add => :cache_update, :after_remove => :cache_update
   validates               :name,    :presence => true
-end
+  latest_change_support
+end

+ 2 - 0
app/models/ticket.rb

@@ -20,6 +20,8 @@ class Ticket < ApplicationModel
 
   notify_clients_support
 
+  latest_change_support
+
   activity_stream_support :ignore_attributes => {
     :create_article_type_id   => true,
     :create_article_sender_id => true,

+ 3 - 1
app/models/ticket/article.rb

@@ -40,9 +40,11 @@ class Ticket::Article < ApplicationModel
 
   class Sender < ApplicationModel
     validates   :name, :presence => true
+    latest_change_support
   end
 
   class Type < ApplicationModel
     validates   :name, :presence => true
+    latest_change_support
   end
-end
+end

+ 2 - 0
app/models/ticket/state.rb

@@ -4,6 +4,8 @@ class Ticket::State < ApplicationModel
   belongs_to    :state_type,        :class_name => 'Ticket::StateType'
   validates     :name, :presence => true
 
+  latest_change_support
+
 =begin
 
 list tickets by customer

Some files were not shown because too many files changed in this diff