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,18 +2,29 @@
|
||||||
|
|
||||||
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 )
|
|
||||||
list = []
|
# check cache
|
||||||
translations.each { |item|
|
list = cache_get( locale )
|
||||||
data = [
|
if !list
|
||||||
item.id,
|
list = []
|
||||||
item.source,
|
translations = Translation.where( :locale => locale.downcase )
|
||||||
item.target,
|
translations.each { |item|
|
||||||
]
|
data = [
|
||||||
list.push data
|
item.id,
|
||||||
}
|
item.source,
|
||||||
|
item.target,
|
||||||
|
]
|
||||||
|
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 = {
|
||||||
|
@ -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