ohm-expire 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +50 -1
  2. data/lib/ohm/expire.rb +10 -4
  3. data/test/expire-test.rb +11 -6
  4. metadata +1 -1
data/README.md CHANGED
@@ -1,4 +1,53 @@
1
1
  ohm-expire
2
2
  ==========
3
3
 
4
- Ohm plugin that exposes ttl control over modules
4
+ Ohm plugin that exposes ttl control over modules
5
+
6
+ Getting started
7
+ ---------------
8
+
9
+ Install gem by running:
10
+
11
+ $ [sudo] gem install ohm-expire
12
+
13
+ On your source code add:
14
+
15
+ require "ohm/expire"
16
+
17
+ After this you need to tell your Ohm Model that it needs to use this extension. Here is an example of a module:
18
+
19
+ class Model < Ohm::Model
20
+
21
+ include Ohm::Expire
22
+
23
+ TTL = 5
24
+
25
+ attribute :hash
26
+ index :hash
27
+
28
+ expire Model::TTL
29
+
30
+ attribute :data
31
+ end
32
+
33
+ This will create this Ohm Model class with an expire of 5 seconds
34
+
35
+ Updating TTL
36
+ ------------
37
+
38
+ It's easy to update the TTL after creating a Model.
39
+ There is also the possibility to simple update the ttl with the default value (the one that was defined on the Model)
40
+
41
+ Taking the example above:
42
+
43
+ m = Model.create(:hash => "123")
44
+ m.update_ttl 30 # Updated to 30 seconds
45
+ m.update_ttl # Updated to 5 seconds (Model::TTL)
46
+
47
+ Getting TTL
48
+ -----------
49
+
50
+ To get the TTL of a given model:
51
+
52
+ m = Model.create(:hash => "123")
53
+ m.get_ttl
data/lib/ohm/expire.rb CHANGED
@@ -49,6 +49,7 @@ module Ohm
49
49
  #
50
50
  # * self
51
51
  def create(*args)
52
+ args.first.merge!(:_default_expire => @expire)
52
53
  object = super(*args)
53
54
  if !object.new? && @expire
54
55
  Ohm.redis.expire(object.key, @expire)
@@ -64,11 +65,14 @@ module Ohm
64
65
  # Extends Ohm::Model with new methods
65
66
  class Model
66
67
 
68
+ attribute :_default_expire
69
+
67
70
  # Update the ttl of a given Model
68
71
  #
69
72
  # ==== Attributes
70
73
  #
71
- # * +model <i>Ohm::Model</i>+ - A Ohm model that will be used to update its ttl
74
+ # * +model <i>Ohm::Model</i>+ - A Ohm model that will be used to update its ttl. If nil, value will fallback
75
+ # to default expire
72
76
  # * +new_ttl <i>Fixnum</i>+ - The new expire amount
73
77
  #
74
78
  # ==== Returns
@@ -79,7 +83,7 @@ module Ohm
79
83
  #
80
84
  # d = Model.create(:hash => "123")
81
85
  # Model.update_ttl(d, 30)
82
- def self.update_ttl(model, new_ttl)
86
+ def self.update_ttl(model, new_ttl=nil)
83
87
  unless model.respond_to? :update_ttl
84
88
  model.update_ttl new_ttl
85
89
  end
@@ -89,7 +93,7 @@ module Ohm
89
93
  #
90
94
  # ==== Attributes
91
95
  #
92
- # * +new_ttl <i>Fixnum</i>+ - The new expire amount
96
+ # * +new_ttl <i>Fixnum</i>+ - The new expire amount. If nil, value will fallback to default expire
93
97
  #
94
98
  # ==== Returns
95
99
  #
@@ -99,7 +103,9 @@ module Ohm
99
103
  #
100
104
  # d = Model.create(:hash => "123")
101
105
  # d.update_ttl(30)
102
- def update_ttl new_ttl
106
+ def update_ttl new_ttl=nil
107
+ # Load default if no new ttl is specified
108
+ new_ttl = self._default_expire if new_ttl.nil?
103
109
  # Make sure we have a valid value
104
110
  new_ttl = -1 if !new_ttl.to_i.is_a?(Fixnum) || new_ttl.to_i < 0
105
111
  # Update indices
data/test/expire-test.rb CHANGED
@@ -1,15 +1,16 @@
1
1
  require_relative "helper"
2
2
  require "ohm/expire"
3
3
 
4
-
5
4
  class Model < Ohm::Model
6
5
 
7
6
  include Ohm::Expire
8
7
 
8
+ TTL = 5
9
+
9
10
  attribute :hash
10
11
  index :hash
11
12
 
12
- expire 5
13
+ expire TTL
13
14
 
14
15
  attribute :data
15
16
  end
@@ -18,17 +19,21 @@ test do
18
19
  Ohm.flush
19
20
 
20
21
  m = Model.create(:hash => "123")
21
- assert_equal 5, m.get_ttl
22
+ assert_equal Model::TTL, m.get_ttl
22
23
 
23
- m.update_ttl 2
24
- assert_equal 2, m.get_ttl
24
+ m.update_ttl 3
25
+ assert_equal 3, m.get_ttl
25
26
 
26
27
  sleep 1
27
28
 
28
29
  m = Model.find(:hash => "123").first
29
30
  assert !m.hash.nil?
30
31
 
31
- sleep 2
32
+ # Update with default expire
33
+ m.update_ttl
34
+ assert_equal Model::TTL, m.get_ttl
35
+
36
+ sleep 6
32
37
 
33
38
  m = Model.find(:hash => "123").first
34
39
  assert m.hash.nil?
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ohm-expire
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Jos\xC3\xA9 P. Airosa"