diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 53e1722..9b6b513 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -10,7 +10,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Publish to RubyGems run: | mkdir -p $HOME/.gem @@ -21,3 +20,21 @@ jobs: gem push cache_store_redis-*.gem env: GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}" + + - uses: actions/checkout@v3 + - name: Set up JRuby + uses: ruby/setup-ruby@v1 + with: + ruby-version: jruby + bundler-cache: true # runs 'bundle install' and caches installed gems automatically + - run: jruby -S gem build cache_store_redis.gemspec + + - name: Publish to RubyGems + run: | + mkdir -p $HOME/.gem + touch $HOME/.gem/credentials + chmod 0600 $HOME/.gem/credentials + printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials + gem push cache_store_redis-*.gem + env: + GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}" diff --git a/.travis.yml b/.travis.yml index c158e89..b23edbb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ services: - redis-server rvm: - 2.3.5 + - jruby cache: bundler before_script: - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 diff --git a/Dockerfile-jruby b/Dockerfile-jruby new file mode 100644 index 0000000..89c4019 --- /dev/null +++ b/Dockerfile-jruby @@ -0,0 +1,12 @@ +FROM jruby + +RUN apk add --no-cache --update bash + +# Create application directory and set it as the WORKDIR. +ENV APP_HOME /code +RUN mkdir -p $APP_HOME +WORKDIR $APP_HOME + +COPY . $APP_HOME + +RUN jruby -S bundle install diff --git a/Gemfile b/Gemfile index 8d06a4c..3dfaa45 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,6 @@ source 'https://rubygems.org' # Specify your gem's dependencies in cache_store_redis.gemspec gemspec -gem 'oj', '3.6.10' +gem 'oj', '3.6.10' unless RUBY_PLATFORM == 'java' gem 'pry' gem 'simplecov', '< 0.18.0' diff --git a/cache_store_redis.gemspec b/cache_store_redis.gemspec index 49fce43..3f1ac0d 100644 --- a/cache_store_redis.gemspec +++ b/cache_store_redis.gemspec @@ -19,12 +19,17 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.add_development_dependency 'bundler' - spec.add_development_dependency 'pry-byebug' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec' spec.add_development_dependency 'simplecov' spec.add_development_dependency 'timecop' - spec.add_dependency 'oj' + if RUBY_PLATFORM =~ /java/ + spec.platform = 'java' + else + spec.add_development_dependency 'pry-byebug' + spec.add_dependency 'oj' + end + spec.add_dependency 'redis' end diff --git a/lib/cache_store_redis.rb b/lib/cache_store_redis.rb index 0e3e017..babfc6b 100644 --- a/lib/cache_store_redis.rb +++ b/lib/cache_store_redis.rb @@ -4,7 +4,6 @@ require 'cache_store_redis/version' require 'redis' require 'securerandom' -require 'oj' require 'logger' require_relative 'cache_store_redis/redis_connection' diff --git a/lib/cache_store_redis/redis_cache_store.rb b/lib/cache_store_redis/redis_cache_store.rb index e5714d2..7e3f130 100644 --- a/lib/cache_store_redis/redis_cache_store.rb +++ b/lib/cache_store_redis/redis_cache_store.rb @@ -4,6 +4,10 @@ class RedisCacheStore DEFAULT_TTL = 3_600 def initialize(namespace = nil, config = nil) + unless RUBY_PLATFORM == 'java' + require 'oj' + end + @connection_pool = RedisConnectionPool.new(namespace, config) @namespace = namespace @@ -143,11 +147,19 @@ def ping private def serialize(object) - Oj.dump(object) + if RUBY_PLATFORM == 'java' + Marshal::dump(object) + else + Oj.dump(object) + end end def deserialize(object) - Oj.load(object) + if RUBY_PLATFORM == 'java' + Marshal::load(object) + else + Oj.load(object) + end end def build_key(key) diff --git a/lib/cache_store_redis/version.rb b/lib/cache_store_redis/version.rb index f4444b7..aef709e 100644 --- a/lib/cache_store_redis/version.rb +++ b/lib/cache_store_redis/version.rb @@ -1,3 +1,3 @@ module CacheStoreRedis - VERSION = '2.2.1' + VERSION = '2.3.0' end diff --git a/spec/cache_store_redis_spec.rb b/spec/cache_store_redis_spec.rb index 753cac4..e9ac4e4 100644 --- a/spec/cache_store_redis_spec.rb +++ b/spec/cache_store_redis_spec.rb @@ -42,6 +42,19 @@ expect(v).to eq(value) end + it 'should add a string to the cache store and retrieve it (java platform)' do + original_platform = RUBY_PLATFORM + key = SecureRandom.uuid + value = 'value123' + Object.const_set(:RUBY_PLATFORM, 'java') + subject.public_send(method_name, key, value) + + v = subject.get(key) + + expect(v).to eq(value) + Object.const_set(:RUBY_PLATFORM, original_platform) + end + it 'should add an object to the cache store and retrieve it' do key = SecureRandom.uuid value = TestObject.new @@ -57,6 +70,24 @@ expect(v.numeric).to eq(value.numeric) end + it 'should add an object to the cache store and retrieve it (java platform)' do + original_platform = RUBY_PLATFORM + key = SecureRandom.uuid + value = TestObject.new + value.text = 'abc123' + value.numeric = 123 + Object.const_set(:RUBY_PLATFORM, 'java') + + subject.public_send(method_name, key, value) + + v = subject.get(key) + + expect(v.class).to eq(TestObject) + expect(v.text).to eq(value.text) + expect(v.numeric).to eq(value.numeric) + Object.const_set(:RUBY_PLATFORM, original_platform) + end + it 'should run the hydration block when the requested key does not exist in the cache' do key = SecureRandom.uuid diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1251a16..c1961f6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,7 +5,7 @@ require 'timecop' require 'pry' -require 'pry-byebug' +require 'pry-byebug' unless RUBY_PLATFORM == 'java' require_relative 'test_object' require_relative '../lib/cache_store_redis'