Added model cache object (lookup method) to increase performance.

This commit is contained in:
Martin Edenhofer 2013-01-01 21:29:26 +01:00
parent 2b0b360ab1
commit 67db493699
24 changed files with 66 additions and 88 deletions

View file

@ -15,7 +15,7 @@ class LinksController < ApplicationController
links.each { |item| links.each { |item|
link_list.push item link_list.push item
if item['link_object'] == 'Ticket' if item['link_object'] == 'Ticket'
data = Ticket.full_data( item['link_object_value'] ) data = Ticket.lookup( :id => item['link_object_value'] )
tickets.push data tickets.push data
if !users[ data['owner_id'] ] if !users[ data['owner_id'] ]
users[ data['owner_id'] ] = User.user_data_full( data['owner_id'] ) users[ data['owner_id'] ] = User.user_data_full( data['owner_id'] )

View file

@ -50,7 +50,7 @@ class TicketOverviewsController < ApplicationController
users = {} users = {}
tickets = [] tickets = []
overview[:ticket_list].each {|ticket_id| overview[:ticket_list].each {|ticket_id|
data = Ticket.full_data(ticket_id) data = Ticket.lookup( :id => ticket_id )
tickets.push data tickets.push data
if !users[ data['owner_id'] ] if !users[ data['owner_id'] ]
users[ data['owner_id'] ] = User.user_data_full( data['owner_id'] ) users[ data['owner_id'] ] = User.user_data_full( data['owner_id'] )

View file

@ -225,7 +225,7 @@ class TicketsController < ApplicationController
users = {} users = {}
tickets = [] tickets = []
ticket_list.each {|ticket| ticket_list.each {|ticket|
data = Ticket.full_data(ticket.id) data = Ticket.lookup( :id => ticket.id )
tickets.push data tickets.push data
if !users[ data['owner_id'] ] if !users[ data['owner_id'] ]
users[ data['owner_id'] ] = User.user_data_full( data['owner_id'] ) users[ data['owner_id'] ] = User.user_data_full( data['owner_id'] )
@ -508,7 +508,7 @@ class TicketsController < ApplicationController
tickets = [] tickets = []
users = {} users = {}
tickets_all.each do |ticket| tickets_all.each do |ticket|
ticket_tmp = Ticket.full_data(ticket.id) ticket_tmp = Ticket.lookup( :id => ticket.id )
tickets.push ticket_tmp tickets.push ticket_tmp
users[ ticket['owner_id'] ] = User.user_data_full( ticket_tmp['owner_id'] ) users[ ticket['owner_id'] ] = User.user_data_full( ticket_tmp['owner_id'] )
users[ ticket['customer_id'] ] = User.user_data_full( ticket_tmp['customer_id'] ) users[ ticket['customer_id'] ] = User.user_data_full( ticket_tmp['customer_id'] )

View file

@ -3,6 +3,10 @@ require 'cache'
class ApplicationModel < ActiveRecord::Base class ApplicationModel < ActiveRecord::Base
self.abstract_class = true self.abstract_class = true
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
def self.param_cleanup(params) def self.param_cleanup(params)
data = {} data = {}
self.new.attributes.each {|item| self.new.attributes.each {|item|
@ -40,6 +44,28 @@ class ApplicationModel < ActiveRecord::Base
Cache.get( key.to_s ) Cache.get( key.to_s )
end end
def self.lookup(data)
if data[:id]
# puts "GET- + #{self.to_s}.#{data[:id].to_s}"
cache = self.cache_get( data[:id] )
return cache if cache
# puts "Fillup- + #{self.to_s}.#{data[:id].to_s}"
record = self.where( :id => data[:id] ).first
self.cache_set( data[:id], record )
return record
elsif data[:name]
cache = self.cache_get( data[:name] )
return cache if cache
record = self.where( :name => data[:name] ).first
self.cache_set( data[:name], record )
return record
else
raise "Need name or id for lookup()"
end
end
def self.create_if_not_exists(data) def self.create_if_not_exists(data)
if data[:name] if data[:name]
record = self.where( :name => data[:name] ).first record = self.where( :name => data[:name] ).first

View file

@ -3,10 +3,6 @@ class Authorization < ApplicationModel
validates_presence_of :user_id, :uid, :provider validates_presence_of :user_id, :uid, :provider
validates_uniqueness_of :uid, :scope => :provider validates_uniqueness_of :uid, :scope => :provider
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
def self.find_from_hash(hash) def self.find_from_hash(hash)
auth = Authorization.where( :provider => hash['provider'], :uid => hash['uid'] ).first auth = Authorization.where( :provider => hash['provider'], :uid => hash['uid'] ).first
if auth if auth

View file

@ -1,8 +1,5 @@
class EmailAddress < ApplicationModel class EmailAddress < ApplicationModel
has_many :groups, :after_add => :cache_update, :after_remove => :cache_update has_many :groups, :after_add => :cache_update, :after_remove => :cache_update
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
validates :realname, :presence => true validates :realname, :presence => true
validates :email, :presence => true validates :email, :presence => true
end end

View file

@ -2,8 +2,5 @@ class Group < ApplicationModel
has_and_belongs_to_many :users, :after_add => :cache_update, :after_remove => :cache_update has_and_belongs_to_many :users, :after_add => :cache_update, :after_remove => :cache_update
belongs_to :email_address belongs_to :email_address
belongs_to :signature belongs_to :signature
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
validates :name, :presence => true validates :name, :presence => true
end end

View file

@ -1,4 +1,4 @@
class History < ActiveRecord::Base class History < ApplicationModel
self.table_name = 'histories' self.table_name = 'histories'
belongs_to :history_type, :class_name => 'History::Type' belongs_to :history_type, :class_name => 'History::Type'
belongs_to :history_object, :class_name => 'History::Object' belongs_to :history_object, :class_name => 'History::Object'
@ -349,13 +349,13 @@ class History < ActiveRecord::Base
return history_attribute return history_attribute
end end
class Object < ActiveRecord::Base class Object < ApplicationModel
end end
class Type < ActiveRecord::Base class Type < ApplicationModel
end end
class Attribute < ActiveRecord::Base class Attribute < ApplicationModel
end end
end end

View file

@ -7,16 +7,16 @@ class Observer::Ticket::Article::CommunicateEmail < ActiveRecord::Observer
return if Setting.get('import_mode') return if Setting.get('import_mode')
# if sender is customer, do not communication # if sender is customer, do not communication
sender = Ticket::Article::Sender.where( :id => record.ticket_article_sender_id ).first sender = Ticket::Article::Sender.lookup( :id => record.ticket_article_sender_id )
return 1 if sender == nil return 1 if sender == nil
return 1 if sender['name'] == 'Customer' return 1 if sender['name'] == 'Customer'
# only apply on emails # only apply on emails
type = Ticket::Article::Type.where( :id => record.ticket_article_type_id ).first type = Ticket::Article::Type.lookup( :id => record.ticket_article_type_id )
return if type['name'] != 'email' return if type['name'] != 'email'
# build subject # build subject
ticket = Ticket.find(record.ticket_id) ticket = Ticket.lookup( record.ticket_id )
subject = ticket.subject_build( record.subject ) subject = ticket.subject_build( record.subject )
# send email # send email

View file

@ -7,12 +7,12 @@ class Observer::Ticket::Article::CommunicateFacebook < ActiveRecord::Observer
return if Setting.get('import_mode') return if Setting.get('import_mode')
# if sender is customer, do not communication # if sender is customer, do not communication
sender = Ticket::Article::Sender.where( :id => record.ticket_article_sender_id ).first sender = Ticket::Article::Sender.lookup( :id => record.ticket_article_sender_id )
return 1 if sender == nil return 1 if sender == nil
return 1 if sender['name'] == 'Customer' return 1 if sender['name'] == 'Customer'
# only apply on emails # only apply on emails
type = Ticket::Article::Type.where( :id => record.ticket_article_type_id ).first type = Ticket::Article::Type.lookup( :id => record.ticket_article_type_id )
return if type['name'] != 'facebook' return if type['name'] != 'facebook'
a = Channel::Facebook.new a = Channel::Facebook.new

View file

@ -7,12 +7,12 @@ class Observer::Ticket::Article::CommunicateTwitter < ActiveRecord::Observer
return if Setting.get('import_mode') return if Setting.get('import_mode')
# if sender is customer, do not communication # if sender is customer, do not communication
sender = Ticket::Article::Sender.where( :id => record.ticket_article_sender_id ).first sender = Ticket::Article::Sender.lookup( :id => record.ticket_article_sender_id )
return 1 if sender == nil return 1 if sender == nil
return 1 if sender['name'] == 'Customer' return 1 if sender['name'] == 'Customer'
# only apply on tweets # only apply on tweets
type = Ticket::Article::Type.where( :id => record.ticket_article_type_id ).first type = Ticket::Article::Type.lookup( :id => record.ticket_article_type_id )
return if type['name'] != 'twitter direct-message' && type['name'] != 'twitter status' return if type['name'] != 'twitter direct-message' && type['name'] != 'twitter status'
a = Channel::Twitter2.new a = Channel::Twitter2.new

View file

@ -7,16 +7,16 @@ class Observer::Ticket::Article::FillupFromEmail < ActiveRecord::Observer
return if Setting.get('import_mode') return if Setting.get('import_mode')
# if sender is customer, do not change anything # if sender is customer, do not change anything
sender = Ticket::Article::Sender.where( :id => record.ticket_article_sender_id ).first sender = Ticket::Article::Sender.lookup( :id => record.ticket_article_sender_id )
return if sender == nil return if sender == nil
return if sender['name'] == 'Customer' return if sender['name'] == 'Customer'
# set email attributes # set email attributes
type = Ticket::Article::Type.where( :id => record.ticket_article_type_id ).first type = Ticket::Article::Type.lookup( :id => record.ticket_article_type_id )
return if type['name'] != 'email' return if type['name'] != 'email'
# set subject if empty # set subject if empty
ticket = Ticket.find( record.ticket_id ) ticket = Ticket.lookup( :id => record.ticket_id )
if !record.subject || record.subject == '' if !record.subject || record.subject == ''
record.subject = ticket.title record.subject = ticket.title
end end

View file

@ -7,7 +7,7 @@ class Observer::Ticket::Article::FillupFromGeneral < ActiveRecord::Observer
return if Setting.get('import_mode') return if Setting.get('import_mode')
# if sender is customer, do not change anything # if sender is customer, do not change anything
sender = Ticket::Article::Sender.where( :id => record.ticket_article_sender_id ).first sender = Ticket::Article::Sender.lookup( :id => record.ticket_article_sender_id )
return if sender == nil return if sender == nil
return if sender['name'] == 'Customer' return if sender['name'] == 'Customer'

View file

@ -20,8 +20,8 @@ class Observer::Ticket::CloseTime < ActiveRecord::Observer
return true if record.close_time return true if record.close_time
# check if ticket is closed now # check if ticket is closed now
ticket_state = Ticket::State.find( record.ticket_state_id ) ticket_state = Ticket::State.lookup( :id => record.ticket_state_id )
ticket_state_type = Ticket::StateType.find( ticket_state.ticket_state_type_id ) ticket_state_type = Ticket::StateType.lookup( :id => ticket_state.ticket_state_type_id )
return true if ticket_state_type.name != 'closed' return true if ticket_state_type.name != 'closed'
# set close_time # set close_time

View file

@ -11,10 +11,10 @@ class Observer::Ticket::FirstResponse < ActiveRecord::Observer
return true if record.internal return true if record.internal
# if sender is not agent # if sender is not agent
return true if record.ticket_article_sender.name != 'Agent' return true if Ticket::Article::Sender.lookup( :id => record.ticket_article_sender_id ).name != 'Agent'
# if article is a message to customer # if article is a message to customer
return true if !record.ticket_article_type.communication return true if !Ticket::Article::Type.lookup( :id => record.ticket_article_type_id ).communication
# check if first_response is already set # check if first_response is already set
return true if record.ticket.first_response return true if record.ticket.first_response

View file

@ -8,10 +8,11 @@ class Observer::Ticket::LastContact < ActiveRecord::Observer
return true if record.internal return true if record.internal
# if article is a message to customer # if article is a message to customer
return true if !record.ticket_article_type.communication return true if !Ticket::Article::Type.lookup( :id => record.ticket_article_type_id ).communication
# if sender is not customer # if sender is not customer
if record.ticket_article_sender.name == 'Customer' sender = Ticket::Article::Sender.lookup( :id => record.ticket_article_sender_id )
if sender.name == 'Customer'
# check if last communication is done by agent, else do not set last_contact_customer # check if last communication is done by agent, else do not set last_contact_customer
if record.ticket.last_contact_customer == nil || if record.ticket.last_contact_customer == nil ||
@ -28,7 +29,7 @@ class Observer::Ticket::LastContact < ActiveRecord::Observer
end end
# if sender is not agent # if sender is not agent
if record.ticket_article_sender.name == 'Agent' if sender.name == 'Agent'
# set last_contact_agent # set last_contact_agent
record.ticket.last_contact_agent = Time.now record.ticket.last_contact_agent = Time.now

View file

@ -16,10 +16,10 @@ class Observer::Ticket::Notification < ActiveRecord::Observer
# get current state of objects # get current state of objects
if event[:name] == 'Ticket::Article' if event[:name] == 'Ticket::Article'
article = Ticket::Article.find( event[:id] ) article = Ticket::Article.lookup( :id => event[:id] )
ticket = article.ticket ticket = article.ticket
else else
ticket = Ticket.find( event[:id] ) ticket = Ticket.lookup( :id => event[:id] )
article = ticket.articles[-1] article = ticket.articles[-1]
end end

View file

@ -1,7 +1,4 @@
class Role < ApplicationModel class Role < ApplicationModel
has_and_belongs_to_many :users, :after_add => :cache_update, :after_remove => :cache_update has_and_belongs_to_many :users, :after_add => :cache_update, :after_remove => :cache_update
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
validates :name, :presence => true validates :name, :presence => true
end end

View file

@ -3,9 +3,6 @@ class Setting < ApplicationModel
store :state store :state
store :state_initial store :state_initial
before_create :set_initial before_create :set_initial
after_create :delete_cache
after_update :delete_cache
after_destroy :delete_cache
def self.load def self.load

View file

@ -1,7 +1,4 @@
class Signature < ApplicationModel class Signature < ApplicationModel
has_many :groups, :after_add => :cache_update, :after_remove => :cache_update has_many :groups, :after_add => :cache_update, :after_remove => :cache_update
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
validates :name, :presence => true validates :name, :presence => true
end end

View file

@ -14,10 +14,6 @@ class Ticket < ApplicationModel
belongs_to :create_article_type, :class_name => 'Ticket::Article::Type' belongs_to :create_article_type, :class_name => 'Ticket::Article::Type'
belongs_to :create_article_sender, :class_name => 'Ticket::Article::Sender' belongs_to :create_article_sender, :class_name => 'Ticket::Article::Sender'
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
def self.number_check (string) def self.number_check (string)
self.number_adapter.number_check_item(string) self.number_adapter.number_check_item(string)
end end
@ -41,8 +37,8 @@ class Ticket < ApplicationModel
Ticket::Article.create( Ticket::Article.create(
:created_by_id => data[:created_by_id], :created_by_id => data[:created_by_id],
:ticket_id => self.id, :ticket_id => self.id,
:ticket_article_type_id => Ticket::Article::Type.where( :name => 'note' ).first.id, :ticket_article_type_id => Ticket::Article::Type.lookup( :name => 'note' ).id,
:ticket_article_sender_id => Ticket::Article::Sender.where( :name => 'Agent' ).first.id, :ticket_article_sender_id => Ticket::Article::Sender.lookup( :name => 'Agent' ).id,
:body => 'merged', :body => 'merged',
:internal => false :internal => false
) )
@ -59,7 +55,7 @@ class Ticket < ApplicationModel
) )
# set state to 'merged' # set state to 'merged'
self.ticket_state_id = Ticket::State.where( :name => 'merged' ).first.id self.ticket_state_id = Ticket::State.lookup( :name => 'merged' ).id
# rest owner # rest owner
self.owner_id = User.where( :login => '-' ).first.id self.owner_id = User.where( :login => '-' ).first.id
@ -304,16 +300,6 @@ class Ticket < ApplicationModel
end end
# data = Ticket.full_data(123)
def self.full_data(ticket_id)
cache = self.cache_get(ticket_id)
return cache if cache
ticket = Ticket.find(ticket_id).attributes
self.cache_set( ticket_id, ticket )
return ticket
end
# Ticket.create_attributes( # Ticket.create_attributes(
# :current_user_id => 123, # :current_user_id => 123,
# ) # )
@ -428,24 +414,15 @@ class Ticket < ApplicationModel
class Priority < ApplicationModel class Priority < ApplicationModel
self.table_name = 'ticket_priorities' self.table_name = 'ticket_priorities'
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
validates :name, :presence => true validates :name, :presence => true
end end
class StateType < ApplicationModel class StateType < ApplicationModel
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
validates :name, :presence => true validates :name, :presence => true
end end
class State < ApplicationModel class State < ApplicationModel
belongs_to :ticket_state_type, :class_name => 'Ticket::StateType' belongs_to :ticket_state_type, :class_name => 'Ticket::StateType'
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
validates :name, :presence => true validates :name, :presence => true
end end
end end

View file

@ -5,10 +5,6 @@ class Ticket::Article < ApplicationModel
belongs_to :ticket_article_sender, :class_name => 'Ticket::Article::Sender' belongs_to :ticket_article_sender, :class_name => 'Ticket::Article::Sender'
belongs_to :created_by, :class_name => 'User' belongs_to :created_by, :class_name => 'User'
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
private private
def attachment_check def attachment_check

View file

@ -3,9 +3,6 @@ class User < ApplicationModel
before_create :check_name, :check_email, :check_login, :check_image, :check_geo before_create :check_name, :check_email, :check_login, :check_image, :check_geo
before_update :check_password, :check_image, :check_geo, :check_email, :check_login before_update :check_password, :check_image, :check_geo, :check_email, :check_login
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
has_and_belongs_to_many :groups, :after_add => :cache_update, :after_remove => :cache_update has_and_belongs_to_many :groups, :after_add => :cache_update, :after_remove => :cache_update
has_and_belongs_to_many :roles, :after_add => :cache_update, :after_remove => :cache_update has_and_belongs_to_many :roles, :after_add => :cache_update, :after_remove => :cache_update

View file

@ -434,7 +434,7 @@ class ClientState
return if !session_data return if !session_data
return if !session_data[:user] return if !session_data[:user]
return if !session_data[:user][:id] return if !session_data[:user][:id]
user = User.find( session_data[:user][:id] ) user = User.lookup( :id => session_data[:user][:id] )
return if !user return if !user
# set cache key # set cache key
@ -691,7 +691,7 @@ class ClientState
if !@pushed[:tickets] if !@pushed[:tickets]
@pushed[:tickets] = {} @pushed[:tickets] = {}
end end
ticket = Ticket.full_data(ticket_id) ticket = Ticket.lookup( :id => ticket_id )
if @pushed[:tickets][ticket_id] != ticket['updated_at'] if @pushed[:tickets][ticket_id] != ticket['updated_at']
@pushed[:tickets][ticket_id] = ticket['updated_at'] @pushed[:tickets][ticket_id] = ticket['updated_at']
tickets.push ticket tickets.push ticket