From 6047e63666f9cbca835b4ffe8a7e0f7121b6a546 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sat, 14 Apr 2012 17:53:00 +0200 Subject: [PATCH] Added in memory cache for user object because of performance reasons (count of SQLs). --- app/controllers/application_controller.rb | 19 ++-------- app/models/user.rb | 44 ++++++++++++++++++++--- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9d474ff1c..09ef51fbc 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -139,21 +139,11 @@ class ApplicationController < ActionController::Base def user_data_full (user_id) # get user - user = User.find(user_id) - - # get linked accounts - user['accounts'] = {} - authorizations = user.authorizations() || [] - authorizations.each do | authorization | - user['accounts'][authorization.provider] = { - :uid => authorization[:uid], - :username => authorization[:username] - } - end + user = User.find_fulldata(user_id) # do not show password user['password'] = '' - + # show linked topics and items user['links'] = [] ticket_state_list_open = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['new','open', 'pending remidner', 'pending action']) ) @@ -183,11 +173,6 @@ class ApplicationController < ActionController::Base } user['links'].push topic - # set roles - user['roles'] = user.roles.select('id, name').where(:active => true) - user['groups'] = user.groups.select('id, name').where(:active => true) - user['organization'] = user.organization - user['organizations'] = user.organizations.select('id, name').where(:active => true) return user end diff --git a/app/models/user.rb b/app/models/user.rb index 9a2f92d06..92d0f6522 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,12 +1,17 @@ class User < ActiveRecord::Base - before_create :check_name, :check_email, :check_image + before_create :check_name, :check_email, :check_image has_and_belongs_to_many :groups has_and_belongs_to_many :roles has_and_belongs_to_many :organizations - belongs_to :organization, :class_name => 'Organization' - has_many :authorizations + belongs_to :organization, :class_name => 'Organization' + has_many :authorizations + after_create :delete_cache + after_update :delete_cache + after_destroy :delete_cache - def self.authenticate(username, password) + @@cache = {} + + def self.authenticate( username, password ) user = User.where( :login => username, :active => true ).first return nil if user.nil? logger.debug 'auth' @@ -47,8 +52,39 @@ class User < ActiveRecord::Base end + def self.find_fulldata(user_id) + + return @@cache[user_id] if @@cache[user_id] + + # get user + user = User.find(user_id) + + # get linked accounts + user['accounts'] = {} + authorizations = user.authorizations() || [] + authorizations.each do | authorization | + user['accounts'][authorization.provider] = { + :uid => authorization[:uid], + :username => authorization[:username] + } + end + + # set roles + user['roles'] = user.roles.select('id, name').where(:active => true) + user['groups'] = user.groups.select('id, name').where(:active => true) + user['organization'] = user.organization + user['organizations'] = user.organizations.select('id, name').where(:active => true) + + @@cache[user_id] = user + + return user + end private + def delete_cache + @@cache[self.id] = nil + end + def check_name if self.firstname && (!self.lastname || self.lastname == '') then name = self.firstname.split(' ', 2)