From f6de6503572121aa890c84241b74ce2945b9ed3a Mon Sep 17 00:00:00 2001 From: dblock Date: Wed, 7 Oct 2015 11:15:02 -0400 Subject: [PATCH] Added support for JRuby. --- .travis.yml | 3 +++ CHANGES | 8 ++++++ lib/rack/cache/entitystore.rb | 20 +++++++++++---- rack-cache.gemspec | 6 ++++- test/entitystore_test.rb | 44 ++++++++++++++++++--------------- test/metastore_test.rb | 46 +++++++++++++++++++---------------- test/spec_setup.rb | 4 +++ 7 files changed, 84 insertions(+), 47 deletions(-) diff --git a/.travis.yml b/.travis.yml index 74c886e..a0b16e3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,11 @@ language: ruby sudo: false cache: bundler +services: memcached rvm: - 2.0.0 - 2.1.7 - 2.2.3 + - jruby + diff --git a/CHANGES b/CHANGES index c220f6d..4ec2628 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,11 @@ +## Next + + * Fix support for JRuby + +## 1.3 / October 2015 + + * TODO + ## 1.2 / March 2012 * Fix a cookie leak vulnerability effecting large numbers of Rails 3.x installs: diff --git a/lib/rack/cache/entitystore.rb b/lib/rack/cache/entitystore.rb index b9494a0..a8e44a8 100644 --- a/lib/rack/cache/entitystore.rb +++ b/lib/rack/cache/entitystore.rb @@ -254,11 +254,21 @@ def initialize(server="localhost:11211", options={}) end end - def exist?(key) - cache.append(key, '') - true - rescue ::Memcached::NotStored - false + if RUBY_PLATFORM =~ /java/ + # see https://github.com/aurorafeint/jruby-memcached/issues/13 + def exist?(key) + cache.get(key, false) + true + rescue ::Memcached::NotFound + false + end + else + def exist?(key) + cache.append(key, '') + true + rescue ::Memcached::NotStored + false + end end def read(key) diff --git a/rack-cache.gemspec b/rack-cache.gemspec index 71a1c90..8f20c7d 100644 --- a/rack-cache.gemspec +++ b/rack-cache.gemspec @@ -12,7 +12,11 @@ Gem::Specification.new 'rack-cache', '1.3.0' do |s| s.add_dependency 'rack', '>= 0.4' s.add_development_dependency 'bacon' - s.add_development_dependency 'memcached' + if RUBY_PLATFORM =~ /java/ + s.add_development_dependency 'jruby-memcached' + else + s.add_development_dependency 'memcached' + end s.add_development_dependency 'dalli' s.add_development_dependency 'bump' s.add_development_dependency 'rake' diff --git a/test/entitystore_test.rb b/test/entitystore_test.rb index 5997443..6aa74ac 100644 --- a/test/entitystore_test.rb +++ b/test/entitystore_test.rb @@ -195,18 +195,20 @@ def sha_like? behaves_like 'A Rack::Cache::EntityStore Implementation' end - describe 'options parsing' do - before do - uri = URI.parse("memcached://#{ENV['MEMCACHED']}/obj_ns1?show_backtraces=true") - @memcached_metastore = Rack::Cache::MetaStore::MemCached.resolve uri - end + not_java 'uses an implementation detail of the memcached gem' do + describe 'options parsing' do + before do + uri = URI.parse("memcached://#{ENV['MEMCACHED']}/obj_ns1?show_backtraces=true") + @memcached_metastore = Rack::Cache::MetaStore::MemCached.resolve uri + end - it 'passes options from uri' do - @memcached_metastore.cache.instance_variable_get(:@options)[:show_backtraces].should.equal true - end + it 'passes options from uri' do + @memcached_metastore.cache.instance_variable_get(:@options)[:show_backtraces].should.equal true + end - it 'takes namespace into account' do - @memcached_metastore.cache.instance_variable_get(:@options)[:prefix_key].should.equal 'obj_ns1' + it 'takes namespace into account' do + @memcached_metastore.cache.instance_variable_get(:@options)[:prefix_key].should.equal 'obj_ns1' + end end end end @@ -224,18 +226,20 @@ def sha_like? behaves_like 'A Rack::Cache::EntityStore Implementation' end - describe 'options parsing' do - before do - uri = URI.parse("memcached://#{ENV['MEMCACHED']}/obj_ns1?show_backtraces=true") - @dalli_metastore = Rack::Cache::MetaStore::Dalli.resolve uri - end + not_java 'uses an implementation detail of the memcached gem' do + describe 'options parsing' do + before do + uri = URI.parse("memcached://#{ENV['MEMCACHED']}/obj_ns1?show_backtraces=true") + @dalli_metastore = Rack::Cache::MetaStore::Dalli.resolve uri + end - it 'passes options from uri' do - @dalli_metastore.cache.instance_variable_get(:@options)[:show_backtraces].should.equal true - end + it 'passes options from uri' do + @dalli_metastore.cache.instance_variable_get(:@options)[:show_backtraces].should.equal true + end - it 'takes namespace into account' do - @dalli_metastore.cache.instance_variable_get(:@options)[:namespace].should.equal 'obj_ns1' + it 'takes namespace into account' do + @dalli_metastore.cache.instance_variable_get(:@options)[:namespace].should.equal 'obj_ns1' + end end end end diff --git a/test/metastore_test.rb b/test/metastore_test.rb index 775c4b9..6343af8 100644 --- a/test/metastore_test.rb +++ b/test/metastore_test.rb @@ -286,18 +286,20 @@ def purge(*args); nil end behaves_like 'A Rack::Cache::MetaStore Implementation' end - describe 'options parsing' do - before do - uri = URI.parse("memcached://#{ENV['MEMCACHED']}/meta_ns1?show_backtraces=true") - @memcached_metastore = Rack::Cache::MetaStore::MemCached.resolve uri - end + not_java 'uses an implementation detail of the memcached gem' do + describe 'options parsing' do + before do + uri = URI.parse("memcached://#{ENV['MEMCACHED']}/meta_ns1?show_backtraces=true") + @memcached_metastore = Rack::Cache::MetaStore::MemCached.resolve uri + end - it 'passes options from uri' do - @memcached_metastore.cache.instance_variable_get(:@options)[:show_backtraces].should.equal true - end + it 'passes options from uri' do + @memcached_metastore.cache.instance_variable_get(:@options)[:show_backtraces].should.equal true + end - it 'takes namespace into account' do - @memcached_metastore.cache.instance_variable_get(:@options)[:prefix_key].should.equal 'meta_ns1' + it 'takes namespace into account' do + @memcached_metastore.cache.instance_variable_get(:@options)[:prefix_key].should.equal 'meta_ns1' + end end end end @@ -313,18 +315,20 @@ def purge(*args); nil end behaves_like 'A Rack::Cache::MetaStore Implementation' end - describe 'options parsing' do - before do - uri = URI.parse("memcached://#{ENV['MEMCACHED']}/meta_ns1?show_backtraces=true") - @dalli_metastore = Rack::Cache::MetaStore::Dalli.resolve uri - end + not_java 'uses an implementation detail of the memcached gem' do + describe 'options parsing' do + before do + uri = URI.parse("memcached://#{ENV['MEMCACHED']}/meta_ns1?show_backtraces=true") + @dalli_metastore = Rack::Cache::MetaStore::Dalli.resolve uri + end - it 'passes options from uri' do - @dalli_metastore.cache.instance_variable_get(:@options)[:show_backtraces].should.equal true - end + it 'passes options from uri' do + @dalli_metastore.cache.instance_variable_get(:@options)[:show_backtraces].should.equal true + end - it 'takes namespace into account' do - @dalli_metastore.cache.instance_variable_get(:@options)[:namespace].should.equal 'meta_ns1' + it 'takes namespace into account' do + @dalli_metastore.cache.instance_variable_get(:@options)[:namespace].should.equal 'meta_ns1' + end end end end @@ -345,7 +349,7 @@ def put(key, value, ttl = nil) end describe 'GAEStore' do - before :each do + before do Rack::Cache::AppEngine::MC::Service.clear @store = Rack::Cache::MetaStore::GAEStore.new @entity_store = Rack::Cache::EntityStore::Heap.new diff --git a/test/spec_setup.rb b/test/spec_setup.rb index d518199..e7f3c9f 100644 --- a/test/spec_setup.rb +++ b/test/spec_setup.rb @@ -69,6 +69,10 @@ def need_java(forwhat) yield if RUBY_PLATFORM =~ /java/ end +def not_java(forwhat) + yield unless RUBY_PLATFORM =~ /java/ +end + require 'rack/cache' # Methods for constructing downstream applications / response