From fb910d03a390ea4b22fe46133ec3378c07e2acb8 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 29 Jul 2012 20:55:51 +0200 Subject: [PATCH] Improved caching. --- app/controllers/application_controller.rb | 15 ++++------ app/controllers/sessions_controller.rb | 2 +- .../ticket_overviews_controller.rb | 30 +++++++++---------- app/models/authorization.rb | 2 +- app/models/group.rb | 2 +- app/models/role.rb | 2 +- app/models/translation.rb | 2 +- config/initializers/cache.rb | 5 ---- lib/cache.rb | 6 +++- 9 files changed, 30 insertions(+), 36 deletions(-) delete mode 100644 config/initializers/cache.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 47111007c..286cc1b52 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -18,11 +18,11 @@ class ApplicationController < ActionController::Base headers['Access-Control-Allow-Headers'] = 'Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control' headers['Access-Control-Allow-Credentials'] = 'true' end - + # If this is a preflight OPTIONS request, then short-circuit the # request, return only the necessary headers and return an empty # text/plain. - + def cors_preflight_check if request.method == 'OPTIONS' headers['Access-Control-Allow-Origin'] = '*' @@ -34,8 +34,7 @@ class ApplicationController < ActionController::Base return false end end - - + private # execute events @@ -81,7 +80,7 @@ class ApplicationController < ActionController::Base current_user_set(userdata) return true end - + # return auth not ok render( :json => { @@ -141,7 +140,7 @@ class ApplicationController < ActionController::Base :name => object.class.name ) end - + History.create( :o_id => object.id, :history_type_id => history_type.id, @@ -160,8 +159,4 @@ class ApplicationController < ActionController::Base return config end - def user_data_full (user_id) - user = User.user_data_full(user_id) - end - end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 5e2d78039..e203d8252 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -75,7 +75,7 @@ class SessionsController < ApplicationController # Save the user ID in the session so it can be used in # subsequent requests - user = user_data_full( user_id ) + user = User.user_data_full( user_id ) # auto population of default collections default_collection = default_collections() diff --git a/app/controllers/ticket_overviews_controller.rb b/app/controllers/ticket_overviews_controller.rb index c72d6a1fa..6a8c59921 100644 --- a/app/controllers/ticket_overviews_controller.rb +++ b/app/controllers/ticket_overviews_controller.rb @@ -48,13 +48,13 @@ class TicketOverviewsController < ApplicationController overview[:tickets].each {|ticket| tickets.push ticket.attributes if !users[ ticket.owner_id ] - users[ ticket.owner_id ] = user_data_full( ticket.owner_id ) + users[ ticket.owner_id ] = User.user_data_full( ticket.owner_id ) end if !users[ ticket.customer_id ] - users[ ticket.customer_id ] = user_data_full( ticket.customer_id ) + users[ ticket.customer_id ] = User.user_data_full( ticket.customer_id ) end if !users[ ticket.created_by_id ] - users[ ticket.created_by_id ] = user_data_full( ticket.created_by_id ) + users[ ticket.created_by_id ] = User.user_data_full( ticket.created_by_id ) end } @@ -64,7 +64,7 @@ class TicketOverviewsController < ApplicationController bulk_owners.each { |user| bulk_owner_ids.push user.id if !users[ user.id ] - users[ user.id ] = user_data_full( user.id ) + users[ user.id ] = User.user_data_full( user.id ) end } @@ -100,20 +100,20 @@ class TicketOverviewsController < ApplicationController # get related users users = {} if !users[ticket.owner_id] - users[ticket.owner_id] = user_data_full(ticket.owner_id) + users[ticket.owner_id] = User.user_data_full(ticket.owner_id) end if !users[ticket.customer_id] - users[ticket.customer_id] = user_data_full(ticket.customer_id) + users[ticket.customer_id] = User.user_data_full(ticket.customer_id) end if !users[ticket.created_by_id] - users[ticket.created_by_id] = user_data_full(ticket.created_by_id) + users[ticket.created_by_id] = User.user_data_full(ticket.created_by_id) end owner_ids = [] ticket.agent_of_group.each { |user| owner_ids.push user.id if !users[user.id] - users[user.id] = user_data_full(user.id) + users[user.id] = User.user_data_full(user.id) end } @@ -127,7 +127,7 @@ class TicketOverviewsController < ApplicationController # load users if !users[article.created_by_id] - users[article.created_by_id] = user_data_full(article.created_by_id) + users[article.created_by_id] = User.user_data_full(article.created_by_id) end end @@ -160,20 +160,20 @@ class TicketOverviewsController < ApplicationController # get related users users = {} if !users[ticket.owner_id] - users[ticket.owner_id] = user_data_full(ticket.owner_id) + users[ticket.owner_id] = User.user_data_full(ticket.owner_id) end if !users[ticket.customer_id] - users[ticket.customer_id] = user_data_full(ticket.customer_id) + users[ticket.customer_id] = User.user_data_full(ticket.customer_id) end if !users[ticket.created_by_id] - users[ticket.created_by_id] = user_data_full(ticket.created_by_id) + users[ticket.created_by_id] = User.user_data_full(ticket.created_by_id) end owner_ids = [] ticket.agent_of_group.each { |user| owner_ids.push user.id if !users[user.id] - users[user.id] = user_data_full(user.id) + users[user.id] = User.user_data_full(user.id) end } @@ -192,7 +192,7 @@ class TicketOverviewsController < ApplicationController # load users if !users[article.created_by_id] - users[article.created_by_id] = user_data_full(article.created_by_id) + users[article.created_by_id] = User.user_data_full(article.created_by_id) end } @@ -349,7 +349,7 @@ class TicketOverviewsController < ApplicationController # get related users users = {} history.each do |item| - users[ item['created_by_id'] ] = user_data_full( item['created_by_id'] ) + users[ item['created_by_id'] ] = User.user_data_full( item['created_by_id'] ) if item['history_object'] == 'Ticket::Article' item['type'] = 'Article ' + item['type'].to_s else diff --git a/app/models/authorization.rb b/app/models/authorization.rb index 309b382a2..50e3ae498 100644 --- a/app/models/authorization.rb +++ b/app/models/authorization.rb @@ -1,5 +1,5 @@ class Authorization < ApplicationModel - belongs_to :user + belongs_to :user, :after_add => :cache_update, :after_remove => :cache_update validates_presence_of :user_id, :uid, :provider validates_uniqueness_of :uid, :scope => :provider diff --git a/app/models/group.rb b/app/models/group.rb index 4c46900af..ef370afb7 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,5 +1,5 @@ class Group < ApplicationModel - has_and_belongs_to_many :users + 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 diff --git a/app/models/role.rb b/app/models/role.rb index 4248f70be..39bd3cd25 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -1,5 +1,5 @@ class Role < ApplicationModel - has_and_belongs_to_many :users + 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 diff --git a/app/models/translation.rb b/app/models/translation.rb index 432a62f1f..0f007fe8c 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -1,4 +1,4 @@ -class Translation < ApplicationModel +class Translation < ActiveRecord::Base before_create :set_initial private diff --git a/config/initializers/cache.rb b/config/initializers/cache.rb deleted file mode 100644 index 672af8cff..000000000 --- a/config/initializers/cache.rb +++ /dev/null @@ -1,5 +0,0 @@ -# clear cache -if Zammad::Application.config.cache_store[1] && File.directory?(Zammad::Application.config.cache_store[1]) - puts 'clear cache...' - Rails.cache.clear -end \ No newline at end of file diff --git a/lib/cache.rb b/lib/cache.rb index ed644ec09..dae7f9c34 100644 --- a/lib/cache.rb +++ b/lib/cache.rb @@ -5,7 +5,7 @@ module Cache end def self.write( key, data, params = {} ) if !params[:expires_in] - params[:expires_in] = 48.hours + params[:expires_in] = 24.hours end puts 'Cache.write: ' + key.to_s Rails.cache.write( key.to_s, data, params) @@ -14,4 +14,8 @@ module Cache puts 'Cache.get: ' + key.to_s Rails.cache.read( key.to_s ) end + def self.clear + puts 'Cache.clear...' + Rails.cache.clear + end end \ No newline at end of file