Fix cache with Rails 5.

This commit is contained in:
Jason Lee committed 2016-05-25 11:07:26 +08:00
1 parent 9b33f4f399
commit bfc24bc56b
11 files changed
+48 -45

No files matched your search

-5
View File
@@ -25,11 +25,6 @@ module RuCaptcha
def configure(&block)
config.instance_exec(&block)
# enable cache if cache_limit less than 1
if config.cache_limit >= 1
RuCaptcha::Captcha.send(:include, RuCaptcha::Cache)
end
end
end
end
+13 -15
View File
@@ -3,30 +3,27 @@ require 'fileutils'
module RuCaptcha
# File Cache
module Cache
extend ActiveSupport::Concern
included do
class << self
alias_method_chain :create, :cache
alias_method_chain :random_chars, :cache
def self.prepended(base)
class << base
prepend ClassMethods
end
end
module ClassMethods
def create_with_cache(code)
cache.fetch(code) do
create_without_cache(code)
def create(code)
cache.fetch(code, expires_in: 1.days) do
super(code)
end
end
def random_chars_with_cache
def random_chars
if cached_codes.length >= RuCaptcha.config.cache_limit
return cached_codes.sample
else
code = random_chars_without_cache
cached_codes << code
return code
end
code = super
cached_codes << code
code
end
def cache
@@ -35,7 +32,8 @@ module RuCaptcha
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)
@cache.clear
# clear expired captcha cache files on Process restart
@cache.cleanup
@cache
end
+1 -1
View File
@@ -77,7 +77,7 @@ module RuCaptcha
png_file_path
else
command.strip!
out, err, st = Open3.capture3(command)
out, err, _st = Open3.capture3(command)
raise "RuCaptcha: #{err.strip}" if err.present?
out
end
+7
View File
@@ -1,5 +1,12 @@
module RuCaptcha
class Engine < ::Rails::Engine
isolate_namespace RuCaptcha
initializer 'rucaptcha.prepend.cache' do
# enable cache if cache_limit less than 1
if RuCaptcha.config.cache_limit >= 1
RuCaptcha::Captcha.send(:prepend, RuCaptcha::Cache)
end
end
end
end
+1 -1
View File
@@ -1,3 +1,3 @@
module RuCaptcha
VERSION = '0.4.5'
VERSION = '0.5.0'
end