Fix cache with Rails 5.

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

View File

@ -1,3 +1,7 @@
0.5.0
- Fix cache with Rails 5.
0.4.5 0.4.5
----- -----

View File

@ -1,4 +1,8 @@
source 'https://rubygems.org' source 'https://rubygems.org'
gemspec gemspec
gem 'rake'
gem 'rails'
gem 'rspec'
gem 'mini_magick' gem 'mini_magick'

View File

@ -1,7 +1,7 @@
PATH PATH
remote: . remote: .
specs: specs:
rucaptcha (0.4.5) rucaptcha (0.5.0)
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
@ -127,7 +127,7 @@ DEPENDENCIES
mini_magick mini_magick
rails rails
rake rake
rspec (>= 3.3.0) rspec
rucaptcha! rucaptcha!
BUNDLED WITH BUNDLED WITH

View File

@ -25,11 +25,6 @@ module RuCaptcha
def configure(&block) def configure(&block)
config.instance_exec(&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 end
end end

View File

@ -3,30 +3,27 @@ require 'fileutils'
module RuCaptcha module RuCaptcha
# File Cache # File Cache
module Cache module Cache
extend ActiveSupport::Concern def self.prepended(base)
class << base
included do prepend ClassMethods
class << self
alias_method_chain :create, :cache
alias_method_chain :random_chars, :cache
end end
end end
module ClassMethods module ClassMethods
def create_with_cache(code) def create(code)
cache.fetch(code) do cache.fetch(code, expires_in: 1.days) do
create_without_cache(code) super(code)
end end
end end
def random_chars_with_cache def random_chars
if cached_codes.length >= RuCaptcha.config.cache_limit if cached_codes.length >= RuCaptcha.config.cache_limit
return cached_codes.sample return cached_codes.sample
else
code = random_chars_without_cache
cached_codes << code
return code
end end
code = super
cached_codes << code
code
end end
def cache def cache
@ -35,7 +32,8 @@ module RuCaptcha
cache_path = Rails.root.join('tmp', 'cache', 'rucaptcha') cache_path = Rails.root.join('tmp', 'cache', 'rucaptcha')
FileUtils.mkdir_p(cache_path) unless File.exist? cache_path FileUtils.mkdir_p(cache_path) unless File.exist? cache_path
@cache = ActiveSupport::Cache::FileStore.new(cache_path) @cache = ActiveSupport::Cache::FileStore.new(cache_path)
@cache.clear # clear expired captcha cache files on Process restart
@cache.cleanup
@cache @cache
end end

View File

@ -77,7 +77,7 @@ module RuCaptcha
png_file_path png_file_path
else else
command.strip! command.strip!
out, err, st = Open3.capture3(command) out, err, _st = Open3.capture3(command)
raise "RuCaptcha: #{err.strip}" if err.present? raise "RuCaptcha: #{err.strip}" if err.present?
out out
end end

View File

@ -1,5 +1,12 @@
module RuCaptcha module RuCaptcha
class Engine < ::Rails::Engine class Engine < ::Rails::Engine
isolate_namespace RuCaptcha 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
end end

View File

@ -1,3 +1,3 @@
module RuCaptcha module RuCaptcha
VERSION = '0.4.5' VERSION = '0.5.0'
end end

View File

@ -1,20 +1,15 @@
lib = File.expand_path('../lib/', __FILE__) 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| Gem::Specification.new do |s|
s.name = 'rucaptcha' s.name = 'rucaptcha'
s.version = RuCaptcha::VERSION s.version = RuCaptcha::VERSION
s.authors = ['Jason Lee'] s.authors = ['Jason Lee']
s.email = 'huacnlee@gmail.com' s.email = 'huacnlee@gmail.com'
s.files = Dir.glob("lib/**/*") + Dir.glob("app/**/*") + Dir.glob("config/**/*") + %w(README.md CHANGELOG.md) s.files = Dir.glob('lib/**/*') + Dir.glob('app/**/*') + Dir.glob('config/**/*') + %w(README.md CHANGELOG.md)
s.homepage = 'https://github.com/huacnlee/rucaptcha' s.homepage = 'https://github.com/huacnlee/rucaptcha'
s.require_paths = ['lib'] s.require_paths = ['lib']
s.summary = 'This is a Captcha gem for Rails Application. It run ImageMagick command to draw Captcha image.' 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'
end end

View File

@ -5,17 +5,17 @@ describe RuCaptcha::Cache do
it 'should generate max chars by config.cache_limit' do it 'should generate max chars by config.cache_limit' do
allow(RuCaptcha.config).to receive(:cache_limit).and_return(5) allow(RuCaptcha.config).to receive(:cache_limit).and_return(5)
items = [] items = []
10.times do 100.times do
items << RuCaptcha::Captcha.random_chars_with_cache items << RuCaptcha::Captcha.random_chars
end 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 expect(RuCaptcha::Captcha.cached_codes).to eq items.uniq
end end
end end
describe '.create' do describe '.create' do
it 'should work' 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') expect(RuCaptcha::Captcha.create('abcd')).to eq('aabb')
end end
end end

View File

@ -3,16 +3,16 @@ require 'spec_helper'
describe RuCaptcha::Captcha do describe RuCaptcha::Captcha do
describe '.random_chars' do describe '.random_chars' do
it 'should len equal config.len' 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 end
it 'should return 0-9 and lower str' do 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 end
it 'should not include [0ol1]' do it 'should not include [0ol1]' do
10_000.times 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 end
end end