From bfc24bc56b09fd089a94596c0dd97494cb9e4906 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 25 May 2016 11:07:26 +0800 Subject: [PATCH] Fix cache with Rails 5. --- CHANGELOG.md | 4 ++++ Gemfile | 4 ++++ Gemfile.lock | 4 ++-- lib/rucaptcha.rb | 5 ----- lib/rucaptcha/cache.rb | 28 +++++++++++++--------------- lib/rucaptcha/captcha.rb | 2 +- lib/rucaptcha/engine.rb | 7 +++++++ lib/rucaptcha/version.rb | 2 +- rucaptcha.gemspec | 23 +++++++++-------------- spec/cache_spec.rb | 8 ++++---- spec/captcha_spec.rb | 6 +++--- 11 files changed, 48 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 571ac19..8364a72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +0.5.0 + +- Fix cache with Rails 5. + 0.4.5 ----- diff --git a/Gemfile b/Gemfile index 46a4f91..8a579c5 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,8 @@ source 'https://rubygems.org' gemspec + +gem 'rake' +gem 'rails' +gem 'rspec' gem 'mini_magick' diff --git a/Gemfile.lock b/Gemfile.lock index 5edd2d6..c86999a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rucaptcha (0.4.5) + rucaptcha (0.5.0) GEM remote: https://rubygems.org/ @@ -127,7 +127,7 @@ DEPENDENCIES mini_magick rails rake - rspec (>= 3.3.0) + rspec rucaptcha! BUNDLED WITH diff --git a/lib/rucaptcha.rb b/lib/rucaptcha.rb index 14cc0a7..d1a8b43 100644 --- a/lib/rucaptcha.rb +++ b/lib/rucaptcha.rb @@ -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 diff --git a/lib/rucaptcha/cache.rb b/lib/rucaptcha/cache.rb index 35bc482..7e7ceaa 100644 --- a/lib/rucaptcha/cache.rb +++ b/lib/rucaptcha/cache.rb @@ -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 diff --git a/lib/rucaptcha/captcha.rb b/lib/rucaptcha/captcha.rb index eda910c..8952f65 100644 --- a/lib/rucaptcha/captcha.rb +++ b/lib/rucaptcha/captcha.rb @@ -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 diff --git a/lib/rucaptcha/engine.rb b/lib/rucaptcha/engine.rb index a31f704..2e8d2af 100644 --- a/lib/rucaptcha/engine.rb +++ b/lib/rucaptcha/engine.rb @@ -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 diff --git a/lib/rucaptcha/version.rb b/lib/rucaptcha/version.rb index 983bde2..ef06ee8 100644 --- a/lib/rucaptcha/version.rb +++ b/lib/rucaptcha/version.rb @@ -1,3 +1,3 @@ module RuCaptcha - VERSION = '0.4.5' + VERSION = '0.5.0' end diff --git a/rucaptcha.gemspec b/rucaptcha.gemspec index c672f27..228fa28 100644 --- a/rucaptcha.gemspec +++ b/rucaptcha.gemspec @@ -1,20 +1,15 @@ lib = File.expand_path('../lib/', __FILE__) -$:.unshift lib unless $:.include?(lib) +$LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib) -require "rucaptcha/version" +require 'rucaptcha/version' Gem::Specification.new do |s| - s.name = 'rucaptcha' - s.version = RuCaptcha::VERSION - s.authors = ['Jason Lee'] - s.email = 'huacnlee@gmail.com' - s.files = Dir.glob("lib/**/*") + Dir.glob("app/**/*") + Dir.glob("config/**/*") + %w(README.md CHANGELOG.md) - s.homepage = 'https://github.com/huacnlee/rucaptcha' + s.name = 'rucaptcha' + s.version = RuCaptcha::VERSION + s.authors = ['Jason Lee'] + s.email = 'huacnlee@gmail.com' + s.files = Dir.glob('lib/**/*') + Dir.glob('app/**/*') + Dir.glob('config/**/*') + %w(README.md CHANGELOG.md) + s.homepage = 'https://github.com/huacnlee/rucaptcha' s.require_paths = ['lib'] - s.summary = 'This is a Captcha gem for Rails Application. It run ImageMagick command to draw Captcha image.' - - - s.add_development_dependency 'rake' - s.add_development_dependency 'rails' - s.add_development_dependency 'rspec', '>= 3.3.0' + s.summary = 'This is a Captcha gem for Rails Application. It run ImageMagick command to draw Captcha image.' end diff --git a/spec/cache_spec.rb b/spec/cache_spec.rb index 4cab649..0656403 100644 --- a/spec/cache_spec.rb +++ b/spec/cache_spec.rb @@ -5,17 +5,17 @@ describe RuCaptcha::Cache do it 'should generate max chars by config.cache_limit' do allow(RuCaptcha.config).to receive(:cache_limit).and_return(5) items = [] - 10.times do - items << RuCaptcha::Captcha.random_chars_with_cache + 100.times do + items << RuCaptcha::Captcha.random_chars end - expect(items.uniq.length).to eq 5 + expect(items.uniq.length).to eq RuCaptcha.config.cache_limit expect(RuCaptcha::Captcha.cached_codes).to eq items.uniq end end describe '.create' do it 'should work' do - expect(RuCaptcha::Captcha).to receive(:create_without_cache).and_return('aabb') + expect(RuCaptcha::Captcha).to receive(:create).and_return('aabb') expect(RuCaptcha::Captcha.create('abcd')).to eq('aabb') end end diff --git a/spec/captcha_spec.rb b/spec/captcha_spec.rb index 4e55dc3..f2412a8 100644 --- a/spec/captcha_spec.rb +++ b/spec/captcha_spec.rb @@ -3,16 +3,16 @@ require 'spec_helper' describe RuCaptcha::Captcha do describe '.random_chars' do it 'should len equal config.len' do - expect(RuCaptcha::Captcha.random_chars_without_cache.length).to eq(RuCaptcha.config.len) + expect(RuCaptcha::Captcha.random_chars.length).to eq(RuCaptcha.config.len) end it 'should return 0-9 and lower str' do - expect(RuCaptcha::Captcha.random_chars_without_cache).to match(/[a-z0-9]/) + expect(RuCaptcha::Captcha.random_chars).to match(/[a-z0-9]/) end it 'should not include [0ol1]' do 10_000.times do - expect(RuCaptcha::Captcha.random_chars_without_cache).not_to match(/[0ol1]/i) + expect(RuCaptcha::Captcha.random_chars).not_to match(/[0ol1]/i) end end end