Improved caching - Facebook style.
This commit is contained in:
parent
f76af2d2b6
commit
1b3066a8b7
6 changed files with 85 additions and 39 deletions
|
@ -146,25 +146,23 @@ class ApplicationController < ActionController::Base
|
|||
|
||||
# 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']) )
|
||||
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()
|
||||
tickets_closed = Ticket.where(:customer_id => user_id, :ticket_state_id => ticket_state_list_closed).count()
|
||||
# TEMP: compat. reasons
|
||||
user[:preferences] = {} if !user[:preferences]
|
||||
|
||||
topic = {
|
||||
:title => 'Tickets',
|
||||
:items => [
|
||||
{
|
||||
:url => '',
|
||||
:name => 'open (' + tickets_open.to_s + ')',
|
||||
:name => 'open (' + user[:preferences][:tickets_open].to_s + ')',
|
||||
:title => 'Open Tickets',
|
||||
:class => 'user-tickets',
|
||||
:data => 'open'
|
||||
},
|
||||
{
|
||||
:url => '',
|
||||
:name => 'closed (' + tickets_closed.to_s + ')',
|
||||
:name => 'closed (' + user[:preferences][:tickets_closed].to_s + ')',
|
||||
:title => 'Closed Tickets',
|
||||
:class => 'user-tickets',
|
||||
:data => 'closed'
|
||||
|
|
31
app/models/application_model.rb
Normal file
31
app/models/application_model.rb
Normal 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
|
|
@ -1,7 +1,7 @@
|
|||
class Authorization < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
validates_presence_of :user_id, :uid, :provider
|
||||
validates_uniqueness_of :uid, :scope => :provider
|
||||
belongs_to :user
|
||||
validates_presence_of :user_id, :uid, :provider
|
||||
validates_uniqueness_of :uid, :scope => :provider
|
||||
|
||||
def self.find_from_hash(hash)
|
||||
auth = Authorization.where( :provider => hash['provider'], :uid => hash['uid'] )
|
||||
|
@ -19,8 +19,11 @@ class Authorization < ActiveRecord::Base
|
|||
if hash['info']['image']
|
||||
user = User.where( :id => auth.first.user_id ).first
|
||||
user.update_attributes(
|
||||
:image => hash['info']['image']
|
||||
:image => hash['info']['image']
|
||||
)
|
||||
|
||||
# reset cache
|
||||
user.cache_delete
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -44,6 +47,8 @@ class Authorization < ActiveRecord::Base
|
|||
:token => hash['credentials']['token'],
|
||||
:secret => hash['credentials']['secret']
|
||||
)
|
||||
end
|
||||
|
||||
# reset cache
|
||||
user.cache_delete
|
||||
end
|
||||
end
|
|
@ -1,12 +1,16 @@
|
|||
class Ticket < ActiveRecord::Base
|
||||
before_create :number_generate, :check_defaults
|
||||
belongs_to :group
|
||||
has_many :articles
|
||||
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'
|
||||
after_create :user_ticket_counter_update
|
||||
after_update :user_ticket_counter_update
|
||||
after_destroy :user_ticket_counter_update
|
||||
|
||||
belongs_to :group
|
||||
has_many :articles
|
||||
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
|
||||
|
||||
|
@ -90,6 +94,21 @@ class Ticket < ActiveRecord::Base
|
|||
return subject
|
||||
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
|
||||
def number_generate
|
||||
Ticket.new.number_adapter = Setting.get('ticket_number')
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
class User < ActiveRecord::Base
|
||||
class User < ApplicationModel
|
||||
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
|
||||
after_create :delete_cache
|
||||
after_update :delete_cache
|
||||
after_destroy :delete_cache
|
||||
after_create :cache_delete
|
||||
after_update :cache_delete
|
||||
after_destroy :cache_delete
|
||||
|
||||
@@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 )
|
||||
user = User.where( :login => username, :active => true ).first
|
||||
|
@ -54,7 +55,7 @@ class User < ActiveRecord::Base
|
|||
|
||||
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
|
||||
user = User.find(user_id)
|
||||
|
@ -93,21 +94,12 @@ class User < ActiveRecord::Base
|
|||
}
|
||||
data['organizations'] = organizations
|
||||
|
||||
@@cache[user_id] = data
|
||||
cache_set(user.id, data)
|
||||
|
||||
return data
|
||||
end
|
||||
|
||||
def cache_reset
|
||||
@@cache[self.id] = nil
|
||||
end
|
||||
|
||||
private
|
||||
def delete_cache
|
||||
puts 'delete_cache', self.insepct
|
||||
@@cache[self.id] = nil
|
||||
end
|
||||
|
||||
def check_name
|
||||
if self.firstname && (!self.lastname || self.lastname == '') then
|
||||
name = self.firstname.split(' ', 2)
|
||||
|
|
|
@ -29,6 +29,7 @@ class CreateBase < ActiveRecord::Migration
|
|||
t.column :active, :boolean, :null => false, :default => true
|
||||
t.column :note, :string, :limit => 250, :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.timestamps
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue