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
|
# 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'
|
||||||
|
|
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
|
|
@ -21,6 +21,9 @@ class Authorization < ActiveRecord::Base
|
||||||
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
|
|
@ -1,5 +1,9 @@
|
||||||
class Ticket < ActiveRecord::Base
|
class Ticket < ActiveRecord::Base
|
||||||
before_create :number_generate, :check_defaults
|
before_create :number_generate, :check_defaults
|
||||||
|
after_create :user_ticket_counter_update
|
||||||
|
after_update :user_ticket_counter_update
|
||||||
|
after_destroy :user_ticket_counter_update
|
||||||
|
|
||||||
belongs_to :group
|
belongs_to :group
|
||||||
has_many :articles
|
has_many :articles
|
||||||
belongs_to :ticket_state, :class_name => 'Ticket::State'
|
belongs_to :ticket_state, :class_name => 'Ticket::State'
|
||||||
|
@ -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')
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue