Added caching of translation table. Thanks to Roy!
This commit is contained in:
parent
bd2310f88e
commit
0a38481287
1 changed files with 30 additions and 10 deletions
|
@ -2,10 +2,17 @@
|
||||||
|
|
||||||
class Translation < ApplicationModel
|
class Translation < ApplicationModel
|
||||||
before_create :set_initial
|
before_create :set_initial
|
||||||
|
after_create :cache_clear
|
||||||
|
after_update :cache_clear
|
||||||
|
after_destroy :cache_clear
|
||||||
|
|
||||||
def self.list(locale)
|
def self.list(locale)
|
||||||
translations = Translation.where( :locale => locale )
|
|
||||||
|
# check cache
|
||||||
|
list = cache_get( locale )
|
||||||
|
if !list
|
||||||
list = []
|
list = []
|
||||||
|
translations = Translation.where( :locale => locale.downcase )
|
||||||
translations.each { |item|
|
translations.each { |item|
|
||||||
data = [
|
data = [
|
||||||
item.id,
|
item.id,
|
||||||
|
@ -15,6 +22,10 @@ class Translation < ApplicationModel
|
||||||
list.push data
|
list.push data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# set cache
|
||||||
|
cache_set( locale, list )
|
||||||
|
end
|
||||||
|
|
||||||
timestamp_map_default = 'yyyy-mm-dd HH:MM'
|
timestamp_map_default = 'yyyy-mm-dd HH:MM'
|
||||||
timestamp_map = {
|
timestamp_map = {
|
||||||
:de => 'dd.mm.yyyy HH:MM',
|
:de => 'dd.mm.yyyy HH:MM',
|
||||||
|
@ -47,4 +58,13 @@ class Translation < ApplicationModel
|
||||||
def set_initial
|
def set_initial
|
||||||
self.target_initial = self.target
|
self.target_initial = self.target
|
||||||
end
|
end
|
||||||
|
def cache_clear
|
||||||
|
Cache.delete( 'Translation::' + self.locale.downcase )
|
||||||
|
end
|
||||||
|
def self.cache_set(locale, data)
|
||||||
|
Cache.write( 'Translation::' + locale.downcase, data )
|
||||||
|
end
|
||||||
|
def self.cache_get(locale)
|
||||||
|
Cache.get( 'Translation::' + locale.downcase )
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue