foobara-cached-command 0.0.1 → 0.0.2
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 +4 -4
- data/CHANGELOG.md +6 -0
- data/src/foobara/cached_command.rb +25 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28d9ff865e9bcef1916074da6800fc794ccd1be7b476eabb0eae805cd30fd927
|
4
|
+
data.tar.gz: 06c371633bde1bd493a2ba945a0b68665d761a4ce13c281cb1e45532b9ca0ec3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a85959c4074b29c67df622245572ffbf1842d4d5439f8bbf92b42be73ab286c595f86c5f0ef878ca3b67b4a06160facd9c6e91274a4f1de084d6e7b91f761b3
|
7
|
+
data.tar.gz: 8ff108e33e8c3b63e519595763e36dda8e82c9d8a3830cdfb63317c8cba04b94febd54762cdf94c42a34169176c18241328a8b8ee19db5712280c50acfd167d5
|
data/CHANGELOG.md
CHANGED
@@ -15,17 +15,22 @@ module Foobara
|
|
15
15
|
unless data
|
16
16
|
if File.exist?(file_path)
|
17
17
|
data = Util.symbolize_keys(JSON.parse(File.read(file_path)))
|
18
|
-
|
19
|
-
|
18
|
+
|
19
|
+
if data.key?(:expires_at)
|
20
|
+
data[:expires_at] = Time.parse(data[:expires_at])
|
21
|
+
cache[key] = data
|
22
|
+
end
|
20
23
|
else
|
21
24
|
return nil
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
25
|
-
if
|
26
|
-
|
27
|
-
|
28
|
-
|
28
|
+
if data.key?(:expires_at)
|
29
|
+
if Time.now > data[:expires_at]
|
30
|
+
cache.delete(key)
|
31
|
+
FileUtils.rm_f(file_path)
|
32
|
+
data = nil
|
33
|
+
end
|
29
34
|
end
|
30
35
|
|
31
36
|
data
|
@@ -40,13 +45,20 @@ module Foobara
|
|
40
45
|
data[:expires_at] = created_at + expiry
|
41
46
|
end
|
42
47
|
|
48
|
+
basedir = File.dirname(file_path)
|
49
|
+
|
50
|
+
FileUtils.mkdir_p(basedir)
|
51
|
+
|
43
52
|
File.write(file_path, JSON.fast_generate(data))
|
44
53
|
cache[key] = data
|
45
54
|
end
|
46
55
|
end
|
47
56
|
|
57
|
+
# We need to prepend this so that we're overriding the method defined in the class we're mixed into
|
48
58
|
module Execute
|
49
59
|
def execute
|
60
|
+
return super if foobara_cache_disabled
|
61
|
+
|
50
62
|
if foobara_cached_value_present?
|
51
63
|
foobara_cached_value
|
52
64
|
else
|
@@ -60,7 +72,8 @@ module Foobara
|
|
60
72
|
include Concern
|
61
73
|
|
62
74
|
inherited_overridable_class_attr_accessor :foobara_cache_expiry,
|
63
|
-
:foobara_cache_serializer
|
75
|
+
:foobara_cache_serializer,
|
76
|
+
:foobara_cache_disabled
|
64
77
|
|
65
78
|
on_include do
|
66
79
|
prepend Execute
|
@@ -83,7 +96,7 @@ module Foobara
|
|
83
96
|
|
84
97
|
# TODO: support various caching strategies like memcached or redis, not just local files
|
85
98
|
def foobara_cache_path
|
86
|
-
"tmp/#{foobara_cache_key}.json"
|
99
|
+
@foobara_cache_path ||= "tmp/cached_command/#{foobara_cache_key}.json".gsub("::", "/")
|
87
100
|
end
|
88
101
|
|
89
102
|
# TODO: support caching based on certain inputs...
|
@@ -121,5 +134,9 @@ module Foobara
|
|
121
134
|
def foobara_cache_expiry
|
122
135
|
self.class.foobara_cache_expiry
|
123
136
|
end
|
137
|
+
|
138
|
+
def foobara_cache_disabled
|
139
|
+
self.class.foobara_cache_disabled
|
140
|
+
end
|
124
141
|
end
|
125
142
|
end
|