redis_support 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/redis_support/locks.rb +28 -18
  2. metadata +8 -17
@@ -16,6 +16,15 @@ module RedisSupport
16
16
  release_redis_lock( key_to_lock )
17
17
  end
18
18
 
19
+ # Throttle a block of code so it is only executed at most every
20
+ # `expiration` seconds. The block is skipped if it has been run
21
+ # more recently.
22
+ #
23
+ # Returns nothing
24
+ def redis_throttle( key_to_lock, expiration = 30 )
25
+ yield if acquire_redis_lock_nonblock( key_to_lock, expiration )
26
+ end
27
+
19
28
  # Acquire a lock on a key in our Redis database. This is a blocking
20
29
  # call. It sleeps until the lock has been successfully acquired.
21
30
  #
@@ -25,6 +34,19 @@ module RedisSupport
25
34
  # # do some stuff on my_key
26
35
  # release_redis_lock( key.my_key )
27
36
  #
37
+ # interval - sleep interval for checking the lock's status.
38
+ #
39
+ # Returns nothing.
40
+ def acquire_redis_lock( key_to_lock, expiration = 30, interval = 1 )
41
+ until acquire_redis_lock_nonblock( key_to_lock, expiration )
42
+ sleep interval
43
+ end
44
+ end
45
+
46
+ # Attempt to acquire a lock on a key in our Redis database.
47
+ #
48
+ # Returns true on success and false on failure
49
+ #
28
50
  # Described in detail here:
29
51
  #
30
52
  # http://code.google.com/p/redis/wiki/SetnxCommand
@@ -40,30 +62,19 @@ module RedisSupport
40
62
  # amount of time others will wait for you, not the amount of time
41
63
  # you will wait to acquire the lock.
42
64
  #
43
- # interval - sleep interval for checking the lock's status.
44
- #
45
- # Returns nothing.
46
- def acquire_redis_lock( key_to_lock, expiration = 30, interval = 1 )
65
+ def acquire_redis_lock_nonblock( key_to_lock, expiration = 30 )
47
66
  key = lock_key( key_to_lock )
48
- until redis.setnx key, timeout_i( expiration )
67
+ if redis.setnx key, timeout_i( expiration )
68
+ return true
69
+ else
49
70
  if redis.get( key ).to_i < Time.now.to_i
50
71
  old_timeout = redis.getset( key, timeout_i( expiration ) ).to_i
51
72
  if old_timeout < Time.now.to_i
52
- return # got it!
73
+ return true
53
74
  end
54
- else
55
- sleep interval
56
75
  end
57
76
  end
58
- end
59
-
60
- # Acquire a redis lock only if it can be acquired
61
- # is a nonblocking action
62
- #
63
- # Returns true on success and false on failure
64
- def acquire_redis_lock_nonblock( key_to_lock, expiration = 30 )
65
- key = lock_key( key_to_lock )
66
- redis.setnx key, timeout_i( expiration )
77
+ false
67
78
  end
68
79
 
69
80
  # See docs for acquire_redis_lock above
@@ -95,6 +106,5 @@ module RedisSupport
95
106
  def timeout_i( timeout )
96
107
  timeout.seconds.from_now.to_i
97
108
  end
98
-
99
109
  end
100
110
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis_support
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 0
9
- - 9
10
- version: 0.0.9
8
+ - 10
9
+ version: 0.0.10
11
10
  platform: ruby
12
11
  authors:
13
12
  - Brian P O'Rourke
@@ -16,25 +15,23 @@ autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2010-06-24 00:00:00 -07:00
18
+ date: 2010-07-01 00:00:00 -07:00
20
19
  default_executable:
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
22
+ name: redis
23
+ type: :runtime
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- hash: 31
29
28
  segments:
30
29
  - 1
31
30
  - 0
32
31
  - 4
33
32
  version: 1.0.4
34
- type: :runtime
35
- name: redis
36
33
  prerelease: false
37
- version_requirements: *id001
34
+ requirement: *id001
38
35
  description: "Module for adding redis functionality to classes: simple key namespacing and locking and connections"
39
36
  email: dolores@doloreslabs.com
40
37
  executables: []
@@ -48,8 +45,6 @@ files:
48
45
  - lib/redis_support/class_extensions.rb
49
46
  - lib/redis_support/locks.rb
50
47
  - README.md
51
- - test/helper.rb
52
- - test/test_redis_support.rb
53
48
  has_rdoc: true
54
49
  homepage: http://github.com/dolores/redis_support
55
50
  licenses: []
@@ -60,27 +55,23 @@ rdoc_options:
60
55
  require_paths:
61
56
  - lib
62
57
  required_ruby_version: !ruby/object:Gem::Requirement
63
- none: false
64
58
  requirements:
65
59
  - - ">="
66
60
  - !ruby/object:Gem::Version
67
- hash: 3
68
61
  segments:
69
62
  - 0
70
63
  version: "0"
71
64
  required_rubygems_version: !ruby/object:Gem::Requirement
72
- none: false
73
65
  requirements:
74
66
  - - ">="
75
67
  - !ruby/object:Gem::Version
76
- hash: 3
77
68
  segments:
78
69
  - 0
79
70
  version: "0"
80
71
  requirements: []
81
72
 
82
73
  rubyforge_project:
83
- rubygems_version: 1.3.7
74
+ rubygems_version: 1.3.6
84
75
  signing_key:
85
76
  specification_version: 3
86
77
  summary: A Redis Support module