rotary 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/rotary/serializer/string.rb +0 -1
- data/lib/rotary/serializer.rb +0 -1
- data/lib/rotary/storage/memory.rb +1 -0
- data/lib/rotary/storage/redis.rb +34 -3
- data/lib/rotary/version.rb +1 -1
- data/lib/rotary.rb +9 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e85fa11711aaad4a8bcf34825a90b0b18988be9
|
4
|
+
data.tar.gz: 09c77574e28dc21544ea6da5cca756aa4e9dca09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ad03e08f14b592110c2e6fe5977f5aef5d054b4478fa5748a8cfd6abf47ccf3f924446279b2cd59d71d0381a8c99cadfc1fe3b6395ac3202717439f654d617e
|
7
|
+
data.tar.gz: ed2dbb9e42f3a1392335e4e50795e3786d5c1b1b3e651e0291d9330855b399aeca4bfbc2e82ba31459a19822e2e6affc9028483202f617859d307f075bb1480e
|
data/README.md
CHANGED
@@ -0,0 +1,2 @@
|
|
1
|
+
# rotary [![Build Status](https://secure.travis-ci.org/dredozubov/rotary.png)](http://travis-ci.org/dredozubov/rotary?branch=master) [![Dependency Status](https://gemnasium.com/dredozubov/rotary.png)](https://gemnasium.com/dredozubov/rotary) [![Code Climate](https://codeclimate.com/github/dredozubov/rotary.png)](https://codeclimate.com/github/dredozubov/rotary)
|
2
|
+
Generic pool with pluggable backends for external storage. This way pool can be used by multiple application servers. It can be used to store sessions for external services.
|
data/lib/rotary/serializer.rb
CHANGED
data/lib/rotary/storage/redis.rb
CHANGED
@@ -4,6 +4,7 @@ require 'redis'
|
|
4
4
|
module Rotary
|
5
5
|
module Storage
|
6
6
|
class Redis
|
7
|
+
DEFAULT_PREFIX = 'rotary'.freeze
|
7
8
|
|
8
9
|
class Retry < Exception
|
9
10
|
end
|
@@ -12,10 +13,10 @@ module Rotary
|
|
12
13
|
::Redis.new
|
13
14
|
end
|
14
15
|
|
15
|
-
def initialize(connection:, ttl:, serializer:, prefix:
|
16
|
+
def initialize(connection:, ttl:, serializer:, prefix: DEFAULT_PREFIX)
|
16
17
|
@redis = connection
|
17
|
-
@prefix = "#{prefix}::"
|
18
18
|
@ttl = ttl # in seconds
|
19
|
+
@prefix = "#{prefix}::"
|
19
20
|
@serializer = serializer
|
20
21
|
@pool_list = "#{@prefix}pool"
|
21
22
|
end
|
@@ -57,6 +58,37 @@ module Rotary
|
|
57
58
|
@redis.del(@pool_list)
|
58
59
|
end
|
59
60
|
|
61
|
+
# Removes sessions, where ttl is bigger than threshold n.
|
62
|
+
def clean_older_than(n)
|
63
|
+
len = @redis.llen(@pool_list)
|
64
|
+
|
65
|
+
# It doesn't have to happen atomically.
|
66
|
+
# New session will be lpush'ed, we can easily check only
|
67
|
+
# N sessions from the right.
|
68
|
+
len.times do
|
69
|
+
session = @redis.rpop(@pool_list)
|
70
|
+
|
71
|
+
# We have no sessions left. It can happen.
|
72
|
+
break unless session
|
73
|
+
|
74
|
+
key = ttl_key(session)
|
75
|
+
ttl_marker = @redis.ttl(key)
|
76
|
+
|
77
|
+
# redis.rb returns -2 when key doesn't exist
|
78
|
+
no_ttl = ttl_marker == -2
|
79
|
+
next if no_ttl
|
80
|
+
|
81
|
+
old = ttl_marker < (@ttl - n)
|
82
|
+
if old
|
83
|
+
# delete ttl key
|
84
|
+
@redis.del(key)
|
85
|
+
else
|
86
|
+
# push back from the left side
|
87
|
+
@redis.lpush(@pool_list, session)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
60
92
|
protected
|
61
93
|
|
62
94
|
def ttl_key(obj)
|
@@ -65,4 +97,3 @@ module Rotary
|
|
65
97
|
end
|
66
98
|
end
|
67
99
|
end
|
68
|
-
|
data/lib/rotary/version.rb
CHANGED
data/lib/rotary.rb
CHANGED
@@ -64,6 +64,14 @@ module Rotary
|
|
64
64
|
@storage.push(obj)
|
65
65
|
end
|
66
66
|
|
67
|
+
# Removes all elements from pool, which means specified by block
|
68
|
+
# argument criteria.
|
69
|
+
def clean_older_than(n)
|
70
|
+
if storage.respond_to?(:clean_older_than)
|
71
|
+
storage.clean_older_than(n)
|
72
|
+
else
|
73
|
+
fail "#{storage.class}#clean_where not implemented"
|
74
|
+
end
|
75
|
+
end
|
67
76
|
end
|
68
77
|
end
|
69
|
-
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rotary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denis Redozubov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|