Class: Tr8n::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/tr8n/cache.rb

Class Method Summary collapse

Class Method Details

.cacheObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tr8n/cache.rb', line 31

def self.cache
  return nil unless enabled?
  
  @cache ||= begin
    if Tr8n::Config.cache_adapter == 'ActiveSupport::Cache'
      store_params = cache_store_params
      store_params[0] = store_params[0].to_sym
      ActiveSupport::Cache.lookup_store(*store_params)
    else
      eval(Tr8n::Config.cache_adapter)  
    end
  end
end

.cache_store_paramsObject



27
28
29
# File 'lib/tr8n/cache.rb', line 27

def self.cache_store_params
  [Tr8n::Config.cache_store].flatten
end

.cleanup(opts = nil) ⇒ Object



96
97
98
99
# File 'lib/tr8n/cache.rb', line 96

def self.cleanup(opts = nil)
  return unless enabled?
  cache.cleanup(opts)
end

.clear(opts = nil) ⇒ Object



91
92
93
94
# File 'lib/tr8n/cache.rb', line 91

def self.clear(opts = nil)
  return unless enabled?
  cache.clear(opts)
end

.decrement(name, amount = 1, opts = nil) ⇒ Object



106
107
108
109
# File 'lib/tr8n/cache.rb', line 106

def self.decrement(name, amount = 1, opts = nil)
  return unless enabled?
  cache.decrement(name, amount, opts)
end

.delete(key, opts = nil) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/tr8n/cache.rb', line 78

def self.delete(key, opts = nil)
  return unless enabled?

  # pp "delete #{key}"

  cache.delete(versioned_key(key), opts)
end

.disabled?Boolean



49
50
51
# File 'lib/tr8n/cache.rb', line 49

def self.disabled?
  not enabled?
end

.enabled?Boolean



45
46
47
# File 'lib/tr8n/cache.rb', line 45

def self.enabled?
  Tr8n::Config.enable_caching?
end

.exist?(name, opts = nil) ⇒ Boolean



86
87
88
89
# File 'lib/tr8n/cache.rb', line 86

def self.exist?(name, opts = nil)
  return unless enabled?
  cache.exists?(name, opts)
end

.fetch(key, opts = {}) ⇒ Object

Cache Adapter Methods



68
69
70
71
72
73
74
75
76
# File 'lib/tr8n/cache.rb', line 68

def self.fetch(key, opts = {})
  return yield unless enabled?
  
  # pp "fetch #{key}"
  
  cache.fetch(versioned_key(key), opts) do 
    yield
  end
end

.increment(name, amount = 1, opts = nil) ⇒ Object



101
102
103
104
# File 'lib/tr8n/cache.rb', line 101

def self.increment(name, amount = 1, opts = nil)
  return unless enabled?
  cache.increment(name, amount, opts)
end

.invalidate_source(source_name, language = Tr8n::Config.current_language) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tr8n/cache.rb', line 125

def self.invalidate_source(source_name, language = Tr8n::Config.current_language)
  return if disabled? or language.default? 
  
  # only memory store needs this kind of reloading
  # memcached and other stores will expire shared keys 
  return unless memory_store?
  
  # pp [:memory_times, sources_timestamps]
  
  translation_source = Tr8n::TranslationSource.find_or_create(source_name)

  # this is the only record that will never be cached and will always be loaded from the database
  translation_source_language = Tr8n::TranslationSourceLanguage.find_or_create(translation_source, language)

  if last_updated_at(translation_source_language) < translation_source_language.updated_at
    keys = Tr8n::TranslationKey.where(["id in (select translation_key_id from #{Tr8n::TranslationKeySource.table_name} where translation_source_id = ?) and updated_at > ?", 
                                      translation_source.id, last_updated_at(translation_source_language)])
                                      
    # pp "****************************** Found #{keys.count} outdated keys for this language"                                  
    keys.each do |key|
      key.clear_translations_cache_for_language(language)
    end
    
    sources_timestamps[translation_source_language.id] = translation_source_language.updated_at
  end
end

.last_updated_at(translation_source_language) ⇒ Object



121
122
123
# File 'lib/tr8n/cache.rb', line 121

def self.last_updated_at(translation_source_language)
  sources_timestamps[translation_source_language.id] ||= 365.days.ago
end

.memory_store?Boolean



61
62
63
# File 'lib/tr8n/cache.rb', line 61

def self.memory_store?
  cache_store_params.first == 'memory_store'
end

.sources_timestampsObject

For local cache, the source+language = updated_at must always be present These keys cannot expire, or refreshing of the resources will never take place



117
118
119
# File 'lib/tr8n/cache.rb', line 117

def self.sources_timestamps
  @sources_timestamps ||= {}
end

.versionObject



53
54
55
# File 'lib/tr8n/cache.rb', line 53

def self.version
  Tr8n::Config.cache_version
end

.versioned_key(key) ⇒ Object



57
58
59
# File 'lib/tr8n/cache.rb', line 57

def self.versioned_key(key)
  "#{version}_#{key}"
end