sir_cachealot 0.4.0 → 0.4.1

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.
data/README.md CHANGED
@@ -10,7 +10,7 @@ Future plans for 1.0:
10
10
 
11
11
  Add this line to your application's Gemfile:
12
12
 
13
- gem 'SirCachealot'
13
+ gem 'sir_cachealot'
14
14
 
15
15
  And then execute:
16
16
 
@@ -18,11 +18,11 @@ And then execute:
18
18
 
19
19
  Or install it yourself as:
20
20
 
21
- $ gem install SirCachealot
21
+ $ gem install sir_cachealot
22
22
 
23
23
  ## Usage
24
24
 
25
- SirCachealot creates a new globally-accessible class named `Sir`.
25
+ SirCachealot exposes a new module named `Sir`. This module is designed to be available globally in both Rails apps and vanilla Ruby scripts (don't forget `require 'sir_cachealot'`!)
26
26
 
27
27
  **You can use SirCachealot immediately, using either:**
28
28
 
@@ -31,21 +31,28 @@ SirCachealot creates a new globally-accessible class named `Sir`.
31
31
 
32
32
  `put()` will return the object you gave it. This is useful if you wish to use `get()`'s `yield` functionality.
33
33
 
34
+ If `config(:delete_on_nil) == true` and `value == nil`, `put()` will return `true` (because it deleted the key).
35
+
34
36
  **You can retreive the value later, if it hasn't expired, with:**
35
37
 
36
38
  my_var = Sir.get(keyname)
37
39
 
38
40
  or
39
41
 
40
- # put() returns the object you give it
41
- my_var = Sir.get(keyname) do
42
- Sir.put(keyname, User.find_by_id(id))
43
- end
42
+ # A convenient way to rectify a cache miss!
43
+ # put() returns the object you give it
44
+ my_var = Sir.get(keyname) do
45
+ Sir.put(keyname, User.find_by_id(id))
46
+ end
44
47
 
45
48
  If the key does not exist, or if it has expired:
46
49
  * `get()` returns `nil` if not given a block to execute.
47
50
  * `get()` yields to code block, if one is supplied.
48
51
 
52
+ **To delete a cache entry, you can:**
53
+
54
+ Sir.delete(key)
55
+
49
56
  **If you want to clear the cache, you can:**
50
57
 
51
58
  Sir.clear
@@ -61,6 +68,7 @@ If the key does not exist, or if it has expired:
61
68
  config[:mode] = :ram_cache # cache storage mode. Currently only :ram_cache is supported. Others may be added at a later date.
62
69
  config[:debug] = true|false # show some debug messages
63
70
  config[:annoy] = true|false # show even more debug messages
71
+ config[:delete_on_nil] = true|false # auto-deletes stale cache entries on if value == nil
64
72
  end
65
73
 
66
74
  *Note: Config keynames are always `.downcase.to_sym`!*
data/lib/sir_cachealot.rb CHANGED
@@ -11,6 +11,7 @@ module Sir
11
11
  debug: false,
12
12
  annoy: false, #super annoying debug messages
13
13
  default_expiry: 3600, #Integer(1.hour),
14
+ delete_on_nil: true
14
15
 
15
16
  }
16
17
 
@@ -62,7 +63,7 @@ module Sir
62
63
 
63
64
  if x = @@ram_cache[key]
64
65
 
65
- if x[:expiry] > Time.now
66
+ if x[:expiry].nil? || x[:expiry] > Time.now
66
67
  return x[:value]
67
68
  else
68
69
  # cache entry is stale
@@ -89,10 +90,25 @@ module Sir
89
90
  end
90
91
 
91
92
  end
93
+
94
+ # deletes the specified key from the cache.
95
+ # returns true if successful, false if not found
96
+ def self.delete(key)
97
+
98
+ case config(:mode)
99
+ when RAM
100
+ if @@ram_cache.has_key?(key)
101
+ @@ram_cache.delete(key)
102
+ return true
103
+ end
104
+ end
105
+
106
+ end
92
107
 
93
108
  # Puts value in cache, key is param key
94
109
  # Param expiry is optional, it can be a relative or absolute Fixnum or Time object
95
- # Returns expiry time in absolute form (e.g. 3:56pm instead of 3 hours from now)
110
+ # Returns the value you put into it, unless config(:delete_on_nil) == true and value == nil.
111
+ # Returns true (because it deleted the key) if config(:delete_on_nil) == true and value == nil.
96
112
  def self.put(key, value, expiry = config(:default_expiry))
97
113
 
98
114
  case expiry.class.name
@@ -100,28 +116,35 @@ module Sir
100
116
  true
101
117
  when "Time"
102
118
  expiry = Integer(expiry)
119
+ when "NilClass"
120
+ true
103
121
  else
104
122
  raise ArgumentError, "Expiry must be a Fixnum or Time object"
105
123
  end
106
124
 
107
125
 
108
- unless (expiry > Integer(Time.now))
126
+ unless (!expiry.nil? && expiry > Integer(Time.now))
109
127
  expiry = Time.now + expiry
110
128
  end
111
129
 
112
130
  puts " SirCachealot: Will expire <#{key}> at #{expiry}" if config(:annoy) == true
113
131
 
114
- case config(:mode)
115
- when RAM
132
+ if config(:delete_on_nil) == true && value == nil
133
+ self.delete(key)
134
+ else
135
+ case config(:mode)
136
+ when RAM
116
137
 
117
- @@ram_cache[key] ||= { }
118
- @@ram_cache[key][:value] = value
119
- @@ram_cache[key][:expiry] = expiry
120
- return value
138
+ @@ram_cache[key] ||= { }
139
+ @@ram_cache[key][:value] = value
140
+ @@ram_cache[key][:expiry] = expiry
141
+ return value
121
142
 
122
- else
123
- puke
143
+ else
144
+ puke
145
+ end
124
146
  end
147
+
125
148
 
126
149
 
127
150
  end
@@ -174,7 +197,7 @@ module Sir
174
197
 
175
198
  when RAM
176
199
  @@ram_cache.each_key do |k|
177
- if @@ram_cache[k][:expiry] < Time.now
200
+ if !@@ram_cache[k][:expiry].nil? && @@ram_cache[k][:expiry] < Time.now
178
201
  puts(" SirCachealot: Cleaned #{k}") if config(:debug)
179
202
  @@ram_cache.delete(k)
180
203
  end
@@ -1,3 +1,3 @@
1
1
  module SirCachealot
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -56,15 +56,29 @@ describe SirCachealot do
56
56
  Sir.clean
57
57
 
58
58
  Sir.size?.should == 0
59
-
60
-
59
+
61
60
  end
62
61
 
63
62
  it 'should dump() correctly' do
64
63
  Sir.put(:dump, TEST)
65
64
  Sir.dump
66
65
  end
67
-
68
-
66
+
67
+ it 'should accept a nil-expiry key' do
68
+ Sir.put(:test_nil, TEST).should == TEST
69
+ end
70
+
71
+ it 'should not expire a nil-expiry key' do
72
+ Sir.get(:test_nil).should == TEST
73
+ end
74
+
75
+ it 'should auto-delete a key when fed a nil' do
76
+ Sir.put(:test_nil, nil).should == true
77
+ end
78
+
79
+ it 'should delete a key if specified' do
80
+ Sir.put(:delete_me, TEST)
81
+ Sir.delete(:delete_me).should == true
82
+ end
69
83
 
70
84
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sir_cachealot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-27 00:00:00.000000000 Z
12
+ date: 2012-09-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec