Improved caching.
This commit is contained in:
parent
8b406e6d52
commit
f3b4043c81
1 changed files with 58 additions and 32 deletions
|
@ -92,10 +92,10 @@ module Session
|
||||||
# raise "Exception from thread"
|
# raise "Exception from thread"
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
# system settings
|
|
||||||
sleep 0.3
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# system settings
|
||||||
|
sleep 0.4
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -146,8 +146,10 @@ end
|
||||||
|
|
||||||
module CacheIn
|
module CacheIn
|
||||||
@@data = {}
|
@@data = {}
|
||||||
|
@@data_time = {}
|
||||||
@@expires_in = {}
|
@@expires_in = {}
|
||||||
@@expires_in_ttl = {}
|
@@expires_in_ttl = {}
|
||||||
|
|
||||||
def self.set( key, value, params = {} )
|
def self.set( key, value, params = {} )
|
||||||
# puts 'CacheIn.set:' + key + '-' + value.inspect
|
# puts 'CacheIn.set:' + key + '-' + value.inspect
|
||||||
if params[:expires_in]
|
if params[:expires_in]
|
||||||
|
@ -155,25 +157,42 @@ module CacheIn
|
||||||
@@expires_in_ttl[key] = params[:expires_in]
|
@@expires_in_ttl[key] = params[:expires_in]
|
||||||
end
|
end
|
||||||
@@data[ key ] = value
|
@@data[ key ] = value
|
||||||
|
@@data_time[ key ] = Time.now
|
||||||
end
|
end
|
||||||
def self.expired( key )
|
|
||||||
|
def self.expired( key, params = {} )
|
||||||
|
|
||||||
|
# expire if value never was set
|
||||||
|
return true if !@@data.include? key
|
||||||
|
|
||||||
|
# set re_expire
|
||||||
|
if params[:re_expire]
|
||||||
|
if @@expires_in[key]
|
||||||
|
@@expires_in[key] = Time.now + @@expires_in_ttl[key]
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
# check if expired
|
||||||
if @@expires_in[key]
|
if @@expires_in[key]
|
||||||
return true if @@expires_in[key] < Time.now
|
return true if @@expires_in[key] < Time.now
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
return true
|
|
||||||
|
# return false if key was set without expires_in
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.get_time( key, params = {} )
|
||||||
|
data = self.get( key, params )
|
||||||
|
if data
|
||||||
|
return @@data_time[key]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.get( key, params = {} )
|
def self.get( key, params = {} )
|
||||||
# puts 'CacheIn.get:' + key + '-' + @@data[ key ].inspect
|
# puts 'CacheIn.get:' + key + '-' + @@data[ key ].inspect
|
||||||
if !params[:re_expire]
|
return if self.expired( key, params )
|
||||||
if @@expires_in[key]
|
|
||||||
return if self.expired(key)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if @@expires_in[key]
|
|
||||||
@@expires_in[key] = Time.now + @@expires_in_ttl[key]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@data[ key ]
|
@@data[ key ]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -344,9 +363,11 @@ class ClientState
|
||||||
|
|
||||||
# overview
|
# overview
|
||||||
cache_key = 'user_' + user.id.to_s + '_overview'
|
cache_key = 'user_' + user.id.to_s + '_overview'
|
||||||
overview = CacheIn.get( cache_key )
|
overview_time = CacheIn.get_time( cache_key )
|
||||||
if overview && @data[:overview] != overview
|
if overview_time && @data[:overview_time] != overview_time
|
||||||
@data[:overview] = overview
|
@data[:overview_time] = overview_time
|
||||||
|
overview = CacheIn.get( cache_key )
|
||||||
|
|
||||||
self.log "push overview for user #{user.id}"
|
self.log "push overview for user #{user.id}"
|
||||||
|
|
||||||
# send update to browser
|
# send update to browser
|
||||||
|
@ -363,9 +384,10 @@ class ClientState
|
||||||
overviews.each { |overview|
|
overviews.each { |overview|
|
||||||
cache_key = 'user_' + user.id.to_s + '_overview_data_' + overview.meta[:url]
|
cache_key = 'user_' + user.id.to_s + '_overview_data_' + overview.meta[:url]
|
||||||
|
|
||||||
overview_data = CacheIn.get( cache_key )
|
overview_data_time = CacheIn.get_time( cache_key )
|
||||||
if overview_data && @data[cache_key] != overview_data
|
if overview_data_time && @data[cache_key] != overview_data_time
|
||||||
@data[cache_key] = overview_data
|
@data[cache_key] = overview_data_time
|
||||||
|
overview_data = CacheIn.get( cache_key )
|
||||||
self.log "push overview_data for user #{user.id}"
|
self.log "push overview_data for user #{user.id}"
|
||||||
|
|
||||||
users = {}
|
users = {}
|
||||||
|
@ -392,9 +414,10 @@ class ClientState
|
||||||
|
|
||||||
# ticket_create_attributes
|
# ticket_create_attributes
|
||||||
cache_key = 'user_' + user.id.to_s + '_ticket_create_attributes'
|
cache_key = 'user_' + user.id.to_s + '_ticket_create_attributes'
|
||||||
ticket_create_attributes = CacheIn.get( cache_key )
|
ticket_create_attributes_time = CacheIn.get_time( cache_key )
|
||||||
if ticket_create_attributes && @data[:ticket_create_attributes] != ticket_create_attributes
|
if ticket_create_attributes_time && @data[:ticket_create_attributes_time] != ticket_create_attributes_time
|
||||||
@data[:ticket_create_attributes] = ticket_create_attributes
|
@data[:ticket_create_attributes_time] = ticket_create_attributes_time
|
||||||
|
ticket_create_attributes = CacheIn.get( cache_key )
|
||||||
self.log "push ticket_create_attributes for user #{user.id}"
|
self.log "push ticket_create_attributes for user #{user.id}"
|
||||||
|
|
||||||
# send update to browser
|
# send update to browser
|
||||||
|
@ -406,9 +429,10 @@ class ClientState
|
||||||
|
|
||||||
# recent viewed
|
# recent viewed
|
||||||
cache_key = 'user_' + user.id.to_s + '_recent_viewed'
|
cache_key = 'user_' + user.id.to_s + '_recent_viewed'
|
||||||
recent_viewed = CacheIn.get( cache_key )
|
recent_viewed_time = CacheIn.get_time( cache_key )
|
||||||
if recent_viewed && @data[:recent_viewed] != recent_viewed
|
if recent_viewed_time && @data[:recent_viewed_time] != recent_viewed_time
|
||||||
@data[:recent_viewed] = recent_viewed
|
@data[:recent_viewed_time] = recent_viewed_time
|
||||||
|
recent_viewed = CacheIn.get( cache_key )
|
||||||
self.log "push recent_viewed for user #{user.id}"
|
self.log "push recent_viewed for user #{user.id}"
|
||||||
|
|
||||||
# send update to browser
|
# send update to browser
|
||||||
|
@ -421,9 +445,10 @@ class ClientState
|
||||||
|
|
||||||
# activity stream
|
# activity stream
|
||||||
cache_key = 'user_' + user.id.to_s + '_activity_stream'
|
cache_key = 'user_' + user.id.to_s + '_activity_stream'
|
||||||
activity_stream = CacheIn.get( cache_key )
|
activity_stream_time = CacheIn.get_time( cache_key )
|
||||||
if activity_stream && @data[:activity_stream] != activity_stream
|
if activity_stream_time && @data[:activity_stream_time] != activity_stream_time
|
||||||
@data[:activity_stream] = activity_stream
|
@data[:activity_stream_time] = activity_stream_time
|
||||||
|
activity_stream = CacheIn.get( cache_key )
|
||||||
self.log "push activity_stream for user #{user.id}"
|
self.log "push activity_stream for user #{user.id}"
|
||||||
|
|
||||||
# send update to browser
|
# send update to browser
|
||||||
|
@ -437,9 +462,10 @@ class ClientState
|
||||||
|
|
||||||
# rss
|
# rss
|
||||||
cache_key = 'user_' + user.id.to_s + '_rss'
|
cache_key = 'user_' + user.id.to_s + '_rss'
|
||||||
rss_items = CacheIn.get( cache_key )
|
rss_items_time = CacheIn.get_time( cache_key )
|
||||||
if rss_items && @data[:rss] != rss_items
|
if rss_items_time && @data[:rss_time] != rss_items_time
|
||||||
@data[:rss] = rss_items
|
@data[:rss_time] = rss_items_time
|
||||||
|
rss_items = CacheIn.get( cache_key )
|
||||||
self.log "push rss for user #{user.id}"
|
self.log "push rss for user #{user.id}"
|
||||||
|
|
||||||
# send update to browser
|
# send update to browser
|
||||||
|
|
Loading…
Reference in a new issue