restruct 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2bd75f82dde2f17f7b562be3a8fe5b264028eaba
4
- data.tar.gz: ed15597cbf0b1487d4ab9697649dd3575ff01e75
3
+ metadata.gz: b6ca1588dcf0f6ebcb9c04d2e13c5883c03c0264
4
+ data.tar.gz: 943e2124112da6248e3dced1ddd043fdeb980701
5
5
  SHA512:
6
- metadata.gz: f5febc64be1599503c84a16a66a1e3fd0d447c99db365f620d67043117a3f49a92f6aeb37b75e11e3df1b8ccd75059d076f2aebfd61c4052eaa0b429a8c34b53
7
- data.tar.gz: 22c7f357f209ee553c05699a1f3e252b65da9378596c1002da42a161ab2b29504215bbf6818d25547160fe0dc38794bba2e48bf0f3d9bac0b5e3a60ce3323cfe
6
+ metadata.gz: f3ca286a4dd746fa4edf2bdeae09bc014cab33895b8a799dfd893fd9934499a3797a230b901b7a75b220d989e60594cc1027685b102d2dffd2e2f2d57d024847
7
+ data.tar.gz: a82d2e756549aa096ef6399875628570051d49ba2359e05ca081622a717843b53c73b729e1044ac7e6d45bf24a3345f15adb055a195ae7dd54931de246bdfe91
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
+
2
3
  rvm:
3
4
  - 1.9.3
4
5
  - 2.0
@@ -6,8 +7,20 @@ rvm:
6
7
  - 2.2
7
8
  - 2.3.0
8
9
  - 2.4.0
9
- - jruby
10
+ - 2.5.0
11
+ - jruby-1.7.25
12
+ - jruby-9.1.7.0
13
+ - ruby-head
14
+ - jruby-head
15
+
16
+ matrix:
17
+ fast_finish: true
18
+ allow_failures:
19
+ - rvm: ruby-head
20
+ - rvm: jruby-head
21
+
10
22
  before_install:
11
23
  - gem install bundler
24
+
12
25
  services:
13
- - redis-server
26
+ - redis-server
data/README.md CHANGED
@@ -14,6 +14,7 @@ Redis structures
14
14
  - NestedHash
15
15
  - Queue
16
16
  - Locker
17
+ - Cache
17
18
 
18
19
  ## Installation
19
20
 
@@ -12,12 +12,14 @@ require_relative 'restruct/array'
12
12
  require_relative 'restruct/set'
13
13
  require_relative 'restruct/hash'
14
14
  require_relative 'restruct/queue'
15
+ require_relative 'restruct/cache'
15
16
  require_relative 'restruct/nested_hash'
16
17
  require_relative 'restruct/marshalizable'
17
18
  require_relative 'restruct/marshal_array'
18
19
  require_relative 'restruct/marshal_set'
19
20
  require_relative 'restruct/marshal_hash'
20
21
  require_relative 'restruct/marshal_queue'
22
+ require_relative 'restruct/marshal_cache'
21
23
  require_relative 'restruct/locker'
22
24
  require_relative 'restruct/connection'
23
25
 
@@ -0,0 +1,94 @@
1
+ module Restruct
2
+ class Cache < Structure
3
+
4
+ include Enumerable
5
+
6
+ attr_reader :ttl
7
+
8
+ def initialize(options={})
9
+ super options
10
+ @ttl = options[:ttl]
11
+ end
12
+
13
+ def key?(key)
14
+ connection.call('EXISTS', id[key]) == 1
15
+ end
16
+ alias_method :has_key?, :key?
17
+
18
+ def keys
19
+ sections = id.sections.count + 1
20
+ connection.call('KEYS', id['*']).map do |k|
21
+ Id.new(k).sections.take(sections).last
22
+ end.uniq.sort
23
+ end
24
+
25
+ def [](key)
26
+ deserialize connection.call('GET', id[key])
27
+ end
28
+
29
+ def []=(key, value)
30
+ connection.lazy 'SET', id[key], serialize(value)
31
+ connection.lazy 'EXPIRE', id[key], ttl if ttl
32
+ end
33
+
34
+ def delete(key)
35
+ value = self[key]
36
+ connection.lazy 'DEL', id[key]
37
+ value
38
+ end
39
+
40
+ def fetch(key, &block)
41
+ if key? key
42
+ connection.lazy 'EXPIRE', id[key], ttl if ttl
43
+ self[key]
44
+ else
45
+ value = block.call
46
+ self[key] = value
47
+ value
48
+ end
49
+ end
50
+
51
+ def each
52
+ keys.each { |key| yield key, self[key] }
53
+ end
54
+
55
+ def size
56
+ keys.count
57
+ end
58
+ alias_method :count, :size
59
+ alias_method :length, :size
60
+
61
+ def empty?
62
+ size == 0
63
+ end
64
+
65
+ def to_h
66
+ keys.each_with_object({}) do |key, hash|
67
+ hash[key] = self[key]
68
+ end
69
+ end
70
+ alias_method :to_primitive, :to_h
71
+ alias_method :dump, :to_h
72
+
73
+ def restore(dump)
74
+ dump.each { |k,v| self[k] = v }
75
+ end
76
+
77
+ def destroy
78
+ keys.each { |k| connection.lazy 'DEL', id[k] }
79
+ self
80
+ end
81
+ alias_method :clear, :destroy
82
+
83
+ private
84
+
85
+ def serialize(string)
86
+ string
87
+ end
88
+
89
+ def deserialize(string)
90
+ string
91
+ end
92
+
93
+ end
94
+ end
@@ -0,0 +1,5 @@
1
+ module Restruct
2
+ class MarshalCache < Cache
3
+ include Marshalizable
4
+ end
5
+ end
@@ -6,7 +6,7 @@ module Restruct
6
6
  end
7
7
 
8
8
  def deserialize(element)
9
- Marshal.load element if element
9
+ Marshal.load element unless element.nil?
10
10
  end
11
11
 
12
12
  end
@@ -1,3 +1,3 @@
1
1
  module Restruct
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -0,0 +1,157 @@
1
+ require 'minitest_helper'
2
+
3
+ [Restruct::Cache, Restruct::MarshalCache].each do |klass|
4
+
5
+ describe klass do
6
+
7
+ let(:sample_cache) { klass.new }
8
+
9
+ def fill(data)
10
+ data.each do |k,v|
11
+ connection.call 'SET', sample_cache.id[k], sample_cache.send(:serialize, v)
12
+ end
13
+ end
14
+
15
+ describe 'Getters' do
16
+
17
+ it '[]' do
18
+ fill a: 'x', b: 'y'
19
+
20
+ sample_cache[:a].must_equal 'x'
21
+ sample_cache[:b].must_equal 'y'
22
+ sample_cache[:c].must_be_nil
23
+ end
24
+
25
+ it 'fetch' do
26
+ fill a: 'x', b: 'y'
27
+
28
+ sample_cache.fetch(:a) { 'z' }.must_equal 'x'
29
+ sample_cache.fetch(:b) { 'z' }.must_equal 'y'
30
+ sample_cache.fetch(:c) { 'z' }.must_equal 'z'
31
+ end
32
+
33
+ it 'keys' do
34
+ fill a: 'x', b: 'y', c: 'z'
35
+ sample_cache.keys.must_equal %w(a b c)
36
+ end
37
+
38
+ it 'TTL' do
39
+ sample_cache = klass.new ttl: 1
40
+ sample_cache[:a] = 'x'
41
+
42
+ sample_cache[:a].must_equal 'x'
43
+ sleep 1.1
44
+ sample_cache[:a].must_be_nil
45
+ end
46
+
47
+ end
48
+
49
+ describe 'Setters' do
50
+
51
+ it 'delete' do
52
+ fill a: 'x', b: 'y'
53
+
54
+ sample_cache.delete(:b).must_equal 'y'
55
+ sample_cache.to_h.must_equal 'a' => 'x'
56
+
57
+ sample_cache.delete(:c).must_be_nil
58
+ sample_cache.to_h.must_equal 'a' => 'x'
59
+ end
60
+
61
+ it 'clear' do
62
+ fill a: 'x', b: 'y'
63
+
64
+ sample_cache.clear.must_equal sample_cache
65
+ sample_cache.must_be_empty
66
+ end
67
+
68
+ end
69
+
70
+ describe 'Info' do
71
+
72
+ %w(size count length).each do |method|
73
+ it method do
74
+ fill a: 'x', b: 'y'
75
+ sample_cache.send(method).must_equal 2
76
+ end
77
+ end
78
+
79
+ it 'empty?' do
80
+ sample_cache.must_be :empty?
81
+ fill a: 'x', b: 'y'
82
+ sample_cache.wont_be :empty?
83
+ end
84
+
85
+ %w(key? has_key?).each do |method|
86
+ it method do
87
+ fill a: 'x', b: 'y'
88
+
89
+ assert sample_cache.send(method, :a)
90
+ refute sample_cache.send(method, :c)
91
+ end
92
+ end
93
+
94
+ end
95
+
96
+ describe 'Transformations' do
97
+
98
+ %w(to_h to_primitive).each do |method|
99
+ it method do
100
+ fill a: 'x', b: 'y'
101
+ sample_cache.send(method).must_equal 'a' => 'x', 'b' => 'y'
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ describe 'Enumerable' do
108
+
109
+ it 'included module' do
110
+ assert Restruct::Hash.included_modules.include? Enumerable
111
+ end
112
+
113
+ it 'each' do
114
+ fill a: 'x', b: 'y'
115
+
116
+ hash = sample_cache.each_with_object({}) { |(k,v), h| h[k] = v }
117
+
118
+ hash.must_equal sample_cache.to_h
119
+ end
120
+
121
+ end
122
+
123
+ it 'Equality' do
124
+ copy = klass.new id: sample_cache.id
125
+ assert sample_cache == copy
126
+ assert sample_cache.eql? copy
127
+ refute sample_cache.equal? copy
128
+ end
129
+
130
+ it 'Dump/Restore' do
131
+ fill a: 'x', b: 'y'
132
+
133
+ dump = sample_cache.dump
134
+ other = klass.new
135
+ other.restore dump
136
+
137
+ other.id.wont_equal sample_cache.id
138
+ other.to_primitive.must_equal sample_cache.to_primitive
139
+ end
140
+
141
+ it 'Batch' do
142
+ fill a: 'x', b: 'y', c: 'z'
143
+
144
+ sample_cache.connection.batch do
145
+ sample_cache[:d] = 'w'
146
+ sample_cache.delete :a
147
+ sample_cache[:b] = 'x'
148
+
149
+ sample_cache.to_h.must_equal 'a' => 'x', 'b' => 'y', 'c' => 'z'
150
+ end
151
+
152
+ sample_cache.to_h.must_equal 'b' => 'x', 'c' => 'z', 'd' => 'w'
153
+ end
154
+
155
+ end
156
+
157
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-08 00:00:00.000000000 Z
11
+ date: 2018-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redic
@@ -196,12 +196,14 @@ files:
196
196
  - Rakefile
197
197
  - lib/restruct.rb
198
198
  - lib/restruct/array.rb
199
+ - lib/restruct/cache.rb
199
200
  - lib/restruct/connection.rb
200
201
  - lib/restruct/errors.rb
201
202
  - lib/restruct/hash.rb
202
203
  - lib/restruct/id.rb
203
204
  - lib/restruct/locker.rb
204
205
  - lib/restruct/marshal_array.rb
206
+ - lib/restruct/marshal_cache.rb
205
207
  - lib/restruct/marshal_hash.rb
206
208
  - lib/restruct/marshal_queue.rb
207
209
  - lib/restruct/marshal_set.rb
@@ -215,6 +217,7 @@ files:
215
217
  - lua/unregister.lua
216
218
  - restruct.gemspec
217
219
  - spec/array_spec.rb
220
+ - spec/cache_spec.rb
218
221
  - spec/connection_spec.rb
219
222
  - spec/coverage_helper.rb
220
223
  - spec/hash_spec.rb
@@ -250,6 +253,7 @@ specification_version: 4
250
253
  summary: Redis structures
251
254
  test_files:
252
255
  - spec/array_spec.rb
256
+ - spec/cache_spec.rb
253
257
  - spec/connection_spec.rb
254
258
  - spec/coverage_helper.rb
255
259
  - spec/hash_spec.rb