Module: RuCaptcha::Cache::ClassMethods

Defined in:
lib/rucaptcha/cache.rb

Instance Method Summary collapse

Instance Method Details

#cacheObject



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

def cache
  return @cache if defined?(@cache)

  cache_path = Rails.root.join('tmp', 'cache', 'rucaptcha')
  if !File.exists? cache_path
    FileUtils.mkdir_p(cache_path)
  end
  @cache = ActiveSupport::Cache::FileStore.new(cache_path)
  @cache.clear
  @cache
end

#cached_codesObject



44
45
46
# File 'lib/rucaptcha/cache.rb', line 44

def cached_codes
  @cached_codes ||= []
end

#create_with_cache(code) ⇒ Object



16
17
18
19
20
# File 'lib/rucaptcha/cache.rb', line 16

def create_with_cache(code)
  cache.fetch(code) do
    create_without_cache(code)
  end
end

#random_chars_with_cacheObject



22
23
24
25
26
27
28
29
30
# File 'lib/rucaptcha/cache.rb', line 22

def random_chars_with_cache
  if cached_codes.length >= RuCaptcha.config.cache_limit
    return cached_codes.sample
  else
    code = random_chars_without_cache
    cached_codes << code
    return code
  end
end