redis_session 0.1.6 → 0.1.11

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cb9790be2c3f12dbe77b050d1a60c52895c9cc4721c99b50958fedc98fdd7f99
4
+ data.tar.gz: 4773c5684e9122430e0aa281a904f87e27b24e93d9fc87037763290b906eeabe
5
+ SHA512:
6
+ metadata.gz: fec16987d0755e5d9c93c441181dbfe6f9aeafd185485c3bc5d9388a249f2490d2a8ade67deac1b3eae0b1eb778c1065ff39c5c84cd46e95b5dfe16e17a97913
7
+ data.tar.gz: d8826059dd08837b9f6ced06df8f7cdad72db7e4779139707bd38100a3c847e9635567b8675c366f100da051fa907497ac1736378e4f62d9356fea5b8c501b25
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_session.gemspec
4
+ gemspec
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,71 @@
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
+ * find key and value based on custom lookup
26
+
27
+ ### Documentation
28
+
29
+ The source code is using rdoc.
30
+
31
+ Example:
32
+ --------
33
+ require 'redis_session'
34
+
35
+ session = Session::SessionClient.new(:prefix => 'example')
36
+
37
+ session.save('key', 'value') # save key with the value of value
38
+ # without expire, return true if successful
39
+ puts session.restore('key') # will return "value"
40
+
41
+ session.save('self_destruct', 'in', 10) # save the key self_destruct with
42
+ # the value of 'in', and will
43
+ # terminates in 10 seconds
44
+ sleep 10.1
45
+ session.restore('self_destruct') # returns empty hash
46
+ session.restore('self_destruct', nil) # returns default value of nil
47
+
48
+ session.save('boom', { :bomb => 'ball' }, 10) # saving a ruby object
49
+ puts session.ttl('boom') # should return the seconds left to the key to live
50
+ # or -1
51
+
52
+ session.expire('key', 60) # the key will be gone in 60 seconds
53
+ puts session.restore('key') # prints 'value'
54
+
55
+ puts 'has value' if session.value? 'key' # check if key has a value
56
+
57
+ session.delete('key') # deleted it before time :)
58
+ # it's alias to remove
59
+
60
+ puts 'still here' if session.key? 'key' # do we have the key ?
61
+ ret = session.scan_by do |x|
62
+ next unless x.kind_of? Hash
63
+ next unless x.key? :click
64
+
65
+ x[:click] == true
66
+ end
67
+
68
+ LICENSE
69
+ -------
70
+ The following library is brought to you using MIT license.
71
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
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
- begin
24
- require 'redis'
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
  #
@@ -136,7 +132,9 @@ module Session
136
132
  @redis.set(a_key, a_data)
137
133
  end
138
134
  true
139
- rescue
135
+ rescue Redis::BaseConnectionError
136
+ raise
137
+ rescue Exception
140
138
  false
141
139
  end
142
140
 
@@ -154,7 +152,9 @@ module Session
154
152
  a_key = make_key(key)
155
153
  data = @redis.get(a_key)
156
154
  data.nil? ? default : Marshal.load(data)
157
- rescue
155
+ rescue Redis::BaseConnectionError
156
+ raise
157
+ rescue Exception
158
158
  default
159
159
  end
160
160
 
@@ -171,7 +171,9 @@ module Session
171
171
  def expire(key, ttl)
172
172
  a_key = make_key(key)
173
173
  @redis.expire(a_key, ttl)
174
- rescue
174
+ rescue Redis::BaseConnectionError
175
+ raise
176
+ rescue Exception
175
177
  false
176
178
  end
177
179
 
@@ -187,7 +189,10 @@ module Session
187
189
  def ttl(key)
188
190
  a_key = make_key(key)
189
191
  @redis.ttl(a_key)
190
- rescue
192
+
193
+ rescue Redis::BaseConnectionError
194
+ raise
195
+ rescue Exception
191
196
  -1
192
197
  end
193
198
 
@@ -203,7 +208,10 @@ module Session
203
208
  def remove(key)
204
209
  a_key = make_key(key)
205
210
  @redis.del(a_key)
206
- rescue
211
+
212
+ rescue Redis::BaseConnectionError
213
+ raise
214
+ rescue Exception
207
215
  false
208
216
  end
209
217
 
@@ -219,7 +227,10 @@ module Session
219
227
  def key?(key)
220
228
  a_key = make_key(key)
221
229
  @redis.exists a_key
222
- rescue
230
+
231
+ rescue Redis::BaseConnectionError
232
+ raise
233
+ rescue Exception
223
234
  false
224
235
  end
225
236
 
@@ -235,7 +246,10 @@ module Session
235
246
  def value?(key)
236
247
  a_key = make_key(key)
237
248
  @redis.get(a_key) != nil
238
- rescue
249
+
250
+ rescue Redis::BaseConnectionError
251
+ raise
252
+ rescue Exception
239
253
  false
240
254
  end
241
255
 
@@ -250,6 +264,50 @@ module Session
250
264
 
251
265
  alias :delete :remove
252
266
 
267
+ ##
268
+ #
269
+ # scan for partial value in redis
270
+ # Using a block you can create your own criteria for lookup:
271
+ #
272
+ # Example:
273
+ # let's say there is a key named "foo", and the value is a hash: {:click=>true, :reading=>0}
274
+ #
275
+ # ret = session.scan_by do |x|
276
+ # next unless x.kind_of? Hash
277
+ # next unless x.key? :click
278
+ #
279
+ # x[:click] == true
280
+ # end
281
+ #
282
+ # => {"foo"=>{:click=>true, :reading=>0}}
283
+ #
284
+ # If found return a hash of key => value
285
+ # If not found, return nil
286
+ #
287
+ def scan_by(&block)
288
+ key = ''
289
+ value = ''
290
+ @redis.keys('*').each do |x|
291
+ next unless @redis.type(x) == 'string'
292
+
293
+ value = Marshal.load(@redis.get(x)) rescue next # not really a ruby object
294
+ if yield value
295
+ key = x
296
+ break
297
+ end
298
+ end
299
+
300
+ return nil if key.empty?
301
+
302
+ { key => value }
303
+
304
+ rescue Redis::BaseConnectionError
305
+ raise
306
+ rescue Exception #=> e
307
+ # puts "exception: #{e}\n#{e.backtrace.join("\n")}"
308
+ nil
309
+ end
310
+
253
311
  private
254
312
  ##
255
313
  #
@@ -0,0 +1,4 @@
1
+ module Session
2
+
3
+ VERSION = '0.1.11'
4
+ end
@@ -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', '>= 2.2.10'
36
+ spec.add_development_dependency 'rake', '>= 12.3.3'
37
+ spec.add_development_dependency 'rspec', '~> 3.0'
38
+ end
metadata CHANGED
@@ -1,89 +1,112 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_session
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
5
- prerelease:
4
+ version: 0.1.11
6
5
  platform: ruby
7
6
  authors:
8
7
  - Ido Kanner
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-01-03 00:00:00.000000000 Z
11
+ date: 2021-05-25 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: 2.2.10
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.10
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 12.3.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 12.3.3
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: ! 'A session like handler of data using the Redis Database.
47
-
48
- The session is built with mindset that non web applications
49
-
50
- can use it just as well as web applications.
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.gemsepc
86
+ - lib/redis_session/version.rb
60
87
  - license
61
- - spec/add_restore_spec.rb
88
+ - redis_session.gemspec
62
89
  homepage: https://github.com/ik5/redis_session
63
90
  licenses:
64
91
  - MIT
65
- post_install_message:
92
+ metadata: {}
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
- rubyforge_project:
83
- rubygems_version: 1.8.23
84
- signing_key:
85
- specification_version: 3
108
+ rubygems_version: 3.2.15
109
+ signing_key:
110
+ specification_version: 4
86
111
  summary: A session handler using Redis DB
87
- test_files:
88
- - spec/add_restore_spec.rb
89
- has_rdoc:
112
+ test_files: []
@@ -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
-
@@ -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