Module: RuCaptcha::Cache::ClassMethods

Defined in:
lib/rucaptcha/cache.rb

Instance Method Summary collapse

Instance Method Details

#cacheObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/rucaptcha/cache.rb', line 29

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

  cache_path = Rails.root.join('tmp', 'cache', 'rucaptcha')
  FileUtils.mkdir_p(cache_path) unless File.exist? cache_path
  @cache = ActiveSupport::Cache::FileStore.new(cache_path)
  # clear expired captcha cache files on Process restart
  @cache.cleanup
  @cache
end

#cached_codesObject



40
41
42
# File 'lib/rucaptcha/cache.rb', line 40

def cached_codes
  @cached_codes ||= []
end

#create(code) ⇒ Object



13
14
15
16
17
# File 'lib/rucaptcha/cache.rb', line 13

def create(code)
  cache.fetch(code, expires_in: 1.days) do
    super(code)
  end
end

#random_charsObject



19
20
21
22
23
24
25
26
27
# File 'lib/rucaptcha/cache.rb', line 19

def random_chars
  if cached_codes.length >= RuCaptcha.config.cache_limit
    return cached_codes.sample
  end

  code = super
  cached_codes << code
  code
end