Fix cache with Rails 5.
This commit is contained in:
parent
9b33f4f399
commit
bfc24bc56b
|
@ -1,3 +1,7 @@
|
|||
0.5.0
|
||||
|
||||
- Fix cache with Rails 5.
|
||||
|
||||
0.4.5
|
||||
-----
|
||||
|
||||
|
|
4
Gemfile
4
Gemfile
|
@ -1,4 +1,8 @@
|
|||
source 'https://rubygems.org'
|
||||
|
||||
gemspec
|
||||
|
||||
gem 'rake'
|
||||
gem 'rails'
|
||||
gem 'rspec'
|
||||
gem 'mini_magick'
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,3 +1,3 @@
|
|||
module RuCaptcha
|
||||
VERSION = '0.4.5'
|
||||
VERSION = '0.5.0'
|
||||
end
|
||||
|
|
|
@ -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.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'
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue