Improved caching - Facebook style.

This commit is contained in:
Martin Edenhofer 2012-04-16 10:04:49 +02:00
parent f76af2d2b6
commit 1b3066a8b7
6 changed files with 85 additions and 39 deletions

View file

@ -146,25 +146,23 @@ class ApplicationController < ActionController::Base
# show linked topics and items # show linked topics and items
user['links'] = [] user['links'] = []
ticket_state_list_open = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['new','open', 'pending remidner', 'pending action']) )
ticket_state_list_closed = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['closed'] ) )
tickets_open = Ticket.where(:customer_id => user_id, :ticket_state_id => ticket_state_list_open).count() # TEMP: compat. reasons
tickets_closed = Ticket.where(:customer_id => user_id, :ticket_state_id => ticket_state_list_closed).count() user[:preferences] = {} if !user[:preferences]
topic = { topic = {
:title => 'Tickets', :title => 'Tickets',
:items => [ :items => [
{ {
:url => '', :url => '',
:name => 'open (' + tickets_open.to_s + ')', :name => 'open (' + user[:preferences][:tickets_open].to_s + ')',
:title => 'Open Tickets', :title => 'Open Tickets',
:class => 'user-tickets', :class => 'user-tickets',
:data => 'open' :data => 'open'
}, },
{ {
:url => '', :url => '',
:name => 'closed (' + tickets_closed.to_s + ')', :name => 'closed (' + user[:preferences][:tickets_closed].to_s + ')',
:title => 'Closed Tickets', :title => 'Closed Tickets',
:class => 'user-tickets', :class => 'user-tickets',
:data => 'closed' :data => 'closed'

View file

@ -0,0 +1,31 @@
class ApplicationModel < ActiveRecord::Base
self.abstract_class = true
@@cache = {}
def cache_update(o)
# puts 'u ' + self.class.to_s
if self.respond_to?('cache_delete') then self.cache_delete end
# puts 'g ' + group.class.to_s
if o.respond_to?('cache_delete') then o.cache_delete end
end
def cache_delete
# puts 'cache_delete', self.inspect
# puts 'cache_delete', self.id
@@cache[self.to_s] = {} if !@@cache[self.to_s]
@@cache[self.to_s][self.id] = nil
end
def self.cache_set(data_id, data)
# puts 'cache_set', self.inspect
# puts 'cache_set', self.to_s
# puts 'cache_set', data_id
@@cache[self.to_s] = {} if !@@cache[self.to_s]
@@cache[self.to_s][data_id] = data
end
def self.cache_get(data_id)
# puts 'cache_get', data_id
# puts 'cache_get', self.inspect
@@cache[self.to_s] = {} if !@@cache[self.to_s]
return @@cache[self.to_s][data_id] if @@cache[self.to_s]
end
end

View file

@ -1,7 +1,7 @@
class Authorization < ActiveRecord::Base class Authorization < ActiveRecord::Base
belongs_to :user belongs_to :user
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
def self.find_from_hash(hash) def self.find_from_hash(hash)
auth = Authorization.where( :provider => hash['provider'], :uid => hash['uid'] ) auth = Authorization.where( :provider => hash['provider'], :uid => hash['uid'] )
@ -19,8 +19,11 @@ class Authorization < ActiveRecord::Base
if hash['info']['image'] if hash['info']['image']
user = User.where( :id => auth.first.user_id ).first user = User.where( :id => auth.first.user_id ).first
user.update_attributes( user.update_attributes(
:image => hash['info']['image'] :image => hash['info']['image']
) )
# reset cache
user.cache_delete
end end
end end
@ -44,6 +47,8 @@ class Authorization < ActiveRecord::Base
:token => hash['credentials']['token'], :token => hash['credentials']['token'],
:secret => hash['credentials']['secret'] :secret => hash['credentials']['secret']
) )
end
# reset cache
user.cache_delete
end
end end

View file

@ -1,12 +1,16 @@
class Ticket < ActiveRecord::Base class Ticket < ActiveRecord::Base
before_create :number_generate, :check_defaults before_create :number_generate, :check_defaults
belongs_to :group after_create :user_ticket_counter_update
has_many :articles after_update :user_ticket_counter_update
belongs_to :ticket_state, :class_name => 'Ticket::State' after_destroy :user_ticket_counter_update
belongs_to :ticket_priority, :class_name => 'Ticket::Priority'
belongs_to :owner, :class_name => 'User' belongs_to :group
belongs_to :customer, :class_name => 'User' has_many :articles
belongs_to :created_by, :class_name => 'User' belongs_to :ticket_state, :class_name => 'Ticket::State'
belongs_to :ticket_priority, :class_name => 'Ticket::Priority'
belongs_to :owner, :class_name => 'User'
belongs_to :customer, :class_name => 'User'
belongs_to :created_by, :class_name => 'User'
@@number_adapter = nil @@number_adapter = nil
@ -90,6 +94,21 @@ class Ticket < ActiveRecord::Base
return subject return subject
end end
def user_ticket_counter_update
return if !self.customer_id
ticket_state_list_open = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['new','open', 'pending remidner', 'pending action']) )
ticket_state_list_closed = Ticket::State.where( :ticket_state_type_id => Ticket::StateType.where(:name => ['closed'] ) )
tickets_open = Ticket.where( :customer_id => self.customer_id, :ticket_state_id => ticket_state_list_open ).count()
tickets_closed = Ticket.where( :customer_id => self.customer_id, :ticket_state_id => ticket_state_list_closed ).count()
customer = User.find( self.customer_id )
customer[:preferences][:tickets_open] = tickets_open
customer[:preferences][:tickets_closed] = tickets_closed
customer.save
end
private private
def number_generate def number_generate
Ticket.new.number_adapter = Setting.get('ticket_number') Ticket.new.number_adapter = Setting.get('ticket_number')

View file

@ -1,15 +1,16 @@
class User < ActiveRecord::Base class User < ApplicationModel
before_create :check_name, :check_email, :check_image before_create :check_name, :check_email, :check_image
has_and_belongs_to_many :groups after_create :cache_delete
has_and_belongs_to_many :roles after_update :cache_delete
has_and_belongs_to_many :organizations after_destroy :cache_delete
belongs_to :organization, :class_name => 'Organization'
has_many :authorizations
after_create :delete_cache
after_update :delete_cache
after_destroy :delete_cache
@@cache = {} 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 :organizations, :after_add => :cache_update, :after_remove => :cache_update
has_many :authorizations, :after_add => :cache_update, :after_remove => :cache_update
belongs_to :organization, :class_name => 'Organization'
store :preferences
def self.authenticate( username, password ) def self.authenticate( username, password )
user = User.where( :login => username, :active => true ).first user = User.where( :login => username, :active => true ).first
@ -54,7 +55,7 @@ class User < ActiveRecord::Base
def self.find_fulldata(user_id) def self.find_fulldata(user_id)
return @@cache[user_id] if @@cache[user_id] return cache_get(user_id) if cache_get(user_id)
# get user # get user
user = User.find(user_id) user = User.find(user_id)
@ -93,21 +94,12 @@ class User < ActiveRecord::Base
} }
data['organizations'] = organizations data['organizations'] = organizations
@@cache[user_id] = data cache_set(user.id, data)
return data return data
end end
def cache_reset
@@cache[self.id] = nil
end
private private
def delete_cache
puts 'delete_cache', self.insepct
@@cache[self.id] = nil
end
def check_name def check_name
if self.firstname && (!self.lastname || self.lastname == '') then if self.firstname && (!self.lastname || self.lastname == '') then
name = self.firstname.split(' ', 2) name = self.firstname.split(' ', 2)

View file

@ -29,6 +29,7 @@ class CreateBase < ActiveRecord::Migration
t.column :active, :boolean, :null => false, :default => true t.column :active, :boolean, :null => false, :default => true
t.column :note, :string, :limit => 250, :null => true t.column :note, :string, :limit => 250, :null => true
t.column :source, :string, :limit => 200, :null => true t.column :source, :string, :limit => 200, :null => true
t.column :preferences, :string, :limit => 4000,:null => true
t.column :created_by_id, :integer, :null => false t.column :created_by_id, :integer, :null => false
t.timestamps t.timestamps
end end