Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JRuby compatibility #19

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Publish to RubyGems
run: |
mkdir -p $HOME/.gem
Expand All @@ -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}}"
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions Dockerfile-jruby
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
9 changes: 7 additions & 2 deletions cache_store_redis.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion lib/cache_store_redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require 'cache_store_redis/version'
require 'redis'
require 'securerandom'
require 'oj'
require 'logger'

require_relative 'cache_store_redis/redis_connection'
Expand Down
16 changes: 14 additions & 2 deletions lib/cache_store_redis/redis_cache_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/cache_store_redis/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CacheStoreRedis
VERSION = '2.2.1'
VERSION = '2.3.0'
end
31 changes: 31 additions & 0 deletions spec/cache_store_redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Loading