riak_sessions 0.1 → 0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/riak_sessions.rb +17 -23
- data/riak_sessions.gemspec +1 -1
- metadata +1 -1
data/lib/riak_sessions.rb
CHANGED
@@ -1,12 +1,9 @@
|
|
1
1
|
require 'rack/session/abstract/id'
|
2
2
|
require 'thread'
|
3
|
-
require 'base64'
|
4
|
-
require 'jiak'
|
5
3
|
|
6
4
|
module Rack
|
7
5
|
|
8
6
|
# The Rack::Session::Riak uses the open source document-oriented web database Riak as a sessions backend,
|
9
|
-
# it uses the Jiak ruby client library: http://hg.basho.com/riak/src/tip/client_lib/jiak.rb
|
10
7
|
# more info about Riak: http://riak.basho.com/
|
11
8
|
#
|
12
9
|
# Examples:
|
@@ -15,8 +12,10 @@ module Rack
|
|
15
12
|
|
16
13
|
module Session
|
17
14
|
class RiakPool
|
18
|
-
def initialize(
|
19
|
-
@
|
15
|
+
def initialize(host, port, options={})
|
16
|
+
@host= host
|
17
|
+
@port = port
|
18
|
+
@path ='/raw/riak_session'
|
20
19
|
end
|
21
20
|
|
22
21
|
def [](session_id)
|
@@ -24,11 +23,10 @@ module Rack
|
|
24
23
|
end
|
25
24
|
|
26
25
|
def get(session_id)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
rescue JiakException => e
|
26
|
+
req = Net::HTTP::Get.new([@path, session_id].join('/'))
|
27
|
+
res = Net::HTTP.start(@host, @port) { |http| http.request(req) }
|
28
|
+
return Marshal.load(res.body)
|
29
|
+
rescue Net::HTTPNotFound => e
|
32
30
|
return nil
|
33
31
|
end
|
34
32
|
|
@@ -37,19 +35,16 @@ module Rack
|
|
37
35
|
end
|
38
36
|
|
39
37
|
def set(session_id, data)
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
session = {'bucket'=>'rack_session', 'key'=>session_id, 'links'=>[]}
|
45
|
-
end
|
46
|
-
session['object'] = {'data'=>Base64.encode64(Marshal.dump(data))}
|
47
|
-
@riak.store(session)
|
38
|
+
req = Net::HTTP::Put.new([@path, session_id].join('/'))
|
39
|
+
req.content_type = 'application/octet-stream'
|
40
|
+
req.body = Marshal.dump(data)
|
41
|
+
Net::HTTP.start(@host, @port) { |http| http.request(req) }
|
48
42
|
return true
|
49
43
|
end
|
50
44
|
|
51
45
|
def delete(session_id)
|
52
|
-
@
|
46
|
+
req = Net::HTTP::Delete.new([@path, session_id].join('/'))
|
47
|
+
Net::HTTP.start(@host, @port) { |http| http.request(req) }
|
53
48
|
end
|
54
49
|
end
|
55
50
|
|
@@ -58,15 +53,14 @@ module Rack
|
|
58
53
|
DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge(
|
59
54
|
:drop => false,
|
60
55
|
:riak_server => '127.0.0.1',
|
61
|
-
:riak_port => 8098
|
62
|
-
|
56
|
+
:riak_port => 8098
|
57
|
+
)
|
63
58
|
|
64
59
|
def initialize(app, options={})
|
65
60
|
super
|
66
61
|
|
67
62
|
@pool = RiakPool.new(@default_options[:riak_server],
|
68
|
-
@default_options[:riak_port]
|
69
|
-
@default_options[:riak_options])
|
63
|
+
@default_options[:riak_port])
|
70
64
|
@mutex = Mutex.new
|
71
65
|
end
|
72
66
|
|
data/riak_sessions.gemspec
CHANGED