cache_box 0.0.1.pre.preview3 → 0.0.1.pre.preview8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/cache_box.gemspec +17 -5
- data/lib/cache_box.rb +39 -162
- data/lib/cache_box/box.rb +17 -0
- data/lib/cache_box/chain.rb +90 -0
- data/lib/cache_box/graph.rb +87 -0
- data/lib/cache_box/helper/validate.rb +139 -0
- data/lib/cache_box/scheduler/serial.rb +159 -0
- data/lib/cache_box/storage/file.rb +118 -0
- data/lib/cache_box/storage/memory.rb +195 -0
- data/lib/cache_box/unit.rb +168 -0
- metadata +21 -12
- data/lib/cache_box_chain.rb +0 -177
@@ -0,0 +1,168 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class CacheBox
|
4
|
+
class Unit
|
5
|
+
include CacheBox::Helper::Validate
|
6
|
+
|
7
|
+
# Input:
|
8
|
+
#
|
9
|
+
# namespace = String | Symbol # Default: :namespace
|
10
|
+
#
|
11
|
+
# Output: N/A
|
12
|
+
def initialize(namespace = nil, logger: nil, storage: nil)
|
13
|
+
namespace ||= :namespace
|
14
|
+
validate_string_or_symbol!(namespace, 'namespace')
|
15
|
+
|
16
|
+
@namespace = namespace
|
17
|
+
@logger = logger || Logger.new(STDOUT, level: Logger::INFO)
|
18
|
+
@storage = storage || ::CacheBox::Storage::File.new(namespace: @namespace)
|
19
|
+
|
20
|
+
@state = { result: {}, stash: {} }
|
21
|
+
end
|
22
|
+
|
23
|
+
# Input:
|
24
|
+
#
|
25
|
+
# name = String | Symbol # Default: 'name'
|
26
|
+
# args = Object
|
27
|
+
# input = Object
|
28
|
+
# &block = Proc(CacheBox::Box)
|
29
|
+
#
|
30
|
+
# Output: Object # Anything the &block returns.
|
31
|
+
def with(name = nil, args = nil, input = nil, &block)
|
32
|
+
name ||= 'name'
|
33
|
+
validate_string_or_symbol!(name, 'name')
|
34
|
+
validate_block_presence!(block, '#with')
|
35
|
+
|
36
|
+
name_o = name
|
37
|
+
name_s = name.to_s
|
38
|
+
|
39
|
+
return @state[:result][name_s] if @state[:result].key?(name_s)
|
40
|
+
|
41
|
+
data = @storage.read!(name_s)
|
42
|
+
if data&.key?(:result)
|
43
|
+
@state[:result][name_s] = data[:result]
|
44
|
+
|
45
|
+
return data[:result]
|
46
|
+
elsif data&.key?(:stash)
|
47
|
+
@state[:stash][name_s] = data[:stash]
|
48
|
+
end
|
49
|
+
|
50
|
+
box = ::CacheBox::Box.new(
|
51
|
+
namespace: @namespace.dup,
|
52
|
+
name: name_o.dup,
|
53
|
+
args: args,
|
54
|
+
input: input,
|
55
|
+
stash: @state[:stash][name] || {},
|
56
|
+
logger: @logger
|
57
|
+
)
|
58
|
+
|
59
|
+
begin
|
60
|
+
@state[:result][name_s] = block.call(box)
|
61
|
+
ensure
|
62
|
+
if @state[:result].key?(name_s)
|
63
|
+
@state[:stash].delete(name_s)
|
64
|
+
@storage.write!(name_s, { result: @state[:result][name_s] })
|
65
|
+
else
|
66
|
+
stash = box.stash
|
67
|
+
dump = { stash: stash }
|
68
|
+
|
69
|
+
@state[:stash][name_s] = stash
|
70
|
+
@storage.write!(name_s, dump)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Input:
|
76
|
+
#
|
77
|
+
# name = String | Symbol # Default: 'name'
|
78
|
+
#
|
79
|
+
# Output: true | false
|
80
|
+
def has?(name = nil)
|
81
|
+
name ||= 'name'
|
82
|
+
validate_string_or_symbol!(name, 'name')
|
83
|
+
name = name.to_s
|
84
|
+
|
85
|
+
return true if @state[:result].key?(name)
|
86
|
+
|
87
|
+
data = @storage.read!(name)
|
88
|
+
|
89
|
+
if data&.key?(:result)
|
90
|
+
@state[:result][name] = data[:result]
|
91
|
+
elsif data&.key?(:stash)
|
92
|
+
@state[:stash][name] = data[:stash]
|
93
|
+
end
|
94
|
+
|
95
|
+
@state[:result].key?(name)
|
96
|
+
end
|
97
|
+
|
98
|
+
# Input:
|
99
|
+
#
|
100
|
+
# name = String | Symbol # Default: 'name'
|
101
|
+
#
|
102
|
+
# Output: true | false
|
103
|
+
def result(name = nil)
|
104
|
+
name ||= 'name'
|
105
|
+
validate_string_or_symbol!(name, 'name')
|
106
|
+
name = name.to_s
|
107
|
+
|
108
|
+
return @state[:result][name] if @state[:result].key?(name)
|
109
|
+
|
110
|
+
data = @storage.read!(name)
|
111
|
+
|
112
|
+
if data&.key?(:result)
|
113
|
+
@state[:result][name] = data[:result]
|
114
|
+
elsif data&.key?(:stash)
|
115
|
+
@state[:stash][name] = data[:stash]
|
116
|
+
end
|
117
|
+
|
118
|
+
@state[:result][name]
|
119
|
+
end
|
120
|
+
|
121
|
+
# Input:
|
122
|
+
#
|
123
|
+
# name = Array[String | Symbol] # Default: []
|
124
|
+
#
|
125
|
+
# Output: self
|
126
|
+
def expire!(*names)
|
127
|
+
validate_array_of_string_or_symbol!(names, 'names')
|
128
|
+
|
129
|
+
if names.empty?
|
130
|
+
@state = { result: {}, stash: {} }
|
131
|
+
|
132
|
+
@storage.reset!
|
133
|
+
else
|
134
|
+
names.each do |name|
|
135
|
+
name = name.to_s
|
136
|
+
@state[:result].delete(name)
|
137
|
+
@state[:stash].delete(name)
|
138
|
+
|
139
|
+
@storage.delete!(name)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
self
|
144
|
+
end
|
145
|
+
|
146
|
+
# Input:
|
147
|
+
#
|
148
|
+
# name = Array[String | Symbol] # Default: []
|
149
|
+
#
|
150
|
+
# Output: self
|
151
|
+
def clear(*names)
|
152
|
+
validate_array_of_string_or_symbol!(names, 'names')
|
153
|
+
|
154
|
+
if names.empty?
|
155
|
+
@state = { result: {}, stash: {} }
|
156
|
+
else
|
157
|
+
names.each do |name_o|
|
158
|
+
name_s = name_o.to_s
|
159
|
+
|
160
|
+
@state[:result].delete(name_s)
|
161
|
+
@state[:stash].delete(name_s)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
self
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cache_box
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.pre.
|
4
|
+
version: 0.0.1.pre.preview8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Codruț Constantin Gușoi
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -66,9 +66,9 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0.88'
|
69
|
-
description:
|
69
|
+
description:
|
70
70
|
email:
|
71
|
-
- codrut.gusoi+
|
71
|
+
- codrut.gusoi+rubygems.org@gmail.com
|
72
72
|
executables: []
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
@@ -76,12 +76,21 @@ files:
|
|
76
76
|
- LICENSE
|
77
77
|
- cache_box.gemspec
|
78
78
|
- lib/cache_box.rb
|
79
|
-
- lib/
|
79
|
+
- lib/cache_box/box.rb
|
80
|
+
- lib/cache_box/chain.rb
|
81
|
+
- lib/cache_box/graph.rb
|
82
|
+
- lib/cache_box/helper/validate.rb
|
83
|
+
- lib/cache_box/scheduler/serial.rb
|
84
|
+
- lib/cache_box/storage/file.rb
|
85
|
+
- lib/cache_box/storage/memory.rb
|
86
|
+
- lib/cache_box/unit.rb
|
80
87
|
homepage: https://gitlab.com/sdwolfz/cache_box_rb
|
81
88
|
licenses:
|
82
|
-
- BSD
|
83
|
-
metadata:
|
84
|
-
|
89
|
+
- BSD-3-Clause
|
90
|
+
metadata:
|
91
|
+
homepage_uri: https://gitlab.com/sdwolfz/cache_box_rb
|
92
|
+
source_code_uri: https://gitlab.com/sdwolfz/cache_box_rb
|
93
|
+
post_install_message:
|
85
94
|
rdoc_options: []
|
86
95
|
require_paths:
|
87
96
|
- lib
|
@@ -96,8 +105,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
105
|
- !ruby/object:Gem::Version
|
97
106
|
version: 1.3.1
|
98
107
|
requirements: []
|
99
|
-
rubygems_version: 3.1.
|
100
|
-
signing_key:
|
108
|
+
rubygems_version: 3.1.3
|
109
|
+
signing_key:
|
101
110
|
specification_version: 4
|
102
|
-
summary: A simple, fast, and easy to use
|
111
|
+
summary: A simple, fast, and easy to use local cache.
|
103
112
|
test_files: []
|
data/lib/cache_box_chain.rb
DELETED
@@ -1,177 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class CacheBox
|
4
|
-
class Chain
|
5
|
-
# Input:
|
6
|
-
#
|
7
|
-
# namespace = String | Symbol # Default: :namespace
|
8
|
-
#
|
9
|
-
# Output: N/A
|
10
|
-
def initialize(namespace = :namespace)
|
11
|
-
@cache = ::CacheBox.new(namespace)
|
12
|
-
@chain = []
|
13
|
-
end
|
14
|
-
|
15
|
-
# Input:
|
16
|
-
#
|
17
|
-
# namespace = String | Symbol # Default: :namespace
|
18
|
-
#
|
19
|
-
# Output: self
|
20
|
-
def reset!(namespace = :namespace)
|
21
|
-
@cache.reset!(namespace)
|
22
|
-
@chain = []
|
23
|
-
|
24
|
-
self
|
25
|
-
end
|
26
|
-
|
27
|
-
# Input:
|
28
|
-
#
|
29
|
-
# name = String | Symbol
|
30
|
-
# args = Array[...Object]
|
31
|
-
# &block = Proc(*args)
|
32
|
-
#
|
33
|
-
# Output: self
|
34
|
-
def chain(name, *args, &block)
|
35
|
-
validate_chain!(name, &block)
|
36
|
-
|
37
|
-
@chain.push(
|
38
|
-
[name, args, proc { |*all| @cache.with(name, *all, &block) }]
|
39
|
-
)
|
40
|
-
|
41
|
-
self
|
42
|
-
end
|
43
|
-
|
44
|
-
# Input:
|
45
|
-
#
|
46
|
-
# name = String | Symbol
|
47
|
-
# args = Array[...Object]
|
48
|
-
# &block = Proc(Hash{...Object => Object}, *args)
|
49
|
-
#
|
50
|
-
# Output: self
|
51
|
-
def chain_many(name, *args, &block)
|
52
|
-
validate_chain!(name, &block)
|
53
|
-
|
54
|
-
@chain.push(
|
55
|
-
[name, args, proc { |*all| @cache.with_many(name, *all, &block) }]
|
56
|
-
)
|
57
|
-
|
58
|
-
self
|
59
|
-
end
|
60
|
-
|
61
|
-
# Input:
|
62
|
-
#
|
63
|
-
# all = nil | :all # Default: nil
|
64
|
-
#
|
65
|
-
# Output: Object # Anything the last block in the chain returns.
|
66
|
-
def run!(all = nil)
|
67
|
-
validate_run!(all)
|
68
|
-
|
69
|
-
if all
|
70
|
-
run_all
|
71
|
-
else
|
72
|
-
run_chain
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
# Output: self
|
77
|
-
def expire!
|
78
|
-
@cache.expire!
|
79
|
-
|
80
|
-
self
|
81
|
-
end
|
82
|
-
|
83
|
-
# Input:
|
84
|
-
#
|
85
|
-
# names = ...(String | Symbol)
|
86
|
-
#
|
87
|
-
# Output: self
|
88
|
-
def expire(*names)
|
89
|
-
names.each do |name|
|
90
|
-
@cache.expire(name)
|
91
|
-
end
|
92
|
-
|
93
|
-
self
|
94
|
-
end
|
95
|
-
|
96
|
-
private
|
97
|
-
|
98
|
-
# Input:
|
99
|
-
#
|
100
|
-
# name = String | Symbol
|
101
|
-
# &block = Proc
|
102
|
-
#
|
103
|
-
# Output: N/A
|
104
|
-
def validate_chain!(name, &block)
|
105
|
-
unless name.is_a?(Symbol) || name.is_a?(String)
|
106
|
-
klass = name.class
|
107
|
-
value = name.inspect
|
108
|
-
|
109
|
-
raise(
|
110
|
-
ArgumentError,
|
111
|
-
"name must be a Symbol or a String, got #{klass}: #{value}"
|
112
|
-
)
|
113
|
-
end
|
114
|
-
|
115
|
-
return unless block.nil?
|
116
|
-
|
117
|
-
raise(
|
118
|
-
ArgumentError,
|
119
|
-
'The `#chain/2` method needs to be called with a block'
|
120
|
-
)
|
121
|
-
end
|
122
|
-
|
123
|
-
# Input:
|
124
|
-
#
|
125
|
-
# arg = nil | :all
|
126
|
-
#
|
127
|
-
# Output: N/A
|
128
|
-
def validate_run!(arg)
|
129
|
-
return if arg.nil? || arg == :all
|
130
|
-
|
131
|
-
klass = arg.class
|
132
|
-
value = arg.inspect
|
133
|
-
|
134
|
-
raise(
|
135
|
-
ArgumentError,
|
136
|
-
'The `run!` method only accepts `nil` or the Symbol `:all` as an ' \
|
137
|
-
"argument, got #{klass}: #{value}"
|
138
|
-
)
|
139
|
-
end
|
140
|
-
|
141
|
-
# Output: Object # Anything the last callable returns.
|
142
|
-
def run_all
|
143
|
-
result = nil
|
144
|
-
@chain.each do |_name, args, callable|
|
145
|
-
input = []
|
146
|
-
input << result if result
|
147
|
-
input += args if args
|
148
|
-
|
149
|
-
result = callable.call(*input)
|
150
|
-
end
|
151
|
-
|
152
|
-
result
|
153
|
-
end
|
154
|
-
|
155
|
-
# Output: Object # Anything the last executed callable returns.
|
156
|
-
def run_chain
|
157
|
-
work = []
|
158
|
-
|
159
|
-
@chain.reverse_each do |name, args, callable|
|
160
|
-
work.push([name, args, callable])
|
161
|
-
|
162
|
-
break if @cache.has?(name)
|
163
|
-
end
|
164
|
-
|
165
|
-
result = nil
|
166
|
-
work.reverse_each do |_name, args, callable|
|
167
|
-
input = []
|
168
|
-
input << result if result
|
169
|
-
input += args if args
|
170
|
-
|
171
|
-
result = callable.call(*input)
|
172
|
-
end
|
173
|
-
|
174
|
-
result
|
175
|
-
end
|
176
|
-
end
|
177
|
-
end
|