redis_session 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +6 -0
- data/lib/redis_session.rb +33 -6
- data/lib/redis_session/version.rb +4 -0
- data/redis_session.gemspec +38 -0
- metadata +56 -32
- data/redis_session.gemsepc +0 -23
- data/spec/add_restore_spec.rb +0 -113
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ab9b19745b1be232115845bae3b81d42cdb5ad18
|
4
|
+
data.tar.gz: 8ae838ebc8c4d3b22310afcfd9572c2e49f4f003
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 690dde2c0244cba85323ab1ff12372bd4a20199c12623dcd2a06d03f98e26d26b312671d40b60ba5a407d358a2e540f0c8eb906d905c613c04d7a086a20cf421
|
7
|
+
data.tar.gz: ca4f9fb4cab1b36d868a0f8991ed9ba3f8624f0bd5e29657852413110b58eb73812d73786bb5081385f84b810871f49029608604e190c62be9c5f14be8c124e6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Ido Kanner
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Redis Session
|
2
|
+
|
3
|
+
Redis\_session is a ruby based library that allow every ruby application to
|
4
|
+
store information using Redis.
|
5
|
+
|
6
|
+
The design of the library was for disconnected non web based applications, but
|
7
|
+
it can be used for web based applications just as well.
|
8
|
+
|
9
|
+
By providing unique prefix for each session, you can distinguish between each
|
10
|
+
request.
|
11
|
+
|
12
|
+
## Current features
|
13
|
+
|
14
|
+
* Adding prefix to each session instance
|
15
|
+
* Expiring keys globally or specific keys
|
16
|
+
* Saving keys with values, and possible to add expire time in seconds
|
17
|
+
* Set expire time to existing keys (updating time for already expiring keys)
|
18
|
+
* Restoring values, and giving default values (by default if no value,
|
19
|
+
it returns an empty hash)
|
20
|
+
* Checking the remaining time of keys
|
21
|
+
* Checking if key exists
|
22
|
+
* Checking if key has a value
|
23
|
+
* Removing keys
|
24
|
+
* Changing the prefix using the prefix= method
|
25
|
+
|
26
|
+
### Documentation
|
27
|
+
|
28
|
+
The source code is using rdoc.
|
29
|
+
|
30
|
+
Example:
|
31
|
+
--------
|
32
|
+
require 'redis_session'
|
33
|
+
|
34
|
+
session = Session::SessionClient.new(:prefix => 'example')
|
35
|
+
|
36
|
+
session.save('key', 'value') # save key with the value of value
|
37
|
+
# without expire, return true if successful
|
38
|
+
puts session.restore('key') # will return "value"
|
39
|
+
|
40
|
+
session.save('self_destruct', 'in', 10) # save the key self_destruct with
|
41
|
+
# the value of 'in', and will
|
42
|
+
# terminates in 10 seconds
|
43
|
+
sleep 10.1
|
44
|
+
session.restore('self_destruct') # returns empty hash
|
45
|
+
session.restore('self_destruct', nil) # returns default value of nil
|
46
|
+
|
47
|
+
session.save('boom', { :bomb => 'ball' }, 10) # saving a ruby object
|
48
|
+
puts session.ttl('boom') # should return the seconds left to the key to live
|
49
|
+
# or -1
|
50
|
+
|
51
|
+
session.expire('key', 60) # the key will be gone in 60 seconds
|
52
|
+
puts session.restore('key') # prints 'value'
|
53
|
+
|
54
|
+
puts 'has value' if session.value? 'key' # check if key has a value
|
55
|
+
|
56
|
+
session.delete('key') # deleted it before time :)
|
57
|
+
# it's alias to remove
|
58
|
+
|
59
|
+
puts 'still here' if session.key? 'key' # do we have the key ?
|
60
|
+
|
61
|
+
LICENSE
|
62
|
+
-------
|
63
|
+
The following library is brought to you using MIT license.
|
64
|
+
|
data/Rakefile
ADDED
data/lib/redis_session.rb
CHANGED
@@ -20,12 +20,8 @@
|
|
20
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
21
|
# SOFTWARE.
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
rescue
|
26
|
-
require 'rubygems'
|
27
|
-
require 'redis'
|
28
|
-
end
|
23
|
+
require 'redis'
|
24
|
+
require_relative 'redis_session/version'
|
29
25
|
|
30
26
|
##
|
31
27
|
#
|
@@ -250,6 +246,37 @@ module Session
|
|
250
246
|
|
251
247
|
alias :delete :remove
|
252
248
|
|
249
|
+
##
|
250
|
+
#
|
251
|
+
# scan for partial value in redis
|
252
|
+
# a value
|
253
|
+
#
|
254
|
+
#
|
255
|
+
# If found return a hash of key => value
|
256
|
+
# If not found, return nil
|
257
|
+
#
|
258
|
+
def scan_by(&block)
|
259
|
+
key = ''
|
260
|
+
value = ''
|
261
|
+
@redis.keys('*').each do |x|
|
262
|
+
next unless @redis.type(x) == 'string'
|
263
|
+
|
264
|
+
value = Marshal.load(@redis.get(x)) rescue next # not really a ruby object
|
265
|
+
if yield value
|
266
|
+
key = x
|
267
|
+
break
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
return nil if key.empty?
|
272
|
+
|
273
|
+
{ key => value }
|
274
|
+
|
275
|
+
rescue Exception => e
|
276
|
+
puts "exception: #{e}\n#{e.backtrace.join("\n")}"
|
277
|
+
nil
|
278
|
+
end
|
279
|
+
|
253
280
|
private
|
254
281
|
##
|
255
282
|
#
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'redis_session/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'redis_session'
|
8
|
+
spec.version = Session::VERSION
|
9
|
+
spec.authors = ['Ido Kanner']
|
10
|
+
spec.email = ['idokan@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'A session handler using Redis DB'
|
13
|
+
spec.description = <<-EOF
|
14
|
+
A session like handler of data using the Redis Database.
|
15
|
+
The session is built with mindset that non web applications
|
16
|
+
can use it just as well as web applications.
|
17
|
+
EOF
|
18
|
+
spec.homepage = 'https://github.com/ik5/redis_session'
|
19
|
+
spec.license = 'MIT'
|
20
|
+
|
21
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
22
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
23
|
+
# if spec.respond_to?(:metadata)
|
24
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
25
|
+
# else
|
26
|
+
# raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
27
|
+
# end
|
28
|
+
|
29
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
spec.bindir = 'bin'
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ['lib']
|
33
|
+
|
34
|
+
spec.add_dependency 'redis'
|
35
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
36
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
37
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
38
|
+
end
|
metadata
CHANGED
@@ -1,89 +1,113 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redis_session
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.7
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Ido Kanner
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2016-08-18 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: redis
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
30
55
|
- !ruby/object:Gem::Dependency
|
31
56
|
name: rspec
|
32
57
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
58
|
requirements:
|
35
|
-
- -
|
59
|
+
- - "~>"
|
36
60
|
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
61
|
+
version: '3.0'
|
38
62
|
type: :development
|
39
63
|
prerelease: false
|
40
64
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
65
|
requirements:
|
43
|
-
- -
|
66
|
+
- - "~>"
|
44
67
|
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
46
|
-
description:
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
'
|
53
|
-
email: idokan@gmail.com
|
68
|
+
version: '3.0'
|
69
|
+
description: |2
|
70
|
+
A session like handler of data using the Redis Database.
|
71
|
+
The session is built with mindset that non web applications
|
72
|
+
can use it just as well as web applications.
|
73
|
+
email:
|
74
|
+
- idokan@gmail.com
|
54
75
|
executables: []
|
55
76
|
extensions: []
|
56
77
|
extra_rdoc_files: []
|
57
78
|
files:
|
79
|
+
- ".gitignore"
|
80
|
+
- ".rspec"
|
81
|
+
- Gemfile
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
58
85
|
- lib/redis_session.rb
|
59
|
-
- redis_session.
|
86
|
+
- lib/redis_session/version.rb
|
60
87
|
- license
|
61
|
-
-
|
88
|
+
- redis_session.gemspec
|
62
89
|
homepage: https://github.com/ik5/redis_session
|
63
90
|
licenses:
|
64
91
|
- MIT
|
92
|
+
metadata: {}
|
65
93
|
post_install_message:
|
66
94
|
rdoc_options: []
|
67
95
|
require_paths:
|
68
96
|
- lib
|
69
97
|
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
-
none: false
|
71
98
|
requirements:
|
72
|
-
- -
|
99
|
+
- - ">="
|
73
100
|
- !ruby/object:Gem::Version
|
74
101
|
version: '0'
|
75
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
-
none: false
|
77
103
|
requirements:
|
78
|
-
- -
|
104
|
+
- - ">="
|
79
105
|
- !ruby/object:Gem::Version
|
80
106
|
version: '0'
|
81
107
|
requirements: []
|
82
108
|
rubyforge_project:
|
83
|
-
rubygems_version:
|
109
|
+
rubygems_version: 2.5.1
|
84
110
|
signing_key:
|
85
|
-
specification_version:
|
111
|
+
specification_version: 4
|
86
112
|
summary: A session handler using Redis DB
|
87
|
-
test_files:
|
88
|
-
- spec/add_restore_spec.rb
|
89
|
-
has_rdoc:
|
113
|
+
test_files: []
|
data/redis_session.gemsepc
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
|
2
|
-
Gem::Specification.new do |s|
|
3
|
-
s.name = 'redis_session'
|
4
|
-
s.version = '0.1.6'
|
5
|
-
s.homepage = 'https://github.com/ik5/redis_session'
|
6
|
-
s.summary = 'A session handler using Redis DB'
|
7
|
-
s.description = <<EOF
|
8
|
-
A session like handler of data using the Redis Database.
|
9
|
-
The session is built with mindset that non web applications
|
10
|
-
can use it just as well as web applications.
|
11
|
-
EOF
|
12
|
-
|
13
|
-
s.files = Dir.glob('lib/**rb') +
|
14
|
-
['redis_session.gemsepc', 'license' ]
|
15
|
-
s.authors = ['Ido Kanner']
|
16
|
-
s.licenses = 'MIT'
|
17
|
-
s.email = 'idokan@gmail.com'
|
18
|
-
s.test_files = Dir.glob('spec/*rb')
|
19
|
-
s.add_dependency('redis')
|
20
|
-
s.add_development_dependency('rspec')
|
21
|
-
|
22
|
-
end
|
23
|
-
|
data/spec/add_restore_spec.rb
DELETED
@@ -1,113 +0,0 @@
|
|
1
|
-
require 'redis_session'
|
2
|
-
|
3
|
-
describe Session::SessionClient do
|
4
|
-
before do
|
5
|
-
@session = Session::SessionClient.new(:prefix => 'add_test')
|
6
|
-
end
|
7
|
-
after do
|
8
|
-
@session.remove('name')
|
9
|
-
end
|
10
|
-
|
11
|
-
context 'With no values' do
|
12
|
-
it 'should return empty hash' do
|
13
|
-
@session.restore('name').length.should equal(0)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'With saving values' do
|
18
|
-
it 'should restore the "name" with "session"' do
|
19
|
-
@session.save('name', { 'name' => 'session'})
|
20
|
-
restored = @session.restore('name')
|
21
|
-
restored.class.should equal Hash and restored.should eq({'name' => 'session'})
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context 'Having restoring non existed value with default' do
|
26
|
-
it 'should have value of false' do
|
27
|
-
val = @session.restore('non_existed', false)
|
28
|
-
val.should == false
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'should not have value of true' do
|
32
|
-
val = @session.restore('non_existed', false)
|
33
|
-
val.should_not == true
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'Checking if key and values exists' do
|
38
|
-
it 'should have existed key' do
|
39
|
-
@session.save('name', 1)
|
40
|
-
key = @session.key? 'name'
|
41
|
-
key.should == true
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'should have non exited key' do
|
45
|
-
key = @session.key? 'no_key'
|
46
|
-
key.should == false
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'should have existed value' do
|
50
|
-
@session.save('name', 1)
|
51
|
-
value = @session.value?('name')
|
52
|
-
value.should == true
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'should not have value' do
|
56
|
-
value = @session.value? 'no_key'
|
57
|
-
value.should == false
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
context 'Dealing with prefix' do
|
62
|
-
it 'should return the prefix' do
|
63
|
-
prefix = @session.prefix
|
64
|
-
prefix.should == 'add_test'
|
65
|
-
end
|
66
|
-
|
67
|
-
it 'should change the prefix' do
|
68
|
-
orig_prefix = @session.prefix
|
69
|
-
@session.prefix = orig_prefix + '_'
|
70
|
-
prefix = @session.prefix
|
71
|
-
prefix.should == orig_prefix + '_'
|
72
|
-
@session.prefix = orig_prefix
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
context 'Working with expire' do
|
77
|
-
it 'should have ttl of 5 seconds when saving' do
|
78
|
-
@session.save('with_ttl', 1, 5)
|
79
|
-
ttl = @session.ttl('with_ttl')
|
80
|
-
ttl.should == 5
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'ttl should not be set' do
|
84
|
-
@session.save('no_ttl', 1)
|
85
|
-
ttl = @session.ttl('no_ttl')
|
86
|
-
ttl.should == -1
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'should have ttl of 5 seconds when expiring' do
|
90
|
-
@session.save('no_ttl', 1)
|
91
|
-
@session.expire('no_ttl', 5)
|
92
|
-
ttl = @session.ttl('no_ttl')
|
93
|
-
ttl.should == 5
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'save with ttl, should not exists after 5 seconds' do
|
97
|
-
@session.save('with_ttl', 1, 5)
|
98
|
-
|
99
|
-
sleep 5.1
|
100
|
-
val = @session.value?('with_ttl')
|
101
|
-
val.should == false
|
102
|
-
end
|
103
|
-
|
104
|
-
it 'expire with of 5 seconds, should not exists after it' do
|
105
|
-
@session.save('no_ttl', 1)
|
106
|
-
@session.expire('no_ttl', 5)
|
107
|
-
sleep 5.1
|
108
|
-
val = @session.value?('no_ttl')
|
109
|
-
val.should == false
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|