frivol 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.6
1
+ 0.1.7
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{frivol}
8
- s.version = "0.1.6"
8
+ s.version = "0.1.7"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marc Heiligers"]
12
- s.date = %q{2010-10-22}
12
+ s.date = %q{2010-11-01}
13
13
  s.description = %q{Simple Redis backed temporary storage intended primarily for use with ActiveRecord models to provide caching}
14
14
  s.email = %q{marc@eternal.co.za}
15
15
  s.extra_rdoc_files = [
@@ -241,7 +241,12 @@ module Frivol
241
241
 
242
242
  def self.store_counter(instance, counter, value)
243
243
  key = instance.send(:storage_key, counter)
244
+ is_new = !Frivol::Config.redis.exists(key)
244
245
  Frivol::Config.redis[key] = value
246
+ if is_new
247
+ time = instance.class.storage_expiry(counter)
248
+ Frivol::Config.redis.expire(key, time) if time != Frivol::NEVER_EXPIRE
249
+ end
245
250
  end
246
251
 
247
252
  def self.retrieve_counter(instance, counter, default)
@@ -29,6 +29,10 @@ class Redis
29
29
  @expires[key] = Time.now + time
30
30
  end
31
31
 
32
+ def exists(key)
33
+ @storage.key? key
34
+ end
35
+
32
36
  def flushdb
33
37
  @storage = {}
34
38
  end
@@ -2,7 +2,7 @@ require 'helper'
2
2
 
3
3
  class TestFrivol < Test::Unit::TestCase
4
4
  def setup
5
- # fake_redis # Comment out this line to test against a real live Redis
5
+ fake_redis # Comment out this line to test against a real live Redis
6
6
  Frivol::Config.redis_config = { :thread_safe => true } # This will connect to a default Redis setup, otherwise set to { :host => "localhost", :port => 6379 }, for example
7
7
  Frivol::Config.redis.flushdb
8
8
  end
@@ -258,6 +258,34 @@ class TestFrivol < Test::Unit::TestCase
258
258
  assert_equal 11, t.increment_blue
259
259
  assert_equal 11, t.load_blue
260
260
  end
261
+
262
+ should "set expiry on counters" do
263
+ class SetExpireCounterTestClass < TestClass
264
+ storage_bucket :sheep_counter, :counter => true, :expires_in => 0.5
265
+ end
266
+ t = SetExpireCounterTestClass.new
267
+ assert_equal 0.5, SetExpireCounterTestClass.storage_expiry(:sheep_counter)
268
+ end
269
+
270
+ should "expire a counter bucket" do
271
+ class ExpireCounterTestClass < TestClass
272
+ storage_bucket :kitten_grave, :counter => true, :expires_in => 0.5
273
+
274
+ def bury_kittens
275
+ store_kitten_grave 10
276
+ end
277
+
278
+ def peek_in_grave
279
+ retrieve_kitten_grave 0
280
+ end
281
+ end
282
+ k = ExpireCounterTestClass.new
283
+ k.bury_kittens
284
+ assert_equal 10, k.peek_in_grave
285
+ sleep(0.6)
286
+ assert_equal 0, k.peek_in_grave
287
+
288
+ end
261
289
 
262
290
  # Note: this test will fail from time to time using fake_redis because fake_redis is not thread safe
263
291
  should "have thread safe counters" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frivol
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 6
10
- version: 0.1.6
9
+ - 7
10
+ version: 0.1.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marc Heiligers
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-22 00:00:00 +02:00
18
+ date: 2010-11-01 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency