Skip to content

Commit 735015c

Browse files
karmakazeandrew
authored andcommitted
144867444998371 dual cookie redis split persistence adapter (#420)
* Use fakeredis for testing * require fakeredis manually * Add fakeredis as a dev dependency * v.1.5.0 * Add Dual Redis(logged-in)/cookie(logged-out) persistence adapter
1 parent 35dc214 commit 735015c

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 1.5.0 (June 8th, 2016)
2+
3+
Features:
4+
5+
- Add `expire_seconds:` TTL option to RedisAdapter (@karmakaze, #409)
6+
- Optional custom persistence adapter (@ndelage, #411)
7+
8+
Misc:
9+
10+
- Use fakeredis for testing (@andrew, #412)
11+
112
## 1.4.5 (June 7th, 2016)
213

314
Bugfixes:

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,25 @@ Options:
223223
* `namespace`: separate namespace to store these persisted values (default "persistence")
224224
* `expire_seconds`: sets TTL for user key. (if a user is in multiple experiments most recent update will reset TTL for all their assignments)
225225

226+
#### Dual Adapter
227+
228+
The Dual Adapter allows the use of different persistence adapters for logged-in and logged-out users. A common use case is to use Redis for logged-in users and Cookies for logged-out users.
229+
230+
```ruby
231+
cookie_adapter = Split::Persistence::CookieAdapter
232+
redis_adapter = Split::Persistence::RedisAdapter.with_config(
233+
lookup_by: -> (context) { context.send(:current_user).try(:id) },
234+
expire_seconds: 2592000)
235+
236+
Split.configure do |config|
237+
config.persistence = Split::Persistence::DualAdapter.with_config(
238+
logged_in: -> (context) { !context.send(:current_user).try(:id).nil? },
239+
logged_in_adapter: redis_adapter,
240+
logged_out_adapter: cookie_adapter)
241+
config.persistence_cookie_length = 2592000 # 30 days
242+
end
243+
```
244+
226245
#### Custom Adapter
227246

228247
Your custom adapter needs to implement the same API as existing adapters.

lib/split/persistence.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
2-
%w[session_adapter cookie_adapter redis_adapter].each do |f|
2+
%w[session_adapter cookie_adapter redis_adapter dual_adapter].each do |f|
33
require "split/persistence/#{f}"
44
end
55

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
module Split
3+
module Persistence
4+
class DualAdapter
5+
extend Forwardable
6+
def_delegators :@adapter, :keys, :[], :[]=, :delete
7+
8+
def initialize(context)
9+
if logged_in = self.class.config[:logged_in]
10+
else
11+
raise "Please configure :logged_in"
12+
end
13+
if logged_in_adapter = self.class.config[:logged_in_adapter]
14+
else
15+
raise "Please configure :logged_in_adapter"
16+
end
17+
if logged_out_adapter = self.class.config[:logged_out_adapter]
18+
else
19+
raise "Please configure :logged_out_adapter"
20+
end
21+
22+
if logged_in.call(context)
23+
@adapter = logged_in_adapter.new(context)
24+
else
25+
@adapter = logged_out_adapter.new(context)
26+
end
27+
end
28+
29+
def self.with_config(options={})
30+
self.config.merge!(options)
31+
self
32+
end
33+
34+
def self.config
35+
@config ||= {}
36+
end
37+
38+
end
39+
end
40+
end

lib/split/version.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
module Split
33
MAJOR = 1
4-
MINOR = 4
5-
PATCH = 5
4+
MINOR = 5
5+
PATCH = 0
66
VERSION = [MAJOR, MINOR, PATCH].join('.')
77
end

0 commit comments

Comments
 (0)