Skip to content

Commit

Permalink
Added support for JRuby.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Oct 7, 2015
1 parent b709199 commit f6de650
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
language: ruby
sudo: false
cache: bundler
services: memcached

rvm:
- 2.0.0
- 2.1.7
- 2.2.3
- jruby

8 changes: 8 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
20 changes: 15 additions & 5 deletions lib/rack/cache/entitystore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion rack-cache.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
44 changes: 24 additions & 20 deletions test/entitystore_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
46 changes: 25 additions & 21 deletions test/metastore_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/spec_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f6de650

Please sign in to comment.