Skip to content
This repository was archived by the owner on Feb 11, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9e077d4
Set minimum config for rubocop to show no offenses
jpawlyn May 30, 2017
1435dc3
Add work around for rubocop bug
jpawlyn May 30, 2017
9701a8d
Merge pull request #92 from jpawlyn/rubocop-fix
roidrage Jun 24, 2017
bcf7792
Update Actionpack version to include all patches for 5.1 (#91)
Jesterovskiy Aug 28, 2017
889488f
Travis: use jruby-9.1.13.0 (#86)
olleolleolle Oct 10, 2017
547d990
Linting (#98)
olleolleolle Oct 18, 2017
bf8be56
Add release 0.9.2 version and changes
Jesterovskiy Oct 31, 2017
1e7a463
Fix linter issues (#99)
Jesterovskiy Oct 31, 2017
46f2601
Travis: jruby-9.1.15.0 (#100)
olleolleolle Dec 8, 2017
022c61e
Bump Redis dependency (#101)
n-rodriguez Feb 9, 2018
6a0f56d
Rails 5.2 (#103)
nebolsin Apr 14, 2018
bd8613b
Decode sesssion with_indifferent_access if defined ActiveSupport (#102)
iggant Apr 14, 2018
7aa1df4
Add release 0.10.0 version and changes
Jesterovskiy Apr 14, 2018
79a174e
Travis: use jruby-9.1.17.0 (#104)
olleolleolle Apr 25, 2018
b788645
Travis: jruby-9.2.0.0 (#106)
olleolleolle May 24, 2018
30575bc
Update Readme
Jesterovskiy Jun 20, 2018
00d2b0a
Merge branch 'master' of https://github.com/roidrage/redis-session-store
Jesterovskiy Jun 20, 2018
1e52073
Add new ruby versions to Travis config
Jesterovskiy Aug 9, 2018
428b57e
Add :ttl configuration option (#84)
pkarman Aug 9, 2018
f553ffa
Fix Travis ClassLength
Jesterovskiy Aug 9, 2018
1297dcf
Add 0.11.0 release version
Jesterovskiy Aug 13, 2018
1ae549a
Fix Gemspec Deprecation (#109)
Oct 11, 2018
fac8243
Remove upper dependency for actionpack (#110)
iggant Oct 15, 2018
ce30362
CI matrix: Add 2.6.3, update to jruby-9.2.8.0 (#111)
olleolleolle Aug 19, 2019
1577206
Release v0.11.1 (#113)
larouxn Aug 22, 2019
c356bc2
Update travis.yml Ruby versions (#114)
larouxn Oct 28, 2019
b317fe4
CI: Use jruby-9.2.9.0 (#115)
olleolleolle Nov 10, 2019
37fd169
Fix rubocop warnings (#117)
y-yagi Dec 23, 2019
d4a7d0c
Update key existence check to use newer #exists? if available (#119)
zachmargolis Jul 22, 2020
38068f1
Add 0.11.2 release version
Jesterovskiy Jul 22, 2020
d5feb38
Add 0.11.3 release version
Jesterovskiy Jul 23, 2020
659346d
Fix build
Jesterovskiy Jul 23, 2020
d166f70
Another build fix
Jesterovskiy Jul 23, 2020
6155489
Merge remote-tracking branch '18f/master' into margolis-true-up-with-…
zachmargolis Nov 15, 2021
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
37 changes: 34 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
inherit_from: .rubocop_todo.yml
AllCops:
DisplayCopNames: true
Exclude:
- 'Rakefile'
- 'vendor/**/*'

FileName:
Naming/FileName:
Enabled: false

DoubleNegation:
Style/DoubleNegation:
Enabled: false

Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'

Layout/LineLength:
Max: 100

Metrics/ClassLength:
Max: 120

Security/MarshalLoad:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false

Style/PercentLiteralDelimiters:
Enabled: false

Style/SafeNavigation:
Enabled: false

Style/YodaCondition:
# temporary work around for rubocop bug 'An error occurred while Style/YodaCondition...' (v0.49.1)
Exclude:
- 'spec/redis_session_store_spec.rb'
12 changes: 0 additions & 12 deletions .rubocop_todo.yml

This file was deleted.

14 changes: 9 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
language: ruby
sudo: false
cache: bundler
rvm:
- 2.3.1
- jruby-9.1.1.0
- 2.3
- 2.4
- 2.5
- 2.6
- ruby-head
- jruby-9.2.12.0
matrix:
allow_failures:
- rvm: jruby-9.1.1.0
- rvm: ruby-head
- rvm: jruby-9.2.12.0
notifications:
email: false
deploy:
Expand All @@ -17,4 +21,4 @@ deploy:
on:
tags: true
repo: roidrage/redis-session-store
rvm: 2.3.1
rvm: 2.6.6
8 changes: 8 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Redis Session Store authors
- Edwin Cruz
- Gonçalo Silva
- Ian C. Anderson
- Jesse Doyle
- Jorge Pardiñas
- Justin McNally
- Mathias Meyer
Expand All @@ -15,3 +16,10 @@ Redis Session Store authors
- Olek Poplavsky
- Tim Lossen
- Todd Bealmear
- Aleksey Dashkevych
- Olle Jonsson
- Nicolas Rodriguez
- Sergey Nebolsin
- Anton Kolodii
- Peter Karman
- Zach Margolis
42 changes: 41 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,42 @@

## [Unreleased]

## [0.11.3] - 2020-07-23
### Fixed
- https://github.com/roidrage/redis-session-store/issues/121

## [0.11.2] - 2020-07-22
### Changed
- Silence deprecation warning when using with redis gem v4.2+

## [0.11.1] - 2019-08-22
### Changed
- Remove the `has_rdoc` parameter from the `.gemspec` file as it has been deprecated.
- Actionpack to '>= 3', remove upper dependency

## [0.11.0] - 2018-08-13
### Changed
- JRuby to jruby-9.2.0.0
- Travis Ruby support: 2.3.7, 2.4.4, 2.5.1

### Added
- :ttl configuration option

## [0.10.0] - 2018-04-14
### Changed
- JRuby to jruby-9.1.15.0
- Redis to '>= 3', '< 5'
- Actionpack to '>= 3', '< 6'
- Rake to 12

### Added
- with_indifferent_access if defined ActiveSupport

## [0.9.2] - 2017-10-31
### Changed
- Actionpack to 5.1
- Travis use jruby 9.1.13.0

## [0.9.1] - 2016-07-03
### Added
- More specific runtime dependencies
Expand Down Expand Up @@ -201,7 +237,11 @@
### Added
- first working version

[Unreleased]: https://github.com/roidrage/redis-session-store/compare/v0.9.1...HEAD
[Unreleased]: https://github.com/roidrage/redis-session-store/compare/v0.11.1...HEAD
[0.11.1]: https://github.com/roidrage/redis-session-store/compare/v0.11.0...v0.11.1
[0.11.0]: https://github.com/roidrage/redis-session-store/compare/v0.10.0...v0.11.0
[0.10.0]: https://github.com/roidrage/redis-session-store/compare/v0.9.2...v0.10.0
[0.9.2]: https://github.com/roidrage/redis-session-store/compare/v0.9.1...v0.9.2
[0.9.1]: https://github.com/roidrage/redis-session-store/compare/v0.9.0...v0.9.1
[0.9.0]: https://github.com/roidrage/redis-session-store/compare/v0.8.1...v0.9.0
[0.8.1]: https://github.com/roidrage/redis-session-store/compare/v0.8.0...v0.8.1
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ See `lib/redis-session-store.rb` for a list of valid options.
In your Rails app, throw in an initializer with the following contents:

``` ruby
My::Application.config.session_store :redis_session_store, {
Rails.application.config.session_store :redis_session_store, {
key: 'your_session_key',
redis: {
expire_after: 120.minutes, # cookie expiration
ttl: 120.minutes, # Redis expiration, defaults to 'expire_after'
key_prefix: 'myapp:session:',
url: 'redis://host:12345/2',
url: 'redis://localhost:6379/0',
}
}
```
Expand All @@ -50,7 +50,7 @@ If you want to handle cases where Redis is unavailable, a custom
callable handler may be provided as `on_redis_down`:

``` ruby
My::Application.config.session_store :redis_session_store, {
Rails.application.config.session_store :redis_session_store, {
# ... other options ...
on_redis_down: ->(e, env, sid) { do_something_will_ya!(e) }
redis: {
Expand All @@ -70,7 +70,7 @@ custom serializer:
* `CustomClass` - You can just pass the constant name of any class that responds to `.load` and `.dump`

``` ruby
My::Application.config.session_store :redis_session_store, {
Rails.application.config.session_store :redis_session_store, {
# ... other options ...
serializer: :hybrid
redis: {
Expand All @@ -89,7 +89,7 @@ custom callable handler may be provided as `on_session_load_error` which
will be given the error and the session ID.

``` ruby
My::Application.config.session_store :redis_session_store, {
Rails.application.config.session_store :redis_session_store, {
# ... other options ...
on_session_load_error: ->(e, sid) { do_something_will_ya!(e) }
redis: {
Expand All @@ -100,6 +100,10 @@ My::Application.config.session_store :redis_session_store, {

**Note** The session will *always* be destroyed when it cannot be loaded.

### Other notes

It returns with_indifferent_access if ActiveSupport is defined

## Rails 2 Compatibility

This gem is currently only compatible with Rails 3+. If you need
Expand Down
51 changes: 31 additions & 20 deletions lib/redis-session-store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
# Redis session storage for Rails, and for Rails only. Derived from
# the MemCacheStore code, simply dropping in Redis instead.
class RedisSessionStore < ActionDispatch::Session::AbstractStore
VERSION = '0.9.1'.freeze
VERSION = '0.11.3'.freeze
# Rails 3.1 and beyond defines the constant elsewhere
unless defined?(ENV_SESSION_OPTIONS_KEY)
if Rack.const_defined?(:RACK_SESSION_OPTIONS)
ENV_SESSION_OPTIONS_KEY = Rack::RACK_SESSION_OPTIONS
else
ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY
end
ENV_SESSION_OPTIONS_KEY = if Rack.release.split('.').first.to_i > 1
Rack::RACK_SESSION_OPTIONS
else
Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY
end
end

USE_INDIFFERENT_ACCESS = defined?(ActiveSupport).freeze
# ==== Options
# * +:key+ - Same as with the other cookie stores, key name
# * +:redis+ - A hash with redis-specific options
Expand All @@ -26,12 +27,12 @@ class RedisSessionStore < ActionDispatch::Session::AbstractStore
#
# ==== Examples
#
# My::Application.config.session_store :redis_session_store, {
# Rails.application.config.session_store :redis_session_store, {
# key: 'your_session_key',
# redis: {
# expire_after: 120.minutes,
# key_prefix: 'myapp:session:',
# url: 'redis://host:12345/2'
# url: 'redis://localhost:6379/0'
# },
# on_redis_down: ->(*a) { logger.error("Redis down! #{a.inspect}") }
# serializer: :hybrid # migrate from Marshal to JSON
Expand Down Expand Up @@ -66,14 +67,24 @@ def session_exists?(env)

!!(
value && !value.empty? &&
redis.exists(prefixed(value))
key_exists?(value)
)
rescue Errno::ECONNREFUSED, Redis::CannotConnectError => e
on_redis_down.call(e, env, value) if on_redis_down

true
end

def key_exists?(value)
if redis.respond_to?(:exists?)
# added in redis gem v4.2
redis.exists?(prefixed(value))
else
# older method, will return an integer starting in redis gem v4.3
redis.exists(prefixed(value))
end
end

def verify_handlers!
%w(on_redis_down on_session_load_error).each do |h|
next unless (handler = public_send(h)) && !handler.respond_to?(:call)
Expand All @@ -86,32 +97,32 @@ def prefixed(sid)
"#{default_options[:key_prefix]}#{sid}"
end

def get_session(env, sid)
unless sid && (session = load_session_from_redis(sid))
sid = generate_sid
session = {}
end
def session_default_values
[generate_sid, USE_INDIFFERENT_ACCESS ? {}.with_indifferent_access : {}]
end

[sid, session]
def get_session(env, sid)
sid && (session = load_session_from_redis(sid)) ? [sid, session] : session_default_values
rescue Errno::ECONNREFUSED, Redis::CannotConnectError => e
on_redis_down.call(e, env, sid) if on_redis_down
[generate_sid, {}]
session_default_values
end
alias find_session get_session

def load_session_from_redis(sid)
data = redis.get(prefixed(sid))
begin
data ? decode(data) : nil
rescue => e
rescue StandardError => e
destroy_session_from_sid(sid, drop: true)
on_session_load_error.call(e, sid) if on_session_load_error
nil
end
end

def decode(data)
serializer.load(data)
session = serializer.load(data)
USE_INDIFFERENT_ACCESS ? session.with_indifferent_access : session
end

def set_session(env, sid, session_data, options = nil)
Expand All @@ -121,10 +132,10 @@ def set_session(env, sid, session_data, options = nil)
else
redis.set(prefixed(sid), encode(session_data))
end
return sid
sid
rescue Errno::ECONNREFUSED, Redis::CannotConnectError => e
on_redis_down.call(e, env, sid) if on_redis_down
return false
false
end
alias write_session set_session

Expand Down
13 changes: 6 additions & 7 deletions redis-session-store.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ Gem::Specification.new do |gem|
gem.homepage = 'https://github.com/roidrage/redis-session-store'
gem.license = 'MIT'

gem.has_rdoc = true
gem.extra_rdoc_files = %w(LICENSE AUTHORS.md CONTRIBUTING.md)

gem.files = `git ls-files -z`.split("\x0")
gem.require_paths = %w(lib)
gem.version = File.read('lib/redis-session-store.rb')
.match(/^ VERSION = '(.*)'/)[1]

gem.add_runtime_dependency 'redis', '~> 3'
gem.add_runtime_dependency 'actionpack', '>= 3', '< 5.2'
gem.add_runtime_dependency 'actionpack', '>= 3', '< 7'
gem.add_runtime_dependency 'redis', '>= 3', '< 5'

gem.add_development_dependency 'fakeredis', '~> 0.5'
gem.add_development_dependency 'rake', '~> 11'
gem.add_development_dependency 'fakeredis', '~> 0.8'
gem.add_development_dependency 'rake', '~> 13'
gem.add_development_dependency 'rspec', '~> 3'
gem.add_development_dependency 'rubocop', '~> 0.41'
gem.add_development_dependency 'simplecov', '~> 0.11'
gem.add_development_dependency 'rubocop', '~> 0.81'
gem.add_development_dependency 'simplecov', '~> 0.17'
end
4 changes: 2 additions & 2 deletions spec/redis_session_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -519,15 +519,15 @@ def self.dump(_value)
end

context 'when callable' do
let(:options) { { :"#{h}" => ->(*) { !nil } } }
let(:options) { { "#{h}": ->(*) { true } } }

it 'does not explode at init' do
expect { store }.to_not raise_error
end
end

context 'when not callable' do
let(:options) { { :"#{h}" => 'herpderp' } }
let(:options) { { "#{h}": 'herpderp' } }

it 'explodes at init' do
expect { store }.to raise_error(ArgumentError)
Expand Down