sir_cachealot 0.4.2 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NmUyMGY5NzdhN2NmNjI2ZTAxYjcwMjM5N2RiYTg5ZmMyOTI0YTZiYg==
5
+ data.tar.gz: !binary |-
6
+ MjRhYjBlYmE0ZDQ4MDQ4MDE2MzE3MTU5NzNhNzZhM2I4ODRiNmRmMw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ M2JhMGZiYzc3NGFjMmY4OWUzOWJhMTZjYTc5ODU3NmQ0ZjI1ZjRmMDAxMjM3
10
+ NjM4NGU4MWVhNzc3YjQ1MmIyNWQ1OGYxYzBmMGFhZGI2M2E3ODEwNDI3NDQz
11
+ MmFjOWEwODk3ZTIwYzE0YzNiZjIzMWZjNjRlOTBhMjc4MTczYmI=
12
+ data.tar.gz: !binary |-
13
+ Mjc5NjRlZGY0ZjU1M2M5YThkZGE0YWVlNmU0NzViMjE2ZGI1ODQ4YzZmZDYy
14
+ N2RhMjE1ZjZiZGI4NGRkMDI0MTZlM2U2MTI1MDE4MjY5ZjM4NWVjYWU1ZWVh
15
+ MjQ1MDA1Y2UyMWJlOTIxMWVhOTdjMjIxNzU2Nzc3MDdlZTBkZjk=
data/README.md CHANGED
@@ -35,7 +35,8 @@ If `config(:delete_on_nil) == true` and `value == nil`, `put()` will return `tru
35
35
 
36
36
  **You can retreive the value later, if it hasn't expired, with:**
37
37
 
38
- my_var = Sir.get(keyname)
38
+ my_var = Sir.get(keyname) # for a shallow copy
39
+ my_var = Sir.get(keyname, true) # for a deep copy (crude way, Marshal.load(Marshal.dump(obj)) )
39
40
 
40
41
  or
41
42
 
data/lib/sir_cachealot.rb CHANGED
@@ -1,8 +1,11 @@
1
- require "sir_cachealot/version"
1
+ #require "sir_cachealot/version"
2
2
 
3
3
  module Sir
4
4
 
5
5
  RAM = :ram_cache
6
+ REDIS = :redis_cache
7
+
8
+ @@redis_driver = nil
6
9
 
7
10
  @@ram_cache = { }
8
11
 
@@ -11,8 +14,8 @@ module Sir
11
14
  debug: false,
12
15
  annoy: false, #super annoying debug messages
13
16
  default_expiry: 3600, #Integer(1.hour),
14
- delete_on_nil: true
15
-
17
+ delete_on_nil: true,
18
+ use_redis_obj: nil
16
19
  }
17
20
 
18
21
  #Configuration parameters
@@ -25,6 +28,11 @@ module Sir
25
28
 
26
29
  if !key.nil? && !value.nil?
27
30
  @@configuration[key] = value
31
+
32
+ if key == :use_redis_obj
33
+ self::set_redis_obj(value)
34
+ end
35
+
28
36
  return value
29
37
 
30
38
  elsif !key.nil? && value.nil?
@@ -43,6 +51,10 @@ module Sir
43
51
  @@configuration.delete(k)
44
52
  end
45
53
 
54
+ if knew == :use_redis_obj
55
+ self::set_redis_obj(@@configuration[knew])
56
+ end
57
+
46
58
  end
47
59
 
48
60
  end
@@ -63,25 +75,30 @@ module Sir
63
75
 
64
76
  case config(:mode)
65
77
  when RAM
66
-
67
78
  if x = @@ram_cache[key]
68
-
69
79
  if x[:expiry].nil? || x[:expiry] > Time.now
70
80
  to_ret = x[:value]
71
81
  else
72
82
  # cache entry is stale
73
83
  puts (" SirCachealot: Cache entry <#{key}> expired at #{x[:expiry]}. Deleting...") if config(:debug)
74
84
  @@ram_cache.delete(key)
75
-
76
85
  end
77
-
78
86
  end
79
-
87
+
88
+ when REDIS
89
+ key = self::nsed_key(key)
90
+ got = @@redis_driver.get(key)
91
+
92
+ unless got.nil?
93
+ to_ret = Marshal.load( got )
94
+ else
95
+ to_ret = nil
96
+ end
80
97
  else
81
98
  puke
82
99
  end
83
100
 
84
- if copy
101
+ if copy && config(:mode) == RAM
85
102
  to_ret = self.crude_clone(to_ret)
86
103
  end
87
104
 
@@ -104,6 +121,12 @@ module Sir
104
121
  @@ram_cache.delete(key)
105
122
  return true
106
123
  end
124
+ when REDIS
125
+ if @@redis_driver.del( self::nsed_key(key) )
126
+ return true
127
+ end
128
+ else
129
+ puke
107
130
  end
108
131
 
109
132
  end
@@ -138,12 +161,18 @@ module Sir
138
161
  else
139
162
  case config(:mode)
140
163
  when RAM
141
-
142
164
  @@ram_cache[key] ||= { }
143
165
  @@ram_cache[key][:value] = value
144
166
  @@ram_cache[key][:expiry] = expiry
145
167
  return value
146
168
 
169
+ when REDIS
170
+ key = self::nsed_key(key)
171
+ @@redis_driver.set(key, Marshal.dump(value).to_s )
172
+ #$stderr.puts "Will expire at #{expiry}"
173
+ @@redis_driver.expireat(key, expiry.to_i) unless expiry == nil
174
+ return value
175
+
147
176
  else
148
177
  puke
149
178
  end
@@ -159,6 +188,8 @@ module Sir
159
188
  case config(:mode)
160
189
  when RAM
161
190
  return @@ram_cache.count
191
+ when REDIS
192
+ return :not_possible
162
193
  else
163
194
  puke
164
195
  end
@@ -174,6 +205,8 @@ module Sir
174
205
  @@ram_cache.each do |k, v|
175
206
  puts("%-20s %-20s %20s" % [k, v[:value].class, v[:expiry]])
176
207
  end
208
+ when REDIS
209
+ raise ArgumentError, "This command not available in REDIS mode"
177
210
  else
178
211
  puke
179
212
  end
@@ -190,6 +223,8 @@ module Sir
190
223
  case config(:mode)
191
224
  when RAM
192
225
  @@ram_cache = { }
226
+ when REDIS
227
+ raise ArgumentError, "This command not available in REDIS mode"
193
228
  else
194
229
  puke
195
230
  end
@@ -209,6 +244,8 @@ module Sir
209
244
  @@ram_cache.delete(k)
210
245
  end
211
246
  end
247
+ when REDIS
248
+ raise ArgumentError, "This command not available in REDIS mode"
212
249
  else
213
250
  puke
214
251
  end
@@ -218,13 +255,28 @@ module Sir
218
255
 
219
256
  private
220
257
 
258
+ def self.set_redis_obj(value)
259
+ if value.class.name == "Redis"
260
+ @@configuration[:mode] = REDIS
261
+ @@redis_driver = @@configuration[:use_redis_obj]
262
+ else
263
+ if @@configuration[:mode] == REDIS
264
+ raise ArgumentError, "use_redis_obj must be of class Redis"
265
+ end
266
+
267
+ end
268
+ end
269
+
221
270
  def self.puke
222
271
  raise TypeError, "Invalid config(:mode). Check the inputs sent to Sir.configure()"
223
272
  end
224
273
 
225
-
226
274
  def self.crude_clone(obj)
227
275
  return Marshal.load(Marshal.dump(obj))
228
276
  end
229
-
230
- end
277
+
278
+ # returns a namespaced key
279
+ def self.nsed_key(key)
280
+ return "SirCachealot-#{key}"
281
+ end
282
+ end
@@ -1,3 +1,3 @@
1
1
  module SirCachealot
2
- VERSION = "0.4.2"
2
+ VERSION = "0.5"
3
3
  end
@@ -18,5 +18,6 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_development_dependency 'rspec', '~> 2.5'
21
+ gem.add_development_dependency 'redis'
21
22
 
22
23
  end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'redis'
3
+
4
+ TEST = "test"
5
+ DEFAULT_EXPIRY = 600
6
+
7
+ redis_obj = Redis.new(:path => "/tmp/redis.sock")
8
+
9
+ describe "SirCachealot Redis support" do
10
+
11
+ it 'should connect and configure correctly' do
12
+ Sir.configure do |config|
13
+ config[:default_expiry] = DEFAULT_EXPIRY
14
+ config[:mode] = :redis_cache
15
+ config[:debug] = true
16
+ config[:use_redis_obj] = redis_obj
17
+ end
18
+ end
19
+
20
+ it 'should return the key that is put into it' do
21
+ Sir.put(:test, TEST).should == TEST
22
+ end
23
+
24
+ it 'should return an arbitrary key' do
25
+ (Sir.get(:test) == TEST).should == true
26
+ end
27
+
28
+ it 'should yield when given a block and a key that does not exist' do
29
+ (Sir.get(:asdoiajdoaijdaodijaodiajdoaidjaodijaodij) { 5 }).should == 5
30
+ end
31
+
32
+ it 'should return nil when not given a block and a key that does not exist' do
33
+ (Sir.get(:asdoiajdoaijdaodijaodiajdoaidjaodijaodij)).should == nil
34
+ end
35
+
36
+ it 'should immediately expire a 0-expiry key' do
37
+ Sir.put(:expire, TEST, 0)
38
+ sleep(1)
39
+
40
+ Sir.get(:expire).should == nil
41
+ end
42
+
43
+ it 'should report its size correctly' do
44
+
45
+ Sir.size?.should == :not_possible
46
+
47
+ end
48
+
49
+ end
@@ -13,7 +13,7 @@ describe SirCachealot do
13
13
  config[:debug] = true
14
14
  end
15
15
 
16
- (Sir.config(:default_expiry) == DEFAULT_EXPIRY && Sir.config("MODE") == :ram_cache).should == true
16
+ (Sir.config(:default_expiry) == DEFAULT_EXPIRY && Sir.config(:mode) == :ram_cache).should == true
17
17
 
18
18
  end
19
19
 
@@ -22,9 +22,7 @@ describe SirCachealot do
22
22
  end
23
23
 
24
24
  it 'should return an arbitrary key' do
25
-
26
25
  (Sir.get(:test) == TEST).should == true
27
-
28
26
  end
29
27
 
30
28
  it 'should yield when given a block and a key that does not exist' do
@@ -104,6 +102,5 @@ describe SirCachealot do
104
102
  (hash[:a][:aa]).should_not == newhash[:a][:aa]
105
103
 
106
104
  end
107
-
108
-
105
+
109
106
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sir_cachealot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
5
- prerelease:
4
+ version: '0.5'
6
5
  platform: ruby
7
6
  authors:
8
7
  - Lyjia
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-12-06 00:00:00.000000000 Z
11
+ date: 2013-08-21 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,11 +20,24 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
29
26
  version: '2.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redis
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
30
41
  description: A dead simple RAM keystore
31
42
  email:
32
43
  - tom@tomcorelis.com
@@ -43,35 +54,36 @@ files:
43
54
  - lib/sir_cachealot/version.rb
44
55
  - sir_cachealot.gemspec
45
56
  - sircachealot.png
57
+ - spec/redis_spec.rb
46
58
  - spec/sir_cachealot_spec.rb
47
59
  - spec/spec_helper.rb
48
60
  homepage: http://www.lyjia.us
49
61
  licenses: []
62
+ metadata: {}
50
63
  post_install_message:
51
64
  rdoc_options: []
52
65
  require_paths:
53
66
  - lib
54
67
  required_ruby_version: !ruby/object:Gem::Requirement
55
- none: false
56
68
  requirements:
57
69
  - - ! '>='
58
70
  - !ruby/object:Gem::Version
59
71
  version: '0'
60
72
  required_rubygems_version: !ruby/object:Gem::Requirement
61
- none: false
62
73
  requirements:
63
74
  - - ! '>='
64
75
  - !ruby/object:Gem::Version
65
76
  version: '0'
66
77
  requirements: []
67
78
  rubyforge_project:
68
- rubygems_version: 1.8.24
79
+ rubygems_version: 2.0.6
69
80
  signing_key:
70
- specification_version: 3
81
+ specification_version: 4
71
82
  summary: SirCachealot is a drop-in memcache-like RAM cache for Ruby. Cache entries
72
83
  are saved and recalled by a key string, and their values can be whatever a Ruby
73
84
  hash can hold. Values can also expire, however expiration is only checked when the
74
85
  key is called, or a manual sweeper is run.
75
86
  test_files:
87
+ - spec/redis_spec.rb
76
88
  - spec/sir_cachealot_spec.rb
77
89
  - spec/spec_helper.rb