collectr 0.0.5 → 0.0.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4a1f4385cfefa7933f53f24c6e7301e69cdc462b
4
- data.tar.gz: 62e291a7a1d17c36f0caffb0a4318dbdb14ad6e2
3
+ metadata.gz: ef5508b4fd489d875c554e14b9a7c66865c5b9ba
4
+ data.tar.gz: 4cc883b6c7e1715a2bf226900b46e7aa75614dd7
5
5
  SHA512:
6
- metadata.gz: 3a9f9c5d33f12bac6e1fb9de0362ccd4c5769370016845468e200e62c9387cb66b18e555e2c2ddb9883facc2e61210c5ac28f6225104aa4730771a4a4e6c80ca
7
- data.tar.gz: 874d39f87800c6d3cdb6f21c7ead59b11615f08bda08215faa421c0a9b18ae63ceb312b5bc1f9b39ec7f5c165df7e01567bf04d54751d5c4aa7f1999bfd9d053
6
+ metadata.gz: deee470529b6ec71158c8846b10ec7b6cec78c2a72f9b752b44a29a35d456529eeeb6dec9e2be57fd7db2a70dad604b19a5ca8da806c234b2b345ae99dfeb440
7
+ data.tar.gz: 6e717276f583f241aa831e31d1c6d7a3dd8a1cb96a6179c8574ac09951b7f417b2c518b23af85f725472481881f6da7971ac88361fad73612b40990b30757e74
@@ -1,3 +1,3 @@
1
1
  module Collectr
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -0,0 +1,122 @@
1
+ require 'redis'
2
+ require 'json'
3
+
4
+ require_relative 'redis_base'
5
+
6
+ # Redis has a special hash type, which keeps the redis namespace clean, but
7
+ # doesn't allow expriations of the keys inside the hash.
8
+ #
9
+ # This class solves that problem by placing the keys in the global redis namespec.
10
+ # As long as the name provided upon creation is unique, this class acts like a hash.
11
+ #
12
+ # It might be tempting to replace redis_hash.rb with this code; however,
13
+ # that file is preferable when expirations aren't needed because the namespace
14
+ # is kept clean.
15
+
16
+ module Collectr
17
+ class RedisHashExpiry < RedisBase
18
+ attr_reader :store
19
+
20
+ def initialize(name, options={})
21
+ @title = name
22
+ # Use raw only when both the keys and values are strings.
23
+ @raw = options.fetch(:raw) { false }
24
+ @store = Redis.current
25
+ @default_expires_in = options[:expires_in]
26
+ end
27
+
28
+ def [](key)
29
+ deserialize @store.get(key_name(key))
30
+ end
31
+
32
+ def []=(key, val, options={})
33
+ write(key, val, options)
34
+ end
35
+
36
+ def set_expiration(key, seconds)
37
+ return unless seconds
38
+ @store.expire(key_name(key), seconds)
39
+ end
40
+
41
+ def write(key, val, options={})
42
+ expiration = options.fetch(:expires_in) { @default_expires_in }
43
+ @store.set key_name(key), serialize(val)
44
+ set_expiration(key, expiration)
45
+ val
46
+ end
47
+
48
+ def fetch(key, options={})
49
+ expiration = options.fetch(:expires_in) { @default_expires_in }
50
+ result = self[key]
51
+ if result.nil?
52
+ return nil if has_key?(key)
53
+ if block_given?
54
+ result = yield key
55
+ else
56
+ raise KeyError
57
+ end
58
+ end
59
+ result
60
+ end
61
+
62
+ def cache(key, options={})
63
+ fetch(key, options) do
64
+ result = yield key
65
+ write(key, result, options)
66
+ result
67
+ end
68
+ end
69
+
70
+ def destroy
71
+ keys.each{ |key| delete key }
72
+ end
73
+
74
+ def delete(key)
75
+ @store.del key_name(key)
76
+ end
77
+
78
+ def empty?
79
+ size == 0
80
+ end
81
+
82
+ def size
83
+ keys.size
84
+ end
85
+
86
+ def has_key?(key)
87
+ key? key
88
+ end
89
+ def key?(key)
90
+ @store.exists key_name(key)
91
+ end
92
+
93
+ def keys
94
+ @store.keys(key_prefix).collect{ |key| dekey key }
95
+ end
96
+
97
+ def to_hash
98
+ hash = {}
99
+ keys.each do |key|
100
+ hash[key] = self[key]
101
+ end
102
+ hash
103
+ end
104
+
105
+ def clear
106
+ destroy
107
+ # keys.each{ |key| delete key }
108
+ end
109
+
110
+ def key_name(key)
111
+ "#{@title}-#{serialize(key)}"
112
+ end
113
+
114
+ def dekey(key)
115
+ deserialize(key[key_prefix.size-1..-1])
116
+ end
117
+
118
+ def key_prefix
119
+ @key_prefix ||= "#{@title}-*"
120
+ end
121
+ end
122
+ end
data/spec/hash_spec.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'json'
2
2
  require 'spec_helper'
3
3
 
4
- require 'redis/redis_hash.rb'
5
- require 'memory/memory_hash.rb'
4
+ #require 'redis/redis_hash.rb'
5
+ #require 'redis/redis_hash_expiry.rb'
6
6
 
7
7
  shared_examples 'a hash' do
8
8
  context 'when keys and/or values may be other than strings' do
@@ -27,7 +27,7 @@ shared_examples 'a hash' do
27
27
  before { collection.clear ; collection[1] = 'aa1' ; collection['b2'] = nil ; collection[[1,2]] = 3 }
28
28
 
29
29
  its(:size) { should eq(3) }
30
- its(:keys) { should eq([1, 'b2', [1,2]]) }
30
+ it { expect(collection.keys.to_set).to eq([1, 'b2', [1,2]].to_set) }
31
31
  its(:to_hash) { should eq({1=>'aa1', 'b2'=>nil, [1,2]=>3}) }
32
32
  it { expect(collection.fetch(1)).to eq('aa1') }
33
33
  it { expect(collection.fetch('b2')).to eq(nil) }
@@ -43,10 +43,8 @@ shared_examples 'a hash' do
43
43
  before { collection.destroy ; collection['1'] = 'a1' ; collection['b2'] = '2'; collection['[1,2]'] = '3' }
44
44
  after { collection.clear }
45
45
  its(:size) { should eq(3) }
46
- its(:keys) { should eq(['1', 'b2', '[1,2]'])}
47
46
  its(:to_hash) { should eq({'1'=>'a1', 'b2'=>'2', '[1,2]'=>'3'}) }
48
- # it { expect(collection.size).to eq(3) }
49
- it { expect(collection.keys).to eq(['1', 'b2', '[1,2]'])}
47
+ it { expect(collection.keys.to_set).to eq(['1', 'b2', '[1,2]'].to_set)}
50
48
  it { expect(collection.to_hash).to eq({'1'=>'a1', 'b2'=>'2', '[1,2]'=>'3'}) }
51
49
  end
52
50
 
@@ -66,14 +64,15 @@ shared_examples 'a hash' do
66
64
  # end
67
65
  end
68
66
 
69
- if redis_exists?
67
+ if redis_exists? and false
70
68
  describe Collectr::RedisHash do
71
69
  it_behaves_like 'a hash'
72
70
  end
71
+
72
+ describe Collectr::RedisHashExpiry do
73
+ it_behaves_like 'a hash'
74
+ end
73
75
  end
74
76
 
75
77
  # warn Redis.current.flushall
76
78
 
77
- describe Collectr::MemoryHash do
78
- it_behaves_like 'a hash'
79
- end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+ require 'memory/memory_hash.rb'
3
+ require 'hash_spec'
4
+
5
+ describe Collectr::MemoryHash do
6
+ it_behaves_like 'a hash'
7
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'redis/redis_hash_expiry'
3
+ require 'hash_spec'
4
+
5
+ if redis_exists?
6
+ describe Collectr::RedisHashExpiry do
7
+
8
+ it_behaves_like 'a hash'
9
+
10
+ describe 'expires' do
11
+ subject(:collection) { described_class.new 'expiry', expires_in: 1 }
12
+ before{ collection.destroy }
13
+
14
+ it 'after the default one second' do
15
+ collection['three'] = 3
16
+ expect(collection['three']).to eq(3)
17
+ sleep 1.1
18
+ expect(collection['three']).to be_nil
19
+ end
20
+
21
+ it 'write after overridden 2 second expiration' do
22
+ collection.write('nine', 9, expires_in: 2)
23
+ expect(collection['nine']).to eq(9)
24
+ sleep 1.3
25
+ expect(collection['nine']).to eq(9)
26
+ sleep 0.8
27
+ expect(collection['nine']).to be_nil
28
+ end
29
+
30
+ it 'cache after overridden 2 seconds' do
31
+ collection.cache('ten', expires_in: 2) { 10 }
32
+ collection.cache('ten', expires_in: 5) { 20 }
33
+ expect(collection['ten']).to eq(10)
34
+ sleep 1.1
35
+ expect(collection['ten']).to eq(10)
36
+ sleep 1
37
+ expect(collection['ten']).to eq(nil)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+ require 'redis/redis_hash'
3
+ require 'hash_spec'
4
+
5
+ if redis_exists?
6
+ describe Collectr::RedisHash do
7
+ it_behaves_like 'a hash'
8
+ end
9
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collectr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Murphy-Dye
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-16 00:00:00.000000000 Z
11
+ date: 2015-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -218,10 +218,14 @@ files:
218
218
  - lib/redis/redis_base.rb
219
219
  - lib/redis/redis_factory.rb
220
220
  - lib/redis/redis_hash.rb
221
+ - lib/redis/redis_hash_expiry.rb
221
222
  - lib/redis/redis_set.rb
222
223
  - spec/bag_spec.rb
223
224
  - spec/hash_spec.rb
225
+ - spec/memory/memory_hash_spec.rb
224
226
  - spec/redis/redis_array_spec.rb
227
+ - spec/redis/redis_hash_expiry_spec.rb
228
+ - spec/redis/redis_hash_spec.rb
225
229
  - spec/spec_helper.rb
226
230
  homepage: ''
227
231
  licenses:
@@ -250,5 +254,8 @@ summary: Abstraction for thread-safe collections (array, hash, set, bag).
250
254
  test_files:
251
255
  - spec/bag_spec.rb
252
256
  - spec/hash_spec.rb
257
+ - spec/memory/memory_hash_spec.rb
253
258
  - spec/redis/redis_array_spec.rb
259
+ - spec/redis/redis_hash_expiry_spec.rb
260
+ - spec/redis/redis_hash_spec.rb
254
261
  - spec/spec_helper.rb