Class: Tr8n::Cache

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

Instance Method Summary collapse

Instance Method Details

#cache_nameObject



69
70
71
# File 'lib/tr8n/cache.rb', line 69

def cache_name
  self.class.name.split('::').last
end

#cached_by_source?Boolean

Returns:

  • (Boolean)


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

def cached_by_source?
  true
end

#clear(opts = {}) ⇒ Object



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

def clear(opts = {})
  # do nothing
end

#delete(key, opts = {}) ⇒ Object



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

def delete(key, opts = {})
  # do nothing
end

#deserialize_object(key, data) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tr8n/cache.rb', line 123

def deserialize_object(key, data)
  #info(data.inspect)

  case key[0..1]
    when Tr8n::Application.cache_prefix
      return Tr8n::Application.new(JSON.parse(data))
    when Tr8n::Language.cache_prefix
      return Tr8n::Language.new(JSON.parse(data).merge(:application => Tr8n.session.application))
    when Tr8n::Source.cache_prefix
      return Tr8n::Source.new(JSON.parse(data).merge(:application => Tr8n.session.application))
    when Tr8n::Component.cache_prefix
      return Tr8n::Component.new(JSON.parse(data).merge(:application => Tr8n.session.application))
    when Tr8n::TranslationKey.cache_prefix
      return Tr8n::TranslationKey.new(JSON.parse(data).merge(:application => Tr8n.session.application))
  end

  # API response form will be here
  JSON.parse(data)
end

#enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  Tr8n.config.cache[:enabled]
end

#exist?(key, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/tr8n/cache.rb', line 99

def exist?(key, opts = {})
  false
end

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



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

def fetch(key, opts = {})
  return nil unless block_given?
  yield
end

#info(msg) ⇒ Object



73
74
75
# File 'lib/tr8n/cache.rb', line 73

def info(msg)
  Tr8n.logger.info("#{cache_name} - #{msg}")
end

#read_only?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/tr8n/cache.rb', line 65

def read_only?
  true
end

#reset_versionObject



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

def reset_version
  @version = nil
end

#serialize_object(key, data) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tr8n/cache.rb', line 107

def serialize_object(key, data)
  if [Tr8n::Application.cache_prefix,
          Tr8n::Language.cache_prefix,
          Tr8n::Source.cache_prefix,
          Tr8n::Component.cache_prefix,
          Tr8n::TranslationKey.cache_prefix].include?(key[0..1])
    json_data = data.to_cache_hash.to_json
  else
    # the data must be in cacheable form - usually API responses
    json_data = data.to_json
  end

  #info(json_data)
  json_data
end

#store(key, data, opts = {}) ⇒ Object



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

def store(key, data, opts = {})
  # do nothing
end

#upgrade_versionObject



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

def upgrade_version
  store('tr8n_cache_version', {'version' => version + 1}, :ttl => 0, :skip_version => true)
  @version = nil
end

#versionObject



41
42
43
44
45
46
# File 'lib/tr8n/cache.rb', line 41

def version
  @version ||= begin
    v = fetch('tr8n_cache_version', :skip_version => true)
    v ? v['version'] : Tr8n.config.cache[:version]
  end
end

#versioned_key(key, opts = {}) ⇒ Object



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

def versioned_key(key, opts = {})
  return key if opts[:skip_version]
  "tr8n_rc_v#{version}_#{key}"
end

#warn(msg) ⇒ Object



77
78
79
# File 'lib/tr8n/cache.rb', line 77

def warn(msg)
  Tr8n.logger.warn("#{cache_name} - #{msg}")
end